MyBatis框架实现模糊分页以及动态操作

MyBatis框架实现模糊分页以及动态操作

1、模糊查询

mapper接口

//模糊查询
    List<Emp> selectLike1(String name);
    List<Emp> selectLike2(Emp emp);
    List<Emp> selectLike3(Emp emp);
    List<Emp> selectLike4(Map map);

映射文件

<!--模糊查询-->
<!-- 1. 如果说参数类型是普通类型,那么在执行模糊查询的时候,只能使用#{}进行拼接查询 -->
    <select id="selectLike1" parameterType="string" resultType="Emp">
        select * from emp where name like "%"#{name}"%"
        <!--concat("%",#{name},"%")-->
        <!--'%${value}%'-->
    </select>
<!-- 2.参数必须是实体类类型,sql语句中参数名称不可以随意起,必须是实体类的属性名称 -->
    <select id="selectLike2" parameterType="emp" resultType="Emp">
        select * from emp where name like "%"#{name}"%"
    </select>
<!-- 3.${}形式 -->
    <select id="selectLike3" parameterType="emp" resultType="Emp">
        select * from emp where name like '%${name}%'
    </select>
<!--4.如果参数类型是map类型,那么sql语句中参数值一定是map集合中的key值 -->
    <select id="selectLike4" parameterType="map" resultType="Emp">
        select * from emp where name like "%"#{aa}"%"
    </select>
<!-- #{}和${}的区别
采用#{}形式赋值,采用是占位符形式?,防止sql注入攻击
采用${}形式赋值,直接赋值,容易造成sql注入攻击 
-->

测试类

//模糊查询
    @Test
    public void selectLke1() {
        List<Emp> emps = session.getMapper(EmpMapper.class).selectLike1("关");
        System.out.println(emps);
    }
    @Test
    public void selectLke2() {
        Emp emp = new Emp();
        emp.setName("关");
        List<Emp> emps = session.getMapper(EmpMapper.class).selectLike2(emp);
        System.out.println(emps);
    }
    @Test
    public void selectLke3() {
        Emp emp = new Emp();
        emp.setName("关");
        List<Emp> emps = session.getMapper(EmpMapper.class).selectLike3(emp);
        System.out.println(emps);
    }
    @Test
    public void selectLke4() {
        HashMap map = new HashMap();
        map.put("name","关");
        List<Emp> emps = session.getMapper(EmpMapper.class).selectLike4(map);
        System.out.println(emps);
    }
2、分页,分页模糊查询

mapper接口

//分页
    List<Emp> selectPage(Map map);
    //模糊分页
    List<Emp> selectLikePage(Map map);

映射文件

<!--分页查询
	分页查询的时候参数类型必须是map类型,那么sql语句中limit后面的两个参数必须和map集合中的k值相对应-->
    <select id="selectPage" parameterType="map" resultType="emp">
        select * from emp limit #{start},#{size}
    </select>
    <!--分页模糊查询
	如果说参数类型是map类型,那么sql语句中的所有的参数名称必须和map集合中的k值相对应-->
    <select id="selectLikePage" parameterType="map" resultType="emp">
        select * from emp where age >= #{age} limit #{start},#{size}
    </select>

测试类

//分页
    @Test
    public void selectPage() {
        HashMap map = new HashMap();
        map.put("start",0);
        map.put("size",3);
        List<Emp> emps = session.getMapper(EmpMapper.class).selectPage(map);
        System.out.println(emps);
    }
    //模糊分页
    @Test
    public void selectLikePage() {
        HashMap map = new HashMap();
        map.put("age",30);
        map.put("start",0);
        map.put("size",3);
        List<Emp> emps = session.getMapper(EmpMapper.class).selectLikePage(map);
        System.out.println(emps);
    }
/*如果使用两个参数的话,会报以下异常;加入分页查询的时候,参数不可以是多个参数类型,只能使用
map类型*/

3、动态新增

mapper接口

//动态新增
    int insertActive(Emp emp);

映射文件

<!--动态新增
    if标签表示判断,如果符合条件,则执行条件内容
    trim表示去掉多余的指定的字符,prefix表示前缀,suffix表示后缀,suffixOverrides去除字	 段之后的指定字符
    prefixOverrides去除字段之前的指定字符
-->
    <insert id="insertActive" parameterType="emp" >
        <trim prefix="insert into emp(" suffix=")" suffixOverrides=",">
            <if test="name!=null">
                name,
            </if>
            <if test="age!=null">
                age,
            </if>
            <if test="sex!=null">
                sex,
            </if>
            <if test="salary!=null">
                salary,
            </if>
            <if test="bonus!=null">
                bonus,
            </if>
            <if test="birth!=null">
                birth,
            </if>
            <if test="hiredate!=null">
                hiredate,
            </if>
            <if test="leader!=null">
                leader,
            </if>
            <if test="deptid!=null">
                deptid,
            </if>
        </trim>
        <trim prefix="values(" suffix=")" suffixOverrides=",">
            <if test="name!=null">
                #{name},
            </if>
            <if test="age!=null">
                #{age},
            </if>
            <if test="sex!=null">
                #{sex},
            </if>
            <if test="salary!=null">
                #{salary},
            </if>
            <if test="bonus!=null">
                #{bonus},
            </if>
            <if test="birth!=null">
                #{birth},
            </if>
            <if test="hiredate!=null">
                #{hiredate},
            </if>
            <if test="leader!=null">
                #{leader},
            </if>
            <if test="deptid!=null">
                #{deptid},
            </if>
        </trim>
    </insert>

测试类

@Test//动态新增    public void insertActive() {        Emp emp = new Emp();        emp.setName("飞飞");        emp.setBonus(2500.0);        emp.setAge(25);        emp.setSex("男");        int i = session.getMapper(EmpMapper.class).insertActive(emp);        System.out.println(i);    }
4、动态修改

mapper接口

//动态修改
    int updateActive(Emp emp);

映射文件

<!--动态修改
set标签专用于修改,特性就是会自动去除掉最后一个条件的之后的逗号-->
    <update id="updateActive" parameterType="emp">
        update emp
        <set>
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="age!=null">
                age=#{age},
            </if>
            <if test="sex!=null">
                sex=#{sex},
            </if>
            <if test="salary!=null">
                salary=#{salary},
            </if>
            <if test="bonus!=null">
                bonus=#{bonus},
            </if>
            <if test="birth!=null">
                birth=#{birth},
            </if>
            <if test="hiredate!=null">
                hiredate=#{hiredate},
            </if>
            <if test="leader!=null">
                leader=#{leader},
            </if>
            <if test="deptid!=null">
                deptid=#{deptid},
            </if>
        </set>
        where id=#{id}
    </update>
<!--也可用trim标签替代set标签,将set标签作为前缀,去掉末尾逗号-->

测试类

@Test//动态修改
    public void updateActive() {
        Emp emp = new Emp();
        emp.setName("小伟");
        emp.setBonus(2500.00);
        emp.setAge(25);
        emp.setId(49);
        int i = session.getMapper(EmpMapper.class).updateActive(emp);
        System.out.println(i);
    }
5、动态查询where标签

mapper接口

//动态查询
    List<Emp> selectActive(Emp emp);

映射文件

<!--动态查询-->
    <select id="selectActive" parameterType="emp" resultType="emp">
        <trim prefix="select * from emp where" prefixOverrides="and">
            <if test="name!=null">
                and name like "%"#{name}"%"
            </if>
            <if test="age!=null">
                and age >= #{age}
            </if>
        </trim>
    </select>
    <!--where标签表示条件连接符
    特性:如果一个条件成立,那么where标签会自动把and关键字去掉,
    若语句的开头为“AND”或“OR”,where 元素也会将它们去除。
    如果所有条件都不成立,那么where会启动自毁程序,把自己干死-->
    <select id="selectActive" parameterType="emp" resultType="emp">
      select * from emp
    <where>
        <if test="name!=null">
            and name like "%"#{name}"%"
        </if>
        <if test="age!=null">
            and age >= #{age}
        </if>
    </where>
    </select>

测试类

 @Test//动态查询    public void selectActive() {        Emp emp = new Emp();        emp.setName("小");        List<Emp> emps = session.getMapper(EmpMapper.class).selectActive(emp);        System.out.println(emps);    }
6、批量查询,批量删除(forEach 标签)

mapper接口

//批量删除
    List<Emp> selectBatch(List ids);
    //批量删除
    int deleteBatch(Integer[] ids);

映射文件

<!--foreach标签表示循环标签,collection表示集合属性,属性值有两种,
如果接口的参数是List类型,那么该属性值=list也可以是collection
如果接口的参数是数组类型,那么该属性值=array
open属性表示类似于前缀
close表示类似于后缀
item表示集合的遍历体,属性值随意起
separator表示隔离间隔的关键字属性,
sql:SELECT * FROM product WHERE id IN(47,46,1,10)
foreach标签之间展示的每次遍历的id值,表达形式#{item属性值}-->

<!--批量查询-->
    <select id="selectBatch" parameterType="list" resultType="emp">
        select * from emp
        <where>
        id in
            <foreach collection="list" open="(" separator="," close=")" item="ids">
                #{ids}
            </foreach>
        </where>
    </select>
<!--批量删除-->
    <delete id="deleteBatch" parameterType="int[]">
        delete from emp
        <where>
            id in
            <foreach collection="array" open="(" separator="," close=")" item="ids">
                #{ids}
            </foreach>
        </where>
    </delete>

测试类

@Test//批量查询
    public void selectBatch() {
        ArrayList<Object> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(6);
        list.add(4);
        list.add(5);
        List<Emp> emps = session.getMapper(EmpMapper.class).selectBatch(list);
        System.out.println(emps);
    }
    @Test//批量删除
    public void deleteBatch() {
        Integer []ids={40,49,50,61};
        int i = session.getMapper(EmpMapper.class).deleteBatch(ids);
        System.out.println(i);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值