1、首先说一下SqlSessionDaoSupport的问题,在mybatis-spring1.1的时候我们直接这样写:
@Repository public class PersonnelDaoImpl extends SqlSessionDaoSupport implements PersonnelDao { @Override public Personnel getById(Integer id) { return this.getSqlSession().selectOne( "com.sanmina.tms.entity.Personnel.getById", id); } }
只需加上@Respositry 就可以了。但是在mybatis-spring1.2以后SqlSessionDaoSupport的源码有所改动(具体改动自己看源码),也就是说用上面的这种做法就会报错,如下:
Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required at org.springframework.util.Assert.notNull(Assert.java:112) at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:74) at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562) ... 12 more
所以我们就要在原来的代码上加上一点东西如下,自动加上sqlSessionFactory。
@Repository public class PersonnelDaoImpl extends SqlSessionDaoSupport implements PersonnelDao { @Override public Personnel getById(Integer id) { return this.getSqlSession().selectOne( "com.sanmina.tms.entity.Personnel.getById", id); } @Override @Autowired public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) { super.setSqlSessionFactory(sqlSessionFactory); } }
直接重写setSqlSessionFactory()方法,并加上@Autowired。Ok!
继续往下走,问题又来了:
2、mybatis 的mapping.xml中的resultMap和resultType的问题:
Caused by: org.apache.ibatis.executor.ExecutorException: A query was run and no Result Maps were found for the Mapped Statement 'com.sanmina.tms.entity.Personnel.getById'. It's likely that neither a Result Type nor a Result Map was specified. at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.validateResultMapsCount(DefaultResultSetHandler.java:234) at org.apache.ibatis.executor.resultset.DefaultResultSetHandler.handleResultSets(DefaultResultSetHandler.java:157) at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:63) at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:78) at org.apache.ibatis.executor.BatchExecutor.doQuery(BatchExecutor.java:91) at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:303) at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:154) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:102) at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:82)
原因是我的xml里没有写resultMap或者resultType:
<select id="getById" parameterType="Integer" > select * from tb_personnel where id = #{id} </select>
于是我加上:
<select id="getById" parameterType="Integer" resultMap="Personnel"> select * from tb_personnel where id = #{id} </select>
问题又来了:
Caused by: java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.sanmina.tms.entity.Personnel.personnel at org.apache.ibatis.session.Configuration$StrictMap.get(Configuration.java:818) at org.apache.ibatis.session.Configuration.getResultMap(Configuration.java:570) at org.apache.ibatis.builder.MapperBuilderAssistant.setStatementResultMap(MapperBuilderAssistant.java:356) ... 17 more
原因是resultMap需要自己配置,于是我又加上了如下的配置:
<resultMap type="Personnel" id="personnel"> </resultMap> <select id="getById" parameterType="Integer" resultMap="personnel"> select * from tb_personnel where id = #{id} </select>
心想这个TM总能走了吧。于是结果如下:
Personnel [id=1, employeeID=null, companyID=null, cnName=null, department=CDC IT, section=SFDC team, positonName=null, superiorName=null, remarkDesc=null, emClassic=null]
长吁一口气,终于出来结果了。。。TM再仔细一数据,怎么有的是null,在查数据库明明不是null啊。真是日了狗...于是接着走。终于找到问题所在:因为我的实体类的有的字段和数据表的字段不是对应的,所以我们要将字段配置对应关系: 如下
<resultMap type="Personnel" id="personnel"> <id property="id" column="id"/> <result property="employeeID" column="employee_id"/> <result property="companyID" column="company_id"/> <result property="cnName" column="cn_name"/> <result property="positonName" column="position_name"/> <result property="superiorName" column="superior_name"/> <result property="remarkDesc" column="remark_desc"/> <result property="emClassic" column="em_classic"/> </resultMap> <select id="getById" parameterType="Integer" resultMap="personnel"> select * from tb_personnel where id = #{id} </select>
在走!:
Personnel [id=1, employeeID=1016969, companyID=22222, cnName=Mercy, department=CDC IT, section=SFDC team, positonName=programer, superiorName=Tommy, remarkDesc=XXX, emClassic=IDE]
终于TM的Ok了,容我喝口水先......
源码的git地址:https://git.oschina.net/mercy_yang/SMP-Demo