在使用springboot和mybatis时出现了一些bug,修改时发现了一些问题。
1. Dao层的Mapper对象注入
Dao层的对象在注入Mapper对象时,创建mapper的bean和注入mapper的时机是在调用mapper时,此次创建和注入时对dao类中的所有mapper而言。Dao层的对象在注入Mapper对象时,创建mapper的bean和注入mapper的时机是在调用mapper时,此次创建和注入时对dao类中的所有mapper而言(注,mapper接口上要写@mapper和@Repository注解,mapper接口内通过注解写sql 的方法:@insert=({}))。
public class testDao{
@Autowired
private Test1Mapper test1Mapper;
@Autowired
private Test2Mapper test2Mapper;
public void test(){
tes1Mapper.insert(1);
}
}
在此代码中,当调用testDao类的test()方法时,若IOC容器中未有对应的bean,test1Mapper会被创建并注入,同时test2Mapper也会被创建和注入。所以出现报错时,除了test1Mapper存在问题,有可能是test2Mapper存在问题,debug时要注意,打印日志时可以打印的出来,仔细阅读即可。(Mapper的注入时机并没有看过源码,在自己编程过程中遇到了这个问题特此记录下,准确性需验证)
2. trim标签
1、trim 有四个属性
2、prefix,suffix 表示在trim标签包裹的部分的前面或者后面添加内容(注意:是没有prefixOverrides,suffixOverrides的情况下)
3、如果有prefixOverrides,suffixOverrides 表示覆盖Overrides中的内容。
4、如果只有prefixOverrides,suffixOverrides 表示删除。
@Insert({
"<script>",
"insert into stat",
"<trim prefix='(' suffix=')' suffixOverrides=','>",
"<if test='batchNo != null'>batch_no,</if>",
"<if test='jobType != null'>job_type,</if>",
"last_modified_date,",
"</trim>",
"<trim prefix='values (' suffix=')' suffixOverrides=','>",
"<if test='batchNo != null'>#{batchNo,jdbcType=VARCHAR},</if>",
"<if test='jobType != null'>#{jobType,jdbcType=VARCHAR},</if>",
"current_timestamp(3),",
"</trim>",
"</script>"
})
int insert(@Param("batchNo")String batchNo,@Param("jobType")String jobType);
如果传入的参数中batchNo有值,jobType为null,则sql语句会被替换为
insert into stat batch_no,last_modified_date values (batchNo,xxx);
3. SelectKey注解
<selectKey keyProperty="目标属性" resultType="结果类型" order="BEFORE"></selectKey>
用来获取自增主键
4. @Results
MyBatis中使用@Results注解来映射查询结果集到实体类属性。(注,用法介绍抄自博客https://blog.youkuaiyun.com/cherlshall/article/details/80950150)
(1)@Results的基本用法。当数据库字段名与实体类对应的属性名不一致时,可以使用@Results映射来将其对应起来。column为数据库字段名,porperty为实体类属性名,jdbcType为数据库字段数据类型,id为是否为主键。
@Select({"select id, name, class_id from my_student"})
@Results({
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="class_id ", property="classId", jdbcType=JdbcType.INTEGER)
})
List<Student> selectAll();
如上所示的数据库字段名class_id与实体类属性名classId,就通过这种方式建立了映射关系。
(2)@ResultMap的用法。当这段@Results代码需要在多个方法用到时,为了提高代码复用性,我们可以为这个@Results注解设置id,然后使用@ResultMap注解来复用这段代码。
@Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="class_id ", property="classId", jdbcType=JdbcType.INTEGER)
})
List<Student> selectAll();
@Select({"select id, name, class_id from my_student where id = #{id}"})
@ResultMap(value="studentMap")
Student selectById(integer id);
(3)@One的用法。当我们需要通过查询到的一个字段值作为参数,去执行另外一个方法来查询关联的内容,而且两者是一对一关系时,可以使用@One注解来便捷的实现。比如当我们需要查询学生信息以及其所属班级信息时,需要以查询到的class_id为参数,来执行ClassesMapper中的selectById方法,从而获得学生所属的班级信息。可以使用如下代码。
@Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="class_id ", property="myClass", javaType=MyClass.class,
one=@One(select="com.my.mybatis.mapper.MyClassMapper.selectById"))
})
List<Student> selectAllAndClassMsg();
(4)@Many的用法。与@One类似,只不过如果使用@One查询到的结果是多行,会抛出TooManyResultException异常,这种时候应该使用的是@Many注解,实现一对多的查询。比如在需要查询学生信息和每次考试的成绩信息时。
@Select({"select id, name, class_id from my_student"})
@Results(id="studentMap", value={
@Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
@Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
@Result(column="class_id ", property="classId", jdbcType=JdbcType.INTEGER),
@Result(column="id", property="gradeList", javaType=List.class,
many=@Many(select="com.my.mybatis.mapper.GradeMapper.selectByStudentId"))
})
List<Student> selectAllAndGrade();
(5) 注意用@ResultMap有个坑,如上例中,假如:
@ResultMap(value="studentMap")
Teacher selectTeacher();
selectAll()返回的数据通过studentMap映射后会变为Student类型(即定义@Results时的类型),将导致类型转换错误,抛出ClassCastException,解决方法是不要使用@ResultMap,重新写一个@Results(id=“teacher” value = {}) 即可(id=“teacher”可以去掉).