一、mybatis的常用动态sql标签

一. 定义 sql 语句
select 标签

属性介绍:

  1. id :唯一的标识符.
  2. parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User user
  3. resultType :语句返回值类型或别名。注意,如果是集合,那么这里填写的是集合的泛型,而不是集合本身(resultType resultMap 不能并用)
  1. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Object">
  2. select * from student where id=#{id}
  3. </select

insert 标签

属性介绍:

  1. id :唯一的标识符
  2. parameterType:传给此语句的参数的全路径名或别名 例:com.test.poso.User
  1. <insert id="insert" parameterType="Object">
  2. insert into student
  3. <trim prefix="(" suffix=")" suffixOverrides=",">
  4. <if test="name != null"> NAME, </if>
  5. </trim>
  6. <trim prefix="values(" suffix=")" suffixOverrides=",">
  7. <if test="name != null"> #{name}, </if>
  8. </trim>
  9. </insert>

delete 标签

属性同 insert

  1. <delete id="deleteByPrimaryKey" parameterType="Object">
  2. delete from student where id=#{id}
  3. </delete>

update 标签

属性同 insert
二. 配置 JAVA 对象属性与查询结果集中列名对应关系

resultMap 标签的使用
基本作用:

  1. 建立 SQL 查询结果字段与实体属性的映射关系信息
  2. 查询的结果集转换为 java 对象,方便进一步操作。
  3. 将结果集中的列与 java 对象中的属性对应起来并将值填充进去

!注意:与 java 对象对应的列不是数据库中表的列名,而是查询后结果集的列名

  1. <resultMap id="BaseResultMap" type="com.online.charge.platform.student.model.Student">
  2. <id property="id" column="id" />
  3. <result column="NAME" property="name" />
  4. <result column="HOBBY" property="hobby" />
  5. <result column="MAJOR" property="major" />
  6. <result column="BIRTHDAY" property="birthday" />
  7. <result column="AGE" property="age" />
  8. </resultMap>
  9. <!--查询时resultMap引用该resultMap -->
  10. <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="Object">
  11. select id,name,hobby,major,birthday,age from student where id=#{id}
  12. </select>

标签说明:

主标签:

  1. id:该 resultMap 的标志
  2. type:返回值的类名,此例中返回 Studnet

子标签:

  1. id:用于设置主键字段与领域模型属性的映射关系,此处主键为 ID,对应 id
  2. result:用于设置普通字段与领域模型属性的映射关系

三. 动态 sql 拼接
if 标签

if 标签通常用于 WHERE 语句、UPDATE 语句、INSERT 语句中,通过判断参数值来决定是否使用某个查询条件、判断是否更新某一个字段、判断是否插入某个字段的值。

  1. <if test="name != null and name != ''">
  2. and NAME = #{name}
  3. </if>

foreach 标签

foreach 标签主要用于构建 in 条件,可在 sql 中对集合进行迭代。也常用到批量删除、添加等操作中。

  1. <!-- in查询所有,不分页 -->
  2. <select id="selectIn" resultMap="BaseResultMap">
  3. select name,hobby from student where id in
  4. <foreach item="item" index="index" collection="list" open="(" separator="," close=")">
  5. #{item}
  6. </foreach>
  7. </select>

属性介绍:

  1. collectioncollection 属性的值有三个分别是 listarraymap 三种,分别对应的参数类型为:List、数组、map 集合。
  2. item :表示在迭代过程中每一个元素的别名
  3. index :表示在迭代过程中每次迭代到的位置(下标)
  4. open :前缀
  5. close :后缀
  6. separator :分隔符,表示迭代时每个元素之间以什么分隔

choose 标签

有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis 提供了 choose 元素,按顺序判断 when 中的条件出否成立,如果有一个成立,则 choose 结束。当 choose 中所有 when
的条件都不满则时,则执行 otherwise 中的 sql。类似于 Java 的 switch 语句,choose 为 switch,when 为 case,otherwise 则为 default。

if 是与(and)的关系,而 choose 是或(or)的关系。

  1. <select id="getStudentListChoose" parameterType="Student" resultMap="BaseResultMap">
  2. SELECT * from STUDENT WHERE 1=1
  3. <where>
  4. <choose>
  5. <when test="Name!=null and student!='' ">
  6. AND name LIKE CONCAT(CONCAT('%', #{student}),'%')
  7. </when>
  8. <when test="hobby!= null and hobby!= '' ">
  9. AND hobby = #{hobby}
  10. </when>
  11. <otherwise>
  12. AND AGE = 15
  13. </otherwise>
  14. </choose>
  15. </where>
  16. </select>

四. 格式化输出
where 标签

当 if 标签较多时,这样的组合可能会导致错误。 如下:

  1. <select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap">
  2. SELECT * from STUDENT WHERE
  3. <if test="name!=null and name!='' ">
  4. NAME LIKE CONCAT(CONCAT('%', #{name}),'%')
  5. </if>
  6. <if test="hobby!= null and hobby!= '' ">
  7. AND hobby = #{hobby}
  8. </if>
  9. </select>

当 name 值为 null 时,查询语句会出现 “WHERE AND” 的情况,解决该情况除了将”WHERE”改为“WHERE 1=1”之外,还可以利用 where
标签。这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以 AND 或 OR 开头的,则它会剔除掉。

  1. <select id="getStudentListWhere" parameterType="Object" resultMap="BaseResultMap">
  2. SELECT * from STUDENT
  3. <where>
  4. <if test="name!=null and name!='' ">
  5. NAME LIKE CONCAT(CONCAT('%', #{name}),'%')
  6. </if>
  7. <if test="hobby!= null and hobby!= '' ">
  8. AND hobby = #{hobby}
  9. </if>
  10. </where>
  11. </select>

set 标签

没有使用 if 标签时,如果有一个参数为 null,都会导致错误。当在 update 语句中使用 if 标签时,如果最后的 if 没有执行,则或导致逗号多余错误。使用 set 标签可以将动态的配置 set
关键字,和剔除追加到条件末尾的任何不相关的逗号。

  1. <update id="updateStudent" parameterType="Object">
  2. UPDATE STUDENT
  3. SET NAME = #{name},
  4. MAJOR = #{major},
  5. HOBBY = #{hobby}
  6. WHERE ID = #{id};
  7. </update>
  8. <update id="updateStudent" parameterType="Object">
  9. UPDATE STUDENT SET
  10. <if test="name!=null and name!='' ">
  11. NAME = #{name},
  12. </if>
  13. <if test="hobby!=null and hobby!='' ">
  14. MAJOR = #{major},
  15. </if>
  16. <if test="hobby!=null and hobby!='' ">
  17. HOBBY = #{hobby}
  18. </if>
  19. WHERE ID = #{id};
  20. </update>

使用 set+if 标签修改后,如果某项为 null 则不进行更新,而是保持数据库原值。

  1. <update id="updateStudent" parameterType="Object">
  2. UPDATE STUDENT
  3. <set>
  4. <if test="name!=null and name!='' ">
  5. NAME = #{name},
  6. </if>
  7. <if test="hobby!=null and hobby!='' ">
  8. MAJOR = #{major},
  9. </if>
  10. <if test="hobby!=null and hobby!='' ">
  11. HOBBY = #{hobby}
  12. </if>
  13. </set>
  14. WHERE ID = #{id};
  15. </update>

trim 标签

请点击此链接

五. 配置关联关系

5.1 collection 标签

5.2 association 标签
关联映射关系

六. 定义常量及引用
sql 标签

当多种类型的查询语句的查询字段或者查询条件相同时,可以将其定义为常量,方便调用。为求 结构清晰也可将 sql 语句分解。

  1. <!-- 查询字段 -->
  2. <sql id="Base_Column_List">
  3. ID,MAJOR,BIRTHDAY,AGE,NAME,HOBBY
  4. </sql>
  5. <!-- 查询条件 -->
  6. <sql id="Example_Where_Clause">
  7. where 1=1
  8. <trim suffixOverrides=",">
  9. <if test="id != null and id !=''">
  10. and id = #{id}
  11. </if>
  12. <if test="major != null and major != ''">
  13. and MAJOR = #{major}
  14. </if>
  15. <if test="birthday != null ">
  16. and BIRTHDAY = #{birthday}
  17. </if>
  18. <if test="age != null ">
  19. and AGE = #{age}
  20. </if>
  21. <if test="name != null and name != ''">
  22. and NAME = #{name}
  23. </if>
  24. <if test="hobby != null and hobby != ''">
  25. and HOBBY = #{hobby}
  26. </if>
  27. <if test="sorting != null">
  28. order by #{sorting}
  29. </if>
  30. <if test="sort!= null and sort != ''">
  31. order by ${sort} ${order}
  32. </if>
  33. </trim>
  34. </sql>

include 标签

用于引用定义的常量

  1. <!-- 查询所有,不分页 -->
  2. <select id="selectAll" resultMap="BaseResultMap">
  3. SELECT
  4. <include refid="Base_Column_List" />
  5. FROM student
  6. <include refid="Example_Where_Clause" />
  7. </select>
  8. <!-- 分页查询 -->
  9. <select id="select" resultMap="BaseResultMap">
  10. select * from (
  11. select tt.*,rownum as rowno from
  12. (
  13. SELECT
  14. <include refid="Base_Column_List" />
  15. FROM student
  16. <include refid="Example_Where_Clause" />
  17. ) tt
  18. <where>
  19. <if test="pageNum != null and rows != null">
  20. and rownum
  21. <![CDATA[<=]]>#{page}*#{rows}
  22. </if>
  23. </where>
  24. ) table_alias
  25. where table_alias.rowno>#{pageNum}
  26. </select>
  27. <!-- 根据条件删除 -->
  28. <delete id="deleteByEntity" parameterType="java.util.Map">
  29. DELETE FROM student
  30. <include refid="Example_Where_Clause" />
  31. </delete>