MyBatis笔记(二)动态sql

    科技2022-08-14  90

    MyBatis笔记二

    动态sql:一. < if >< where >1. sql处理 👇2. 标签处理 二. < foreach >1. 输入属性(Grade类:List ids)2. 简单数组3. 集合4. 对象数组5. MyBatis:

    动态sql:

    简单来说就是sql的拼接

    一. < if >< where >

    1. sql处理 👇

    作用:拼接Sql语句,person有name属性 且 不为null 注意所有 SQL字段大小写忽略,但是 实体类属性区分大小写 第二个拼接语句 开头 需要加 and

    <!-- 动态SQL --> <!-- person有name属性 且 不为null --> <!-- 注意所有SQL字段大小写忽略,但是实体类属性区分大小写 --> <!-- 第二个拼接语句 开头 需要加 and --> <select id="queryPersonByNameOrSexWithSqlTag" resultType="mybatis.Person" parameterType="mybatis.Person"> select id,name from person where 1=1 <if test="name != null and name != '' "> and name = #{name} </if> <if test="sex != null and sex != '' "> and sex = #{sex} </if> </select>

    2. 标签处理

    <!-- <where> 自动处理 第一个<if>标签中的and 之后的不管 --> <select id="queryPersonByNameOrSexWithSqlTag" resultType="mybatis.Person" parameterType="mybatis.Person"> select id,name from person <where> <if test="name != null and name != '' "> and name = #{name} </if> <if test="sex != null and sex != '' "> and sex = #{sex} </if> </where> </select>

    二. < foreach >

    查询学号为1,2,3的学生信息 输入一个集合 ids = {1,2,3} 迭代的类型:数组、对象数组、集合、属性(Grade类:List ids)

    1. 输入属性(Grade类:List ids)

    mapper.xml 👇

    <!-- <foreach>输入集合 --> <select id="queryPersonWithIdsInGrade" parameterType="mybatis.Grade" resultType="person"> select * from person <where> <if test="ids!=null and ids.size>0"> <foreach collection="ids" open="and id in (" close=")" item="id" separator=","> #{id} </foreach> </if> </where> </select>

    测试 👇

    List<Integer> ids = new ArrayList<>(); ids.add(1); ids.add(2); ids.add(3); Grade grade = new Grade(ids); System.out.println(queryPersonWithIdsInGrade(grade));

    2. 简单数组

    无论在编写时,用的什么参数名,在mapper.xml中 必须 要用 array 代替数组名

    mapper.xml 👇

    <!-- <foreach>输入数组 int[] --> <select id="queryPersonWithIdsInArr" parameterType="int[]" resultType="person"> select * from person <where> <if test="array!=null and array.length>0"> <foreach collection="array" open="and id in (" close=")" item="id" separator=","> #{id} </foreach> </if> </where> </select>

    3. 集合

    无论在编写时,用的什么参数名,在mapper.xml中 必须 要用 list 代替集合 名

    mapper.xml 👇

    <!-- <foreach>输入集合 list 必须用list代替数组名 --> <select id="queryPersonWithIdsInList" parameterType="list" resultType="person"> select * from person <where> <if test="list!=null and list.size>0"> <foreach collection="list" open="and id in (" close=")" item="id" separator=","> #{id} </foreach> </if> </where> </select>

    4. 对象数组

    对象数组的输入要用Object [ ]

    mapper.xml 👇

    <!-- <foreach>输入对象数组 Person [] person = {p1,p2,p3} --> <select id="queryPersonWithIdsInObjArray" parameterType="Object[]" resultType="person"> select * from person <where> <if test="array!=null and array.length>0"> <foreach collection="array" open="and id in (" close=")" item="person" separator=","> #{person.id} </foreach> </if> </where> </select>

    5. MyBatis:

    提取相似的 MyBatis片段 <select id="queryPersonWithIdsInObjArray" parameterType="Object[]" resultType="person"> select * from person <include refid="objectArrayids"></include> </select> <sql id="objectArrayids"> <where> <if test="array!=null and array.length>0"> <foreach collection="array" open="and id in (" close=")" item="person" separator=","> #{person.id} </foreach> </if> </where> </sql> 如果不在同一文件内,id要加前缀id = mybatis.personMapper.objectArrayids“namespace . id”
    Processed: 0.018, SQL: 8