Mybatis-04-结果集映射resultMap/动态SQL/关联查询

一.结果集映射resultMap

resultType可以指定将查询结果映射为持久化类,但需要持久化类的属性名和sql查询的列名一致才能映射成功。如果数据库中的表的字段名和持久化类的属性名不一致,则需要通过resultMap将字段名和属性名建立起对应关系
例如:
数据库表bbs_employee

Mybatis-04-结果集映射resultMap/动态SQL/关联查询_第1张图片

持久化类BbsEmployee

public class BbsEmployee {

    private String username;
    private String password;
    private String degree;
    private String email;
    private Boolean gender;
    private String imgUrl;
    private String phone;
    private String realName;
    private String school;
    .......

需要在mapper.xml文件中添加配置:

  
  <resultMap id="BaseResultMap" type="com.sandking.mybatis.pojo.BbsEmployee" >

    <id column="username" property="username" jdbcType="VARCHAR" />
    
    

    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="degree" property="degree" jdbcType="VARCHAR" />
    <result column="email" property="email" jdbcType="VARCHAR" />
    <result column="gender" property="gender" jdbcType="BIT" />
    ......
    <result column="is_del" property="isDel" jdbcType="BIT" />
  resultMap>

< resultMap >标签与< select >< insert >等标签同级

二.动态SQL

通过Mybatis为mapper映射文件提供的几种标签,实现动态**拼接**SQL,即SQL不是写死的而是根据传入的参数可以发生变化的

1.if标签

为参数设定条件,当条件满足才会将if标签内的内容拼接到向数据库发送的SQL中
if标签的使用案例:
mapper.xml文件

<select id="queryTest" parameterType="com.sandking.mybatis.pojo.BbsEmployee"
          resultType="com.sandking.mybatis.pojo.BbsEmployee">
    SELECT * FROM `bbs_employee`
    WHERE 1=1
    
    <if test="username != null and username !=''">
      AND username  LIKE #{username}
    if>
    <if test="password != null and password !=''">
      AND password LIKE #{password}
    if>
  select>

测试程序:

@Test
    public void testQuery(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BbsEmployee employee = new BbsEmployee();
        employee.setUsername("%ad%");
        //传入查询参数,password的值不为空
        employee.setPassword("%1%");
        BbsEmployee bbsEmployee = sqlSession.selectOne("queryTest",employee);

        System.out.println(bbsEmployee);
        sqlSession.close();

    }

查询结果:

Mybatis-04-结果集映射resultMap/动态SQL/关联查询_第2张图片

Mybatis向数据库发送的查询语句中有两个限定范围的条件,username和password

更改测试程序传入的查询参数值

@Test
    public void testQuery(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BbsEmployee employee = new BbsEmployee();
        employee.setUsername("%ad%");
        //设置传入参数password的值为空
        employee.setPassword("");
        BbsEmployee bbsEmployee = sqlSession.selectOne("queryTest",employee);

        System.out.println(bbsEmployee);
        sqlSession.close();

    }

查询结果:

Mybatis-04-结果集映射resultMap/动态SQL/关联查询_第3张图片

可以看到,因为password的值为null,不符合if标签的条件,if标签内的内容就没有拼接到向数据库发送的SQL中

2.foreach标签

如果SQL的参数是一个数组,例如:

SELECT * FROM `bbs_employee` WHERE id IN ( ? , ? , ? , ? )

需要传递id的值,而且不确定到底有多少个id值(即SQL需要根据参数的数量做相应更改),这是就需要用到foreach标签
foreach标签的使用案例:

在BbsEmployee持久化类中增加一个字符串数组

private List<String> ids;

mapper映射文件:

    <select id="query3" parameterType="com.sandking.mybatis.pojo.BbsEmployee"
          resultType="com.sandking.mybatis.pojo.BbsEmployee">
    SELECT * FROM `bbs_employee`
    WHERE
    
    
    
    
    
    
    <foreach collection="ids" item="id" open="id IN (" close=")"
             separator=",">
      #{id}
    foreach>
  select>

测试程序:

    @Test
    public void query3(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        BbsEmployee employee = new BbsEmployee();
        List<String> list = new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("4");
        list.add("5");
        employee.setIds(list);
        List<BbsEmployee> list1 = sqlSession.selectList("query3",employee);
        for (BbsEmployee e:list1) {
            System.out.println(e);
        }
        sqlSession.close();

    }

查询结果:
Mybatis-04-结果集映射resultMap/动态SQL/关联查询_第4张图片

3.include标签

用于抽取相同的SQL片段,增加重用性,减少书写量

include标签的使用案例:

  <select id="query3" parameterType="com.sandking.mybatis.pojo.BbsEmployee"
          resultType="com.sandking.mybatis.pojo.BbsEmployee">
    <include refid="select"/>
    WHERE
    <foreach collection="ids" item="id" open="id IN (" close=")"
             separator=",">
      #{id}
    foreach>
  select>

  <sql id="select">
    SELECT * FROM `bbs_employee`
  sql>

4.where标签

用于代替传统SQL语句中的“where 1=1”

where标签的使用案例:
传统方式:

<select id="queryTest" parameterType="com.sandking.mybatis.pojo.BbsEmployee"
          resultType="com.sandking.mybatis.pojo.BbsEmployee">
    SELECT * FROM `bbs_employee`
    WHERE 1=1
    <if test="username != null and username !=''">
      AND username  LIKE #{username}
    if>
    <if test="password != null and password !=''">
      AND password LIKE #{password}
    if>
  select>

使用where标签:

<select id="queryTest" parameterType="com.sandking.mybatis.pojo.BbsEmployee"
          resultType="com.sandking.mybatis.pojo.BbsEmployee">
    SELECT * FROM `bbs_employee`
    <where>
      <if test="username != null and username !=''">
        AND username  LIKE #{username}
      if>
      <if test="password != null and password !=''">
        AND password LIKE #{password}
      if>
    where>
  select>

查询结果:

Mybatis-04-结果集映射resultMap/动态SQL/关联查询_第5张图片

可以看出where标签自动去掉了username的前的AND关键字

三.关联查询

你可能感兴趣的:(Mybatis,Mybatis从入门到入土)