mybatis的动态sql和分页

目标
1、mybatis动态sql
2、模糊查询
3、查询返回结果集的处理
4、分页查询
5、特殊字符处理

mybatis动态sql
If、trim、foreach

<select id="selectByIn" resultType="com.ly.model.Student" parameterType="java.util.List">
    select * from tb_stu where sid in 
    <foreach collection="sids" open="(" close=")" separator="," item="sid">
      #{sid}
    </foreach>
  </select>
List<Student> selectByIn(@Param("sids") List sids);

在这里插入图片描述
模糊查询

#{...}

${...}

Concat

注意:#{…}自带引号,${…}有sql注入的风险

<select id="selectBylike1" resultType="com.ly.model.Student" parameterType="java.lang.String">
    select * from tb_stu where sname like #{sname}
  </select>
  <select id="selectBylike2" resultType="com.ly.model.Student" parameterType="java.lang.String">
    select * from tb_stu where sname like '${sname}'
  </select>
  <select id="selectBylike3" resultType="com.ly.model.Student" parameterType="java.lang.String">
    select * from tb_stu where sname like concat(concat('%',#{sname}),'%')
  </select>
List<Student> selectBylike1(@Param("sname") String sname);

    List<Student> selectBylike2(@Param("sname") String sname);

    List<Student> selectBylike3(@Param("sname") String sname);

在这里插入图片描述
查询返回结果集的处理
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型
1 使用resultMap返回自定义类型集合
2 使用resultType返回List
3 使用resultType返回单个对象
4 使用resultType返回List,适用于多表查询返回结果集
5 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集

<select id="list1" resultMap="BaseResultMap">
    select * from tb_stu
  </select>
  <select id="list2" resultType="com.ly.model.Student">
    select * from tb_stu
  </select>
  <select id="list3" resultType="com.ly.model.Student" parameterType="com.ly.model.StudentVo">
    select * from tb_stu where sid in
    <foreach collection="sids" open="(" close=")" separator="," item="sid">
      #{sid}
    </foreach>
  </select>
  <select id="list4" resultType="java.util.Map" parameterType="java.util.Map">
    select * from tb_stu
    <where>
      <if test="null != sname and sname != ''">
        and sname like #{sname}
      </if>
    </where>
  </select>
  <select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
   select * from tb_stu
    <where>
      <if test="null != sid and sid != ''">
        and sid = #{sid}
      </if>
    </where>
  </select>

测试代码

@Test
    public void list() {
        //返回一个resultmap但是使用list<T>
//        List<Student> stus = this.stuService.list1();

        //返回一个resulttype但是使用list<T>接收
//        List<Student> stus = this.stuService.list2();
//        for (Student stu : stus) {
//            System.out.println(stu);
//        }

        //返回一个resulttype但是使用T接收
        /*StudentVo studentVo = new StudentVo();
        List list = new ArrayList();
        list.add(1);
        studentVo.setSids(list);
        Student student = this.stuService.list3(studentVo);
        System.out.println(student);*/

        //返回的是resulttype,然后用list<Mao>进行接收
        Map map = new HashMap();
        /*map.put("sname",StringUtils.toLikeStr("宜章"));
        List<Map> list = this.stuService.list4(map);
        for (Map m : list) {
            System.out.println(m);
        }*/

        //返回的是resulttype,然后用Map进行接收
        map.put("sid",1);
        Map m = this.stuService.list5(map);
        System.out.println(m);
    }

分页查询
为什么要重写mybatis的分页?
Mybatis的分页功能很弱,它是基于内存的分页(查出所有记录再按偏移offset和边界limit取结果),在大数据量的情况下这样的分页基本上是没有用的
使用分页插件步奏
1、导入pom依赖
2、Mybatis.cfg.xml配置拦截器
3、使用PageHelper进行分页
4、处理分页结果
Pom依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.2</version>
</dependency>

Mybatis.cfg.xml配置拦截器

<plugins>
    <!-- 配置分页插件PageHelper, 4.0.0以后的版本支持自动识别使用的数据库 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
    </plugin>
</plugins>

使用分页插件

<select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
  select * from tb_stu where sname like concat(concat('%',#{sname}),'%')
</select>

Mapper层

List<Map> listPager(Map map);

Service层

List<Map> listPager(Map map, PageBean pageBean);
@Override
    public List<Map> listPager(Map map, PageBean pageBean) {
        if(pageBean != null && pageBean.isPagination()){
            PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
        }
        List<Map> list = this.studentMapper.list4(map);
        if(pageBean != null && pageBean.isPagination()){
            PageInfo pageInfo = new PageInfo(list);
            System.out.println("当前页码"+pageInfo.getPageNum());
            System.out.println("当前数据量"+pageInfo.getPageSize());
            System.out.println("符合条件的记录数"+pageInfo.getTotal());
            pageBean.setTotal(pageInfo.getTotal()+"");
        }
        return list;
    }

测试代码

@Test
    public void listPager() {
        Map map = new HashMap();
        map.put("sname",StringUtils.toLikeStr("宜章"));
        PageBean pageBean = new PageBean();
        List<Map> list = this.stuService.listPager(map, pageBean);
        for (Map m : list) {
            System.out.println(m);
        }
    }

在这里插入图片描述
特殊字符处理

>(&gt;)   
    <(&lt;)  
    &(&amp;) 
 空格(&nbsp;)
 <![CDATA[ <= ]]> 
package com.ly.model;

import java.util.List;

/**
 * @author 毅哥哥
 * @site
 * @company
 * @create 2019-09-21 12:47
 * vo类用来存放包括数据库表映射字段以及多余查询条件所需属性
 */
public class StudentVo {
    private List<String> sids;
    private float min;
    private float max;

    public float getMin() {
        return min;
    }

    public void setMin(float min) {
        this.min = min;
    }

    public float getMax() {
        return max;
    }

    public void setMax(float max) {
        this.max = max;
    }

    public List<String> getSids() {
        return sids;
    }

    public void setSids(List<String> sids) {
        this.sids = sids;
    }
}

<select id="list6" resultType="java.util.Map" parameterType="com.ly.model.StudentVo">
    select * from tb_stu
    <where>
      <if test="null != min and min != ''">
        and sid &gt; #{min}
      </if>
      <if test="null != max and max != ''">
        and sid &lt; #{max}
      </if>
    </where>
  </select>
  <select id="list7" resultType="java.util.Map" parameterType="com.ly.model.StudentVo">
    select * from tb_stu
    <where>
      <if test="null != min and min != ''">
        <![CDATA[  and sid > #{min} ]]>
      </if>
      <if test="null != max and max != ''">
        <![CDATA[  and sid < #{max} ]]>
      </if>
    </where>
  </select>
//    处理特殊字符的方式
    List<Map> list6(StudentVo studentVo);

    List<Map> list7(StudentVo studentVo);
 @Test
    public void sqlSpecical() {
        StudentVo studentVo = new StudentVo();
        studentVo.setMax(6);
        studentVo.setMin(2);
//        List<Map> list = this.stuService.list6(studentVo);
        List<Map> list = this.stuService.list7(studentVo);
        for (Map map : list) {
            System.out.println(map);
        }
    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值