四、动态sql
作者:互联网
四、动态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
/** * 使用trim标签查询学生信息 */ public List<Student> getStudentByTrim(Student stu);
-
StudentMapper.xml
<!-- trim : 用来自定义where标签或set标签的功能--> <!-- prefix : 会在拼接完成的sql语句前面加上某一字段--> <!-- prefixOverrides : 会在拼接完成的sql语句前面去掉某一字段--> <!-- suffix : 会在拼接完成的sql语句后面加上某一字段--> <!-- suffixOverrides : 会在拼接完成的sql语句后面删除某一字段--> <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
/** * 测试choose标签,类似于加上break关键字的switch-case语句 */ 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
/** * 测试set标签 */ 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
/** * 测试foreach标签 */ public List<Student> getStudentByForeach(@Param("ids") List<Integer> ids); /** * 使用foreach标签完成批量添加 */ 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> <!-- public Integer insertStudentByForeach(@Param("stus") List<Student> stus);--> <!-- 方式一: MySql支持 insert into table() values(),()--> <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> <!-- 方式二: 需要开启Mysql的一个数据库连接属性allowMultiQueries 看 jdbc.properties文件 --> <!-- 这种使用;分割的不仅可以使用批量添加,同样也可以使用其他批量操作(删除,修改)--> <!-- <insert id="insertStudentByForeach">--> <!-- <foreach collection="stus" item="stu" separator=";">--> <!-- insert into tbl_student(studentName,gender,classId)--> <!-- values(#{stu.name},#{stu.gender},#{stu.clazz.id})--> <!-- </foreach>--> <!-- </insert>-->
7、bind标签
-
StudentMapper.java
/** * 测试bind标签,用来转换属性,具体看案例 */ 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片段。方便后面引用 1、sql抽取:经常将要查询的列名,或者插入用的列名抽取出来方便引用 2、include来引用已经抽取的sql: 3、include还可以自定义一些property,sql标签内部就能使用自定义的属性 include-property:取值的正确方式${prop}, #{不能使用这种方式} --> <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标签
<!-- 引用外部定义的sql --> <include refid="insertColumn"> <!--可以自定义一个列,弥补sql标签不足的列--> <property name="testColomn" value="abc"/> </include>
标签:StudentMapper,name,gender,sql,studentName,标签,动态,id 来源: https://blog.csdn.net/weixin_46203713/article/details/120791341