回顾mybatis的标签

resultType和resultMap的区别

resultType只有查询出来的列名和pojo的属性名一致,才可以映射成功

resultMap可以自定义映射关系,比如

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!-- 定义resultType  
select id id_,username _username from userUser类中的属性做一个映射关系
type:resultMap最终所映射的Java对象类型,可以使用别名
id:对resultMap的唯一标识 -->
<resultMap type="user" id="userResultMap">
<!-- id表示查询结果集中唯一标识
column:查询出的列名
property:type所指定的POJO中的属性名
最终reslutMap对column和property做一个映射关系(对应关系)
-->
<id column="_id" property="id"/>
<!-- 对普通列的映射定义 -->
<result column="_username" property="username"/>
</resultMap>

动态拼接sql

  • <if>标签 判断标签,常用在where中

  • <foreach>标签 通常用在批量删除、添加等操作中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 /*我们假如说参数为----  int[] ids = {1,2,3,4,5}  ----那么打印之后的SQL如下:

  delete form user where id in (1,2,3,4,5)
  
   collection :collection属性的值有三个分别是list、array、map三种,分别对应的参数类型为:List、数组、map集合,下面因为参数为数组,所以值为array

    item : 表示在迭代过程中每一个元素的别名

    index :表示在迭代过程中每次迭代到的位置(下标)

    open :前缀

    close :后缀

    separator :分隔符,表示迭代时每个元素之间以什么分隔
*/
  
 
<delete id="deleteBatch"> 

    delete from user where id in
<!--这里有个奇怪的现象,collection中填写array我一直报错,填写array的参数名则正确-->
<!--collection中填写的是array的参数名-->
  <foreach collection="id" item="id" index="index" open="(" close=")" separator=",">

      #{id}

  </foreach>

</delete>
  • choose标签 类似于java中的switch语句
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<select id="" parameterType="" resultMap="">     
    SELECT * from STUDENT_TBL ST      
    <where>     
        <choose>     
            <when 条件1>
             执行1     
            </when>     
            <when 条件2>
             执行2     
            </when>     
            <when 条件3>
             执行3     
            </when>      
            <otherwise>     
                 以上条件都不满足,执行此方法 
            </otherwise>     
        </choose>     
    </where>     
</select>

格式化输出

  • <where>标签 它会判断如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。就不会出现判断语句中出现‘WHERE AND’之类关键字多余的sql错误
  • set标签 在update语句中使用if,如果前面的if没有执行则会出现逗号多余错误,使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<update id="update">
update t_message
<set>
<if test="name != null and name != ''">
name = #{name},
</if>
<if test="address != null and address != ''">
address = #{address},
</if>
<if test="business != null and business != ''">
business = #{business},
</if>
<if test="level != null and level != ''">
level = #{level},
</if>
<if test="total != null and total != ''">
total = #{total},
</if>
<if test="do_business != null and do_business != ''">
do_business = #{do_business},
</if>
</set>
where id = #{id};
</update>
  • trim标签 灵活的去除多余关键字的,可实现where和set的效果
1
2
<trim prefix="WHERE" prefixOverrides="AND|OR">   等同于<where>标签
<trim prefix="SET" suffixOverrides=","> 等同于<set>标签

关联查询

当关联的是个List或者Set等多个对象时,使用 <collcetion>标签,如

1
private List<WarningDown> warningDownForMeet;

当关联的只有一个对象时,使用<association>标签,如

1
private Company company;

如果我需要查找用户(封装在user类)的时候也能找到他的一篇文章(封装在Article类)

1.首先需要在user类中添加Article属性

1
2
3
4
private String id;//主键
private String userName;//用户姓名
private Article article;//新增的文章属性
//省略getter/setter方法

2.在user对应的mapper中将id传过去

1
2
3
4
5
6
7
<resultMap id="userResultMap" type="test.mybatis.entity.User">
<id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/>
<result column="userName" property="userName" jdbcType="VARCHAR" javaType="java.lang.String"/>
//这里把user的id传过去
<association property="article" column="id"
select="test.mybatis.dao.articleMapper.selectArticleByUserId" />//test.mybatis.dao.articleMapper为命名空间
</resultMap>

3.在article对应的mapper中

1
2
3
4
5
6
7
8
9
10
<resultMap id="articleResultMap" type="test.mybatis.entity.Article">
<id column="id" property="id" jdbcType="VARCHAR" javaType="java.lang.String"/>
<result column="articleTitle" property="articleTitle" jdbcType="VARCHAR" javaType="java.lang.String"/>
<result column="articleContent" property="articleContent" jdbcType="VARCHAR" javaType="java.lang.String"/>
</resultMap>

<!--这边的id要和前面的user所对应的mapper中的association标签中的select后面的命名相同-->
<select id="selectArticleByUserId" parameterType="java.lang.String" resultMap="ArticleResultMap" >
select * from tb_article where userId=#{userId}
</select>
赏个🍗吧
0%