mybatis系列技术之五 问题集合

1. 时间出现yyyy-MM-dd HH:mm:ss.0 的情况:

今天有个同事和我反馈是数据库中时间没有后面的.0 ,通过mybatis 查询出来的数据出现了, 是因为映射类型不一致导致的。

案例:在实体中使用的是String, 在配置文件中使用的是VARCHAR, 查询出来开的就会映射成为yyyy-MM-dd HH:mm:ss.0  格式

  1. <result column="created_time" jdbcType="VARCHAR" property="createTime" />  
  2. <result column="pay_time" jdbcType="VARCHAR" property="payTime" />  

问题解决:

<!-- 在实体中, 修改类型和数据库的对应, 数据库字段:timestamp;   实体用Date, 两个类型对应后, 就不会出现.0 -->

  1. <result column="created_time" jdbcType="TIMESTAMP" property="createTime" />  
  2. <result column="pay_time" jdbcType="TIMESTAMP" property="payTime" />  

2. Mybatis 找不到数据库驱动Driver:

MyBatis异常 Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException:
原因是因为: 没有引入配置文件。
<!-- 引入properties 文件 -->
<properties resource="jdbc.properties"></properties>
mybatis系列技术之五 问题集合

3. 由于做更新操作, 但是使用了select 标签导致的:

attempted to return null from a method with a primitive return type (int).


  1. <select id="updateOffLineRepositoryShelf" parameterType="java.util.Map" >  
  2.     update repository_shelf set issuance = #{issuance} where spu_id in  
  3.     <foreach item="item" index="index" collection="spuIds" open="(" separator="," close=")"> #{item}</foreach>  
  4. </select>  
解决方案: 将select 修改为update