mybatis动态sql和分页

1、mybatis动态sql

1 foreach
遍历集合,批量查询、通常用于in关键字

 <!--foreach-->
  <select id="selectBooksIn" resultType="com.houyitao.model.Book" parameterType="java.util.List">
    select * from t_mvc_book where bid in
    <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
      #{bid}
    </foreach>
  </select>

在这里插入图片描述
2、模糊查询
#{…}
${…}
Concat
2.1 参数中直接加入%%

2.2 使用${…}代替#{…}(不建议使用该方式,有SQL注入风险)

关键:#{…}与KaTeX parse error: Expected 'EOF', got '#' at position 29: … 参数类型为字符串,#̲会在前后加单引号['],则直接插入值
注:
1) mybatis中使用OGNL表达式传递参数
2) 优先使用#{…}
3) ${…}方式存在SQL注入风险

2.3 SQL字符串拼接CONCAT

 /**
     * mybatis对模糊查询共有三种方式
     * #{}
     * ${}
     * concat
     * @param banme
     * @return
     */
     List<Book> selectBooksLike1(@Param("bname") String banme);
    List<Book> selectBooksLike2(@Param("bname") String banme);
    List<Book> selectBooksLike3(@Param("bname") String banme);

<!--模糊查询-->
  <select id="selectBooksLike1" resultType="com.houyitao.model.Book" parameterType="java.lang.String">
    select * from t_mvc_book where bname like #{bname}
  </select>
  <select id="selectBooksLike2" resultType="com.houyitao.model.Book" parameterType="java.lang.String">
    select * from t_mvc_book where bname like '${bname}'
  </select>
  <select id="selectBooksLike3" resultType="com.houyitao.model.Book" parameterType="java.lang.String">
    select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
  </select>

在这里插入图片描述
3.查询返回结果集
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型
3.1 使用resultMap返回自定义类型集合

3.2 使用resultType返回List

3.3 使用resultType返回单个对象

3.4 使用resultType返回List,适用于多表查询返回结果集

3.5 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集

 /**
     * 结果集处理的五种情况
     * @return
     */
    List<Book> list1();
    List<Book> list2();
    List<Book> list3(BookVo bookVo);
    List<Map> list4(Map map);
    Map list5(Map map);
 <!--查询返回结果集的处理-->
  <select id="list1" resultMap="BaseResultMap">
  select * from t_mvc_book
</select>

  <select id="list2" resultType="com.houyitao.model.Book">
  select * from t_mvc_book
</select>

  <select id="list3" resultType="com.houyitao.model.Book" parameterType="com.houyitao.model.BookVo">
 select * from t_mvc_book where bid in
    <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
    #{bid}
    </foreach>
</select>

  <select id="list4" resultType="java.util.Map" parameterType="java.util.Map">
    select * from t_mvc_book where bid in
    <foreach collection="bookIds" open="(" close=")" separator="," item="bid">
      #{bid}
    </foreach>
  </select>

  <select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
    select * from t_mvc_book where bid =#{bid}
  </select>

BookVo

package com.houyitao.model;

/**
 * @author HYT
 * @site www.hyt.com
 * @company 阿里巴巴
 * @create  2019-10-17 15:49
 */

import java.util.List;

/**
 * vo介绍
 * mybatis,hibernate都是orm框架,表所存在的列段在实体类model都有映射
 * 实际开发中,会因为一些需求改变model,破坏model封装性
 * 此时为了保证model的封装性,就可以使用vo类来完成指定的需求
 *
 */
public class BookVo extends Book{
    private  Float min;
    private  Float max;
    private List<Integer>bookIds;

    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<Integer> getBookIds() {
        return bookIds;
    }

    public void setBookIds(List<Integer> bookIds) {
        this.bookIds = bookIds;
    }

}

在这里插入图片描述

4.分页查询
为什么要重写mybatis的分页?
Mybatis的分页功能很弱,它是基于内存的分页(查出所有记录再按偏移量offset和边界limit取结果),在大数据量的情况下这样的分页基本上是没有用的

4.1 导入分页插件

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

4.2 将pagehelper插件配置到mybatis中

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

使用分页插件

<!--f分页-->
  <select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
   select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')

  </select>

Mapper层

 /**
     * 分页查询
     * @param map
     * @return
     */
    List<Map> listPager(Map map);

Service层

List<Map> listPager(Map map);
@Override
    public List<Map> listPager(Map map, PageBean pageBean) {
if(pageBean!=null&&pageBean.isPagination()){
    PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
}
        List<Map> list = bookMapper.listPager(map);
        if(pageBean!=null&&pageBean.isPagination()){
            PageInfo pageInfo=new PageInfo(list);
            System.out.println("总记录数:" + pageInfo.getTotal());
            System.out.println("当前页:" + pageInfo.getPageNum());
            System.out.println("页大小:" + pageInfo.getPageSize());
            pageBean.setTotal(pageInfo.getTotal()+"");
            System.out.println("总页数:" + pageBean.getMaxPage());
        }
        return list;
    }

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

 >(&gt;)   
    <(&lt;)  
    &(&amp;) 
 空格(&nbsp;)
 
 <![CDATA[ <= ]]> 
<!--特殊字符处理-->
  <select id="list6" resultType="com.houyitao.model.Book" parameterType="com.houyitao.model.BookVo">
    select * from t_mvc_book where price &gt; #{min} and price &lt; #{max}
       <!--select * from t_mvc_book where  <![CDATA[ price > #{min} and price < #{max} ]]>-->
  </select>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值