mybatis-动态sql和分页

mybatis动态sql

If
用途:添加条件
trim
用途:去空格
foreach
代码演示
BookMapper
返回对象

 List<Book> selectBooksIn(@Param("bookIds")List bookIds);

BookMapper.xml
select标签
parameterType:指定参数
foreach标签:
collection:变量
item:指针
open:左开区间
close:右开区间
separator:逗号隔开

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

测试

 @Override
    public List<Book> selectBooksIn(List bookIds) {
        
        return bookMapper.selectBooksIn(bookIds);
    }
@Test
    public void selectBooksIn() {
        List bookIds=new ArrayList();
        bookIds.add(16);
        bookIds.add(19);
        bookIds.add(20);
        for (Book book : this.bookService.selectBooksIn(bookIds)) {
            System.out.println(book);
        }
    }

在这里插入图片描述

模糊查询

三种形式
#{…}:自带引号 一般选择
${…}:有sql注入的风险 动态列选择
Concat:使用拼接
代码演示
BookMapper

List<Book> selectBooksLike1(@Param("bname")String bname);
    List<Book> selectBooksLike2(@Param("bname")String bname);
    List<Book> selectBooksLike3(@Param("bname")String bname);

BookMapper.xml

<!-- /*模糊查询*/-->
  <select id="selectBooksLike1" resultType="com.zxp.model.Book" parameterType="java.lang.String">
select * from t_mvc_book
<where>
  bname like #{bname}
</where>
  </select>

<select id="selectBooksLike2" resultType="com.zxp.model.Book" parameterType="java.lang.String">
select * from t_mvc_book
<where>
  bname like '${bname}'
</where>
</select>

  <select id="selectBooksLike3" resultType="com.zxp.model.Book" parameterType="java.lang.String">
    select * from t_mvc_book
    <where>
      bname like concat('%',#{bname},'%')
    </where>
  </select>

test

 public void selectBooksLike() {
//        for (Book book : this.bookService.selectBooksLike1("%圣墟%")) {
//            System.out.println(book);
//        }
//        for (Book book : this.bookService.selectBooksLike2("%圣墟%")) {
//            System.out.println(book);
//        }
        for (Book book : this.bookService.selectBooksLike3("圣墟")) {
            System.out.println(book);
        }

图片演示

#{…}:
在这里插入图片描述
${…}:
在这里插入图片描述
Concat:
在这里插入图片描述

查询返回结果集的处理

resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型
五种情况
1 使用resultMap返回自定义类型集合

2 使用resultType返回List

3 使用resultType返回单个对象

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

5 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
代码演示

BookMapper

//    查询返回结果集的处理

    /**
     * 使用resultMap返回自定义类型集合
     * @return
     */
    List<Book> list1();

    /**
     * 使用resultType返回List<T>
     * @return
     */
    List<Book> list2();
    /**
     * 使用resultType返回单个对象
     * @return
     */
    List<Book> list3(BookVo bookVo);
    /**
     * 使用resultType返回List<Map>,适用于多表查询返回结果集
     * @return
     */
    List<Map> list4();

    /**
     * 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
     * @return
     */
    Map list5(Map map);

BookMapper.xml

<!--查询返回结果集的处理-->
  <select id="list1" resultMap="BaseResultMap">
  select * from t_mvc_book
</select>
  <select id="list2" resultType="com.zxp.model.Book">
  select * from t_mvc_book
</select>

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

  <select id="list4" resultType="java.util.Map">
  select * from t_mvc_book
</select>

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

test

  @Test
    public void list1() {
        for (Book book : this.bookService.list1()) {
            System.out.println(book);
        }
    }
    @Test
    public void list2() {
        for (Book book : this.bookService.list2()) {
            System.out.println(book);
        }
    }
    @Test
    public void list3() {
        BookVo bookVo=new BookVo();
        List bookIds=new ArrayList();
        bookIds.add(16);
        bookIds.add(19);
        bookIds.add(20);
        bookVo.setBookIds(bookIds);
        for (Book book : this.bookService.list3(bookVo)) {
            System.out.println(book);
        }
    }
    @Test
    public void list4() {
        for (Map map : this.bookService.list4()) {
            System.out.println(map);
        }
    }
    @Test
    public void list5() {
        Map map=new HashMap();
        map.put("bid",20);
        System.out.println(this.bookService.list5(map));
    }

图片演示

1
在这里插入图片描述
2
在这里插入图片描述
3
在这里插入图片描述
4
在这里插入图片描述
5
在这里插入图片描述

分页查询

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

使用分页插件步奏
1、导入pom依赖

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

2、Mybatis.cfg.xml配置拦截器

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

3、使用PageHelper进行分页
BookMapper

List<Map> listPage(Map map, PageBean pageBean);

BookMapper.xml

<!--分页-->
  <select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
    select * from t_mvc_book
    <where>
      <if test="bname !=null and bname!='' ">
        bname like #{bname}
      </if>
    </where>
  </select>

4、处理分页结果
test

 @Test
    public void listPage() {
        Map map=new HashMap();
        map.put("bname","%圣墟%");
        PageBean pageBean=new PageBean();
        pageBean.setPagination(false);
        pageBean.setPage(3);
        for (Map m : this.bookService.listPage(map, pageBean)) {
            System.out.println(m);
        }
    }

图片演示
在这里插入图片描述

特殊字符处理

>(>) <(<) &(&) 空格( ) <![CDATA[ <= ]]>
举例 >(>) <(<) <![CDATA[ <= ]]>
BookMapper.xml

 <select id="list6" resultType="com.zxp.model.Book" parameterType="com.zxp.model.BookVo">

    <!--select * from t_mvc_book where   <![CDATA[ price >#{min} and price <#{max}  ]]>-->
    
    select * from t_mvc_book where   price &gt; #{min} and price &lt; #{max}
  </select>

test

@Test
    public void list6() {
        BookVo bookVo=new BookVo();
        bookVo.setMax(20f);
        bookVo.setMin(15f);
        for (Book book : this.bookService.list6(bookVo)) {
            System.out.println(book);
        }
    }

图片演示
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值