使用的maven, dao在service一直自动注入失败..在application.properties中配置了这些东西..pom.xml中引入了mybatis-spring-boot-starter..
å spring3åå¸æ¶mybatisè¿æ²¡æåºæ£å¼çæ¬ï¼æ以spring没ææ´åææ°çmybatis.ä¸è¿ç¤¾åºåæ¯å¼åäºä¸ä¸ªä¸é´ä»¶ã
éè¦çjarå
mybatis-3.0.6.jar
mybatis-spring-1.0.2.jar
è¦ç¹ï¼
å¨springä¸é ç½®mybatiså·¥åç±»
2.å¨daoå±ä½¿ç¨springæ³¨å ¥ççå·¥å ·bean对æ°æ®è¿è¡æä½
æ´åæ¶ï¼å¯ä»¥æåç§æ¹å¼æ¥ä½¿ç¨mybatisè¿è¡æ°æ®å¤çã
spring ä¸å¿ é¡»çé ç½®ã
springçé ç½®æ件ä¸å å ¥ä»¥ä¸å 容
<!-- MyBatisé ç½® -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="c3p0DataSource" />
<property name="configLocation" value="/WEB-INF/config/db/MyBatisConfiguration.xml" />
<property name="mapperLocations" value="/WEB-INF/config/db/*Mapper.xml" />
<property name="typeAliasesPackage" value="${mybatis.alias.basepackage}" />
</bean>
SqlSessionFactoryBean (å¿
é)
å°±æ¯ä¸é´ä»¶æéçå¤çç±»
2.dataSource (å¿ é)
springä¸æ°æ®æºå¼ç¨
3.configLocation (å¯é)
Mybatisèªèº«çé ç½®æ件ï¼ä¸è¬ç¨æ¥å£°æå«å
4.mapperLocation (å¯é)
mybatisçæ å°æ件
5.typeAliasesPackage (å¯é)
è¦æ å°ç±»çå è·¯å¾ï¼å¦æ使ç¨äºè¿ç§æ¹å¼ï¼åconfigLocationä¸ä¸å¿ åè¿è¡å£°æ
使ç¨mybatisè¿è¡æ°æ®å¤ççåç§æ¹å¼(SqlSessionTemplate/SqlSessionDaoSupport/MapperFactoryBean/MapperScannerConfigurer)
ä¸åæ¹å¼çç¹ç¹
SqlSessionTemplate è¿ä¸ªéè¦åé ç½®æ件ï¼å¨å®ç°ç±»ä¸æ³¨å ¥sqlsessionï¼å使ç¨sqlsession,æ¯ç»é¢ç²æ§å¶
SqlSessionDaoSupport è¿ä¸ªåªéè¦å¨å®ç°ç±»ä¸ç»§æ¿ç¹æ®ç±»å°±å¯ä»¥ä½¿ç¨sqlsession
MapperFactoryBean è¿ä¸ªè¦åé ç½®æ件ï¼æ对åºçæææ¥å£å¨é ç½®æ件ä¸å¼ç¨å³å¯ï¼æ éåå®ç°ç±»
MapperScannerConfigurer è¿ä¸ªè¦åé ç½®æ件ï¼åªè¦ç»åºæ¥å£æå¨çå å³å¯ï¼ä¼èªå¨æå ä¸çæ¥å£å¼å ¥ï¼æ éåå®ç°ç±»
SqlSessionTemplate
é ç½®æ件å å ¥æ°é
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
<constructor-arg index="1" value="BATCH" /><!--- å¦ææ³è¦è¿è¡æ¹éæä½å¯å å ¥è¿ä¸ªå±æ§ ->
</bean>
æ³¨å ¥sqlsession()
@Reasource //使ç¨spring3çæ³¨è§£æ³¨å ¥
private SqlSession sqlSession;
使ç¨sqlsessionæ¥è¿è¡æä½
public User getUser(String userId) {
return (User) sqlSession.selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
}
SqlSessionDaoSupport(sqlSessionFactoryä¼è¢«springèªå¨è£ é ,ä¸éè¦æå¨æ³¨å ¥)
继æ¿SqlSessionDaoSupportç±»
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
}
使ç¨getSqlSession()æ¹æ³åsqlSessionæ¥è¿è¡æ°æ®å¤ç
public User getUser(String userId) {
return (User) getSqlSession().selectOne("org.mybatis.spring.sample.mapper.UserMapper.getUser", userId);
}
MapperFactoryBean
åé ç½®æ件,å¼å ¥æ¯ä¸ªDAOæ¥å£
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.mybatis.spring.sample.mapper.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
å¨ä¸å¡å±å¯ç´æ¥æ³¨å ¥daoçæ¥å£è¿è¡æä½
MapperScannerConfigurer
åé ç½®æ件,é ç½®å åå°èªå¨å¼å ¥å ä¸çæææ¥å£
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mybatis.spring.sample.mapper" />
</bean>
å¨ä¸å¡å±å¯ç´æ¥æ³¨å ¥DAOæ¥å£æä½ï¼æ³¨å ¥æ¶ä½¿ç¨çæ¯æ¥å£åï¼å ¶é¦åæ¯å°å
注æï¼å¦ææå«çå®ç°ç±»ï¼å ¶æä¾çå称å¦ææ¯æ¥å£åï¼ä¸é¦åæ¯å°åï¼åä¼å¨å¯å¨æ¶åºç°å²çªé误
追é®æç¨çspringbootæ´ååï¼ä¸æ¯è¿ä¸ªææ
一、可能出现问题的原因
1.sqlmapConfig.xml文件头写错
2.namespace写错
3.包在工程中放错层级,也会报这个错误.因为Springboot,是通过main方法启动工程的,要求是,通过main项目入口启动的模块,必须和他同级或是它的子包,不能放到他外面,否则,会报错
二、会导致,报,dao注入失败
1.学习springboot整合mybaits,根据demo做
2.看整个流程都没问题了,因为配置本身就不多
3.配置文件主要东西都在application.properties中
本回答被网友采纳springBoot1.5.9整合mybatis,开源中国官方推荐的