Mybatis:结果处理+嵌套查询+动态sql

目录

一、结果处理

1.单表结果处理

2.多表关联结果集处理

3.association与collection标签

二、常用注解标签

三、添加单元测试

四、动态sql 

1. where、if

2.trim

3.choose、when、otherwise


一、结果处理

1.单表结果处理

        当返回结果为对象时,通常sql中的列名与对象中的属性名相同,mybatis默认结果会自动映射到对象中。

2.多表关联结果集处理

        多张表关联查询时,mybatis不会将结果全部正确的自动映射到对象中,在settings标签中name为autoMappingBehavior标签可以关闭自动映射,有三个value值:

  1. NONE 表示关闭自动映射
  2. PARTIAL 只会自动映射没有定义嵌套结果映射的字段(mybatis默认值)
  3. FULL 会自动映射任何复杂的结果集(无论是否嵌套)

注意:在使用FULL值时,若类之间有相同的属性名时,映射结果可能会出现问题,例如如果出现一个正确映射值id,该值可能会映射到其他的相同属性id中。

比较方便地会使用mybatis默认的结果映射方式。

3.association与collection标签

association用于当查询结果需要封装到另一个类且结果是单个对象时,在resultMap中的一个标签例如以下方式查询的结果封装:

返回结果如下列Student类

 标签id为主键,result为普通列名,默认情况下需要配置返回类型结果的属性,否则只有自己的属性类映射到结果。

以下是嵌套查询方式,association中的select对应到相同id的select标签中,colum为主select标签中sql语句的对应列将作为子sql语句的参数。

collection用于当查询结果需要封装到另一个类且结果是多个对象封装到集合中时,在resultMap中的一个标签例如以下方式查询的结果封装:

返回结果如下列Dorm类

 以下是嵌套查询方式,与collection相同

sql语句将相同domid的student封装到返回类Dorm中的List<Student>中。 

二、常用注解标签

@Insert : 插入 sql , 和 xml insert sql 语法完全一样
@Select : 查询 sql, 和 xml select sql 语法完全一样
@Update : 更新 sql, 和 xml update sql 语法完全一样
@Delete : 删除 sql, 和 xml delete sql 语法完全一样
@Param : 入参
@Results : 设置结果集合
@Result : 结果

示例:

public interface StudentDao_back {
    Student findStuById(int id);

    Student findStuById1(int id);

    @Delete("delete from student where id=#{id}")
    void delStudentById(int id);

    @Insert("insert into student(num,name,gender) value (#{num},#{name},#{gender})")
    void insertStudent(Student student);


    @Select("select num,name,gender from student where id = #{id}")
    @Results(id = "studentMap",value ={
            @Result(column = "num",property = "num"),
            @Result(column = "name",property = "name"),
            @Result(column = "gender",property = "gender")

    })
    Student findStuById2(int id);
}

三、添加单元测试

在配置文件中添加依赖,使用@Test可以在任意类直接测试中方法

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>

 

四、动态sql 

详细动态sql使用方法可以查看官网:mybatis – MyBatis 3 | 动态 SQL

1. where、if

 如果<if>中的条件满足, <where>会自动删除多余的“and”

    <select id="findstuBySQldt" resultType="Student" parameterType="Student">
        select id,num,gender from student
        <where>
            <if test="num!=0"> num = #{num}</if>
            <if test="name!=null"> and name = #{name}</if>
            <if test="gender!=null"> and gender=#{gender}</if>
        </where>
    </select>

2.trim

where 标签,其实用 trim 也可以表示,当 WHERE 后紧随 AND 或则 OR 的时候,就去除 AND 或者 OR。prefix 前缀,prefixOverrides 覆盖首部指定内容。 

3.choose、when、otherwise

有点像于Java中的switch语句,首先when中成立的添加到sql中,其余后面的when不会添加到sql中,否则(when中条件都不成立)添加otherwise中的sql。

 <select id="findstuBySQldt1" resultType="Student">
            select id,num,gender from student
            <trim prefix="where" prefixOverrides="and">
                <choose>
                       <when test="num!=0">num=#{num}</when>
                       <when test="name!=null">and name =#{name}</when>
                       <otherwise>and gender="男" </otherwise>
                </choose>
            </trim>
    </select>

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小俱的一步步

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值