1. 程式人生 > >No bean named 'sqlSessionFactory' is defined

No bean named 'sqlSessionFactory' is defined

-s property apc sql 位置 onf 定義 con sca

  其實程序給出這個錯誤已經很明確了,說你的sqlSessionFactory是未定義的。

  首先你要查看一下自己的web.xml文件是否配置了application*.xml

<!--設置spring 配置文件的位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring/applicationContext-*.xml</param-value>
</context-param>
<!--springmvc前端控制器配置-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

然後查看你的application*.xml文件裏面是否掃描了mapper接口,有沒有註入sqlsessionFaction ,代碼如下。
<!--sqlsessionFactory bean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/mybatis/SqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
</bean>

<!--自動掃描mapper接口,並註入sqlsession-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.syw.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

配置好後SqlMapConfig.xml中找相應文件就可以了。文件內容如下。

技術分享



No bean named 'sqlSessionFactory' is defined