码迷,mamicode.com
首页 > 数据库 > 详细

mybatis动态sql以及分页

时间:2019-09-22 12:30:27      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:页码   HERE   三种方式   and   data   ids   bat   ash   extend   

1.mybatis动态sql

If、trim、foreach

If :如果 name 不为空,就进行if体的拼接

 <if test="bname != null" >
        #{bname,jdbcType=VARCHAR},
      </if>


trim:一样的sql语句拼接:prefix前缀,suffi 后缀。suffixOverrides 后缀覆盖

  <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="name != null" >
        name,
      </if>
      <if test="pwd != null" >
        pwd,
      </if>
    </trim>

 

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

<select id="selectByIn" resultType="com.liuting.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>

 

测试:

 @Test
    public void slectByIn() {
        List list=new ArrayList();
        list.add(2);
        list.add(5);
        list.add(19);
        list.add(27);
        List<Book> books = this.bookService.slectByIn(list);
        for (Book b : books) {
            System.out.println(b);
        }
    }

技术图片

 

模糊查询的三种方式

    List<Book> slectBylike1(@Param("bname") String bname);
    List<Book> slectBylike2(@Param("bname") String bname);
    List<Book> slectBylike3(@Param("bname") String bname);

 

<!--模糊查的三种方式-->
    <select id="slectBylike1" resultType="com.psy.model.Book" parameterType="java.lang.String">
    select * from t_mvc_book where bname like #{bname}
  </select>

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


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

 

测试:

@Test
    public void slectByLike() {

        List<Book> books = this.bookService.slectBylike1(StringUtils.toLikeStr("圣墟"));
        for (Book b : books) {
            System.out.println(b);
        }
    }

 

这里调用的是自己写的一个方法:

public class StringUtils {

    public static String toLikeStr(String str){
        return "%"+str+"%";
    }
}

 

测试结果:

技术图片

 

 

 ${...}方式存在SQL注入风险

 

 

查询返回结果集的处理

BookVo

public class BookVo extends Book{
    private List<String> bookIds;

    public List<String> getBookIds() {
        return bookIds;
    }

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

}

 

//    3.1 使用resultMap返回自定义类型集合
    List<Book> list1();
    //    3.2 使用resultType返回List<T>
    List<Book> list2();
    //    3.3 使用resultType返回单个对象
    Book list3(BookVo bookVo);
    //    3.4 使用resultType返回List<Map>,适用于多表查询返回结果集
    List<Map> list4(Map map);
    //   3.5 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
    Map list5(Map map);

 

BookMapper.xml

 

 <select id="list1" resultType="com.psy.model.Book">
    select * from t_mvc_book
  </select>

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

    <select id="list3" resultType="com.psy.model.Book" parameterType="com.psy.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>
            <if test="null != bname and bname != ‘‘">
                and bname like #{bname}
            </if>

        </where>
    </select>
    <select id="list5" resultType="java.util.Map" parameterType="java.util.Map">

        select * from t_mvc_book
        <where>
            <if test="null != bid and bid != ‘‘">
                and bid like #{bid}
            </if>

        </where>

    </select>

 

 

测试:

@Test
    public void List() {
        //返回resultMap但是使用list<T>接收
        /*List<Book> books = this.bookService.list1();*/
        //返回的是resulttype使用list<T>
        /* List<Book> books = this.bookService.list2();
         */
       /* for (Book book : books) {
            System.out.println(book);
        }*/

        //返回的是resulttype使用T接收
//        BookVo bookVo = new BookVo();
//        List list = new ArrayList();
//        list.add(27);
//        bookVo.setBookIds(list);
//       Book book =this.bookService.list3(bookVo);
//        System.out.println(book);

        //返回的是resultType,然后用list<Map>进行接收
        Map map = new HashMap();
//        map.put("bname", StringUtil.toLikeStr("圣墟"));
//        List<Map> list= this.bookService.list4(map);
//        for (Map m : list) {
//            System.out.println(m);
//        }

        //返回单个Map
        map.put("bid",27);
        Map m = this.bookService.list5(map);
        System.out.println(m);
    }

 

分页查询

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进行分页

@Override
    public List<Map> listPager(Map map, PageBean pageBean) {
        //如果分页对象不为空就继续分页操作
        if(pageBean != null && pageBean.isPagination()){
            PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
        }
        List<Map> list  = this.userMapper.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());
        }
        return list;
    }


4、处理分页结果

测试

@Test
    public void ListPager() {
        Map map = new HashMap();
       map.put("bname", StringUtil.toLikeStr("圣墟"));
        PageBean pageBean = new PageBean();
        List<Map> list = this.bookService.listPager(map,pageBean);
        for (Map m : list) {
            System.out.println(m);
        }

    }

 

特殊字符处理

>(&gt;) 
<(&lt;) 
&(&amp;) 
空格(&nbsp;)
<![CDATA[ <= ]]>

/处理特殊字符的方式
    List<Map> list6(BookVo bookVo);
    List<Map> list7(BookVo bookVo);
<!--处理特殊字符-->
  <select id="list6" resultType="java.util.Map" parameterType="com.psy.model.BookVo">
    select * from t_mvc_book
    <where>
      <if test="null != min and min != ‘‘">
        and price &gt; #{min}
      </if>
      <if test="null != max and max != ‘‘">
        and price &lt; #{max}
      </if>

    </where>
  </select>
  <select id="list7" resultType="java.util.Map" parameterType="com.psy.model.BookVo">
    select * from t_mvc_book
    <where>
      <if test="null != min and min != ‘‘">
        <![CDATA[ and price > #{min} ]]>
      </if>
      <if test="null != max and max !=‘‘">
      <![CDATA[ and price < #{max} ]]>
      </if>
    </where>


  </select>

测试

@Test
    public void sqlSpecial() {
        BookVo bookVo = new BookVo();
        bookVo.setMax(30);
        bookVo.setMin(20);

        List<Map> list = this.bookService.list6(bookVo);
        for (Map map : list) {
            System.out.println(map);
        }
    }

 

 

 

 

 

 

 

 

 

@Test
public void slectByLike() {

List<Book> books = this.bookService.slectBylike1(StringUtils.toLikeStr("圣墟"));
for (Book b : books) {
System.out.println(b);
}
}

mybatis动态sql以及分页

标签:页码   HERE   三种方式   and   data   ids   bat   ash   extend   

原文地址:https://www.cnblogs.com/psyu/p/11566798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!