mybatis之动态SQL

来源:互联网 发布:苹果字体软件 编辑:程序博客网 时间:2024/05/22 00:54

mybatis动态SQL的作用

不错,就是为了减少写代码,体现mybatis的可维护性和高度的灵活性。不用像jdbc那样拼装sql。

mybatis动态SQL包括

这里写图片描述

if元素

if 元素常常和test搭配使用
这儿使用的数据库表是tb_person,在PersonMapper.xml中代码如下

<select id="findName" parameterType="string" resultType="com.stumybatis.pojo.Person">        select * from tb_person where 1=1        <if test="name!=null and name!=''">            and name like concat ('%',#{name},'%')        </if>    </select>

测试代码若为

  @Test    public void findName() throws Exception {        Person person = dao.findName("三");        System.out.println(person);    }

就会找到张三的这条记录(我添加张三这个记录在person表原有基础上),若是没有“三”这个模糊查询,这就不构造if分支匹配。

choose,when,otherwise元素

下面这个语句可以理解为,当角色姓名不为空,就单单用角色姓名去查;当角色姓名为空,那么就用角色的性别去查,而角色的性别不为空的时候,就用角色性别去做模糊查询,但是两个都为空的时候,就去找card_id不为空的情况,

   <select id="findPerson" parameterType="com.stumybatis.pojo.Person"            resultType="com.stumybatis.pojo.Person">        select * from tb_person where 1=1        <choose>            <when test="name!=null and name !=''">                and name like concat ('%',#{name},'%')            </when>            <when test="sex !=null and sex !=''">                and sex concat (#{sex},'%')            </when>            <otherwise>                and card_id is not null            </otherwise>        </choose>    </select>

测试代码

    public void findPerson() throws Exception{        Person person = new Person();        person.setName("三");        person.setSex("man");        person = dao.findPerson(person);        System.out.print(person);    }

这里如果不写 person.setSex(“man”);查出来的结果也是一样,同样不写person.setName(“三”);结果也一样,就是看你接口怎么写了

 public Person findPerson(Person person);

要是这么写,光用setSex查就要错,好像有两条数据都是man,最好返回返回集和,容错率大。

trim,where,set

where 这个比较好玩,就是where里面的条件成立的时候才去执行where,但是若是不成立的话,那么就不用去执行where的部分。这个功能简直用起来要不要辣么顺手。

   <select id="findName2" parameterType="string" resultType="com.stumybatis.pojo.Person">        select * from tb_person        <where>            <if test="name!=null and name!=''">                and name like concat ('%',#{name},'%')            </if>        </where>    </select>

这个代码的功能和最开始举例的那个if的查询是一个功能。


trim元素的功能就是
表示去掉元素比如prefix表示的是语句的前缀,prefixOverrides表示去掉的那种字符串,下面这个就是第三种实现

 <select id="findName3" parameterType="string" resultType="com.stumybatis.pojo.Person">        select * from tb_person          <trim prefix="where" prefixOverrides="and">              and name like concat ('%',#{name},'%')          </trim>    </select>

set的用法

动态更新语句一般用set,set元素会自动前置set关键词,同时也消除无关的逗号,具体的映射代码为

 <!--根据ID查找person对象--> <select id="selectPersonById" parameterType="int" resultMap="personCard">        select * from tb_person where id = #{id} </select> <!--根据查找到的ID去更新此ID对象--> <update id="updateEmployeeNecessary" parameterType="com.stumybatis.pojo.Person">        UPDATE tb_person        <set>            <if test="name!=null">name=#{name},</if>            <if test="sex !=null">sex=#{sex},</if>            <if test="age !=null">age=#{age}</if>        </set>        WHERE  id=#{id}</update>
public Person selectPersonById(Integer id);public void updateEmployeeNecessary(Person person);

测试代码

 @Test    public void updateEmployeeNecessary() throws Exception{        Person person = dao.selectPersonById(2);        System.out.println(person);        System.out.println("---------------------------");        person.setName("张三");        person.setSex("women");        person.setAge(20);        dao.updateEmployeeNecessary(person);        System.out.println(person);    }

打印的结果如图
这里写图片描述

foreach元素

作用就是遍历集合
foreach中每个元素的作用
connection配置的是list是传过来的参数List list,list对应一致,这个一般就是数组或者List、Set等集合
item配置的是循环当前的元素,也就是ROM中表述的sex
index配置的是当前的元素 在集合的位置下标。。表示不知道怎么用
open和close配置的是查询的结果用什么符号将他们包装起来
separator是各个元素的间隔符,最后附上一张截图,上面的结果明确表示。

  <select id="findUserBySex" resultType="com.stumybatis.pojo.Person">       select * from tb_person where sex in       <foreach collection="list" item="sex"  index="index" open="(" separator="," close=")">           #{sex}       </foreach>   </select>

接口中的声明

  public List<Person> findUserBySex(List<String> list);

测试代码

   @Test    public void findUserBySex() throws  Exception{        List<String> sexlist = Arrays.asList("man", "woman");        List<Person> sexList = dao.findUserBySex(sexlist);        System.out.println(sexList);    }

测试截图