四、动态sql
1、动态sql概述
- 动态 SQL 是 MyBatis 的强大特性之一
- 使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦
2、if和where标签
-
StudentMapper.java
public List<Student> getStudentByCondition(Student stu);
-
StudentMapper.xml <select id="getStudentByCondition" resultType="day01.domain.Student">
select
id,studentName name,gender
from
tbl_student
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="name != null && name != ''">
and studentName like #{name}
</if>
<if test="gender != null && gender != ''">
and gender like #{gender}
</if>
</where>
</select>
3、trim标签
-
StudentMapper.java
public List<Student> getStudentByTrim(Student stu);
-
StudentMapper.xml
<select id="getStudentByTrim" resultType="day01.domain.Student">
select id,studentName name,gender from tbl_student
<trim prefix="where" suffixOverrides="and">
<if test="id != null">
id = #{id} and
</if>
<if test="name != null && name != ''">
studentName like #{name} and
</if>
<if test="gender != null && gender != ''">
gender like #{gender} and
</if>
</trim>
</select>
4、choose标签
-
StudentMapper.java
public List<Student> getStudentByChoose(Student stu);
-
StudentMapper.xml <select id="getStudentByChoose" resultType="day01.domain.Student">
select id,studentName name,gender from tbl_student
<where>
<choose>
<when test="id != null">
and id = #{id}
</when>
<when test="name != null">
and studentName like #{name}
</when>
<when test="gender != null">
and gender like #{gender}
</when>
<otherwise>
1 = 1
</otherwise>
</choose>
</where>
</select>
5、set标签
-
StudentMapper.java
public void setStudentBySet(Student stu);
-
StudentMapper.xml <update id="setStudentBySet">
update tbl_student
<set>
<if test="name != null">
studentName = #{name},
</if>
<if test="gender != null">
gender = #{gender}
</if>
</set>
<where>
id = #{id}
</where>
</update>
6、foreach标签
-
StudentMapper.java
public List<Student> getStudentByForeach(@Param("ids") List<Integer> ids);
public Integer insertStudentByForeach(@Param("stus") List<Student> stus);
-
StudentMapper.xml <select id="getStudentByForeach" resultType="day01.domain.Student">
select id,studentName name,gender from tbl_student where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
<insert id="insertStudentByForeach">
insert into tbl_student(studentName,gender,classId)
values
<foreach collection="stus" item="stu" separator=",">
(#{stu.name},#{stu.gender},#{stu.clazz.id})
</foreach>
</insert>
7、bind标签
-
StudentMapper.java
public List<Student> getStudentBind(Student stu);
-
StudetnMapper.xml <select id="getStudentBind" resultType="day01.domain.Student">
<bind name="_name" value="'_'+name+'%'"/>
select id,studentName name,gender from tbl_student where studentName like #{_name}
</select>
8、sql标签
-
sql标签示例
<sql id="insertColumn">
<if test="_databaseId=='oracle'">
employee_id,last_name,email
</if>
<if test="_databaseId=='mysql'">
last_name,email,gender,d_id
</if>
</sql>
-
引用sql标签示例 include标签
<include refid="insertColumn">
<property name="testColomn" value="abc"/>
</include>
|