mybatis:动态sql加分页

1.mybatis动态sql

    1.1 if

    1.2 trim 

trim:格式化标签 simpleDateFormart new date->是一个长整型

作用:构造可运行的sql语句

prifex:前缀

sufix:后缀

suffixOverrides:减除最后一个逗号

    1.3 foreach

bookVo

package com.zking.mybatis01.vo;

import com.zking.mybatis01.model.Book;

import java.util.List;

/**
 * @author Zhang
 * @site www.zhangmage.com
 * @company zking
 * @create 2018-12-16 11:14
 * vo层的母的在于不破坏原有的实体类与数据库的映射关系(结构关系)
 */
public class BookVo extends Book {
    private List<Integer> bookIds;

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

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


    1.4 其他

       choose/set/where


2.模糊查询(3种方式)
 
    2.1 参数中直接加入%%

    2.2 使用${...}代替#{...}(不建议使用该方式,有SQL注入风险)
       
          关键:#{...}与${...}区别?
          参数类型为字符串,#会在前后加单引号['],$则直接插入值

          注:
          1) mybatis中使用OGNL表达式传递参数
          2) 优先使用#{...}
          3) ${...}方式存在SQL注入风险

    2.3 SQL字符串拼接CONCAT

3.查询返回结果集

    resultMap:适合使用返回值是自定义实体类的情况(非人为提供的类就是自定义的,也就是jdk提供的类)
    resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型

    3.1 使用resultMap返回自定义类型集合
    
    3.2 使用resultType返回List<T>

    3.3 使用resultType返回单个对象

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

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

代码

bookMapper

/**
     * 讲解foreach
     * @param bookVo
     * @return
     */
    List<Book> list1(BookVo bookVo);

    /**
     * 讲解模糊查询
     * 1、#{} ognl 传入的字符串自带''
     * 2、${}占位符 如果没有''会报错 会形成sql注入
     * 3、concat 拼接两个字符串
     * @param book
     * @return
     */
    List<Book> list2(Book book);

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

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

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

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

    /**
     * 使用resultType返回单个对象
     */
    Book selectByPrimaryKey(Integer bid);

bookMapper.xml

<resultMap id="BaseResultMap" type="com.zking.mybatis01.model.Book" >
    <constructor >
      <idArg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="bname" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
    </constructor>
  </resultMap>


 <select id="list1" resultType="com.zking.mybatis01.model.Book" parameterType="com.zking.mybatis01.vo.BookVo">
    select
      <include refid="Base_Column_List"/>
      from t_mvc_book
      where bid in
      <foreach collection="bookIds" separator="," item="bid" open="(" close=")">
        #{bid}
      </foreach>
  </select>
  <select id="list2" resultType="com.zking.mybatis01.model.Book" parameterType="com.zking.mybatis01.model.Book">
    select
    <include refid="Base_Column_List"/>
    from t_mvc_book
    where bname like concat(concat('%',#{bname}),'%')
  </select>
  <select id="list3" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from t_mvc_book
  </select>
  <select id="list4" resultType="com.zking.mybatis01.model.Book">
    select
    <include refid="Base_Column_List"/>
    from t_mvc_book
  </select>
  <select id="list5" resultType="java.util.Map">
    select
    <include refid="Base_Column_List"/>
    from t_mvc_book
  </select>
  <select id="list6" resultType="java.util.Map" parameterType="java.util.Map">
    select
    <include refid="Base_Column_List"/>
    from t_mvc_book
    where bid =#{bid}
  </select>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from t_mvc_book
    where bid = #{bid,jdbcType=INTEGER}
  </select>

bookService

package com.zking.service;

import com.zking.mybatis01.model.Book;
import com.zking.mybatis01.uitl.PageBean;
import com.zking.mybatis01.vo.BookVo;

import java.util.List;
import java.util.Map;

/**
 * @author Zhang
 * @site www.zhangmage.com
 * @company zking
 * @create 2018-12-14 11:26
 */
public interface BookService {
    int insert(Book record);

    Book selectByPrimaryKey(Integer bid);
    List<Book> list1(BookVo bookVo);
    List<Book> list2(Book book);
    List<Book> list3();
    List<Book> list4();
    List<Map> list5();
    Map list6(Map map);
}

bookServiceImpl

 package com.zking.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zking.mybatis01.mapper.BookMapper;
import com.zking.mybatis01.model.Book;
import com.zking.mybatis01.uitl.PageBean;
import com.zking.mybatis01.vo.BookVo;
import com.zking.service.BookService;

import java.util.List;
import java.util.Map;

/**
 * @author Zhang
 * @site www.zhangmage.com
 * @company zking
 * @create 2018-12-14 11:35
 */
public class BookServiceImpl implements BookService {
    private BookMapper bookMapper;


    public Book selectByPrimaryKey(Integer bid) {
        return this.bookMapper.selectByPrimaryKey(bid);
    }

    @Override
    public List<Book> list1(BookVo bookVo) {
        return bookMapper.list1(bookVo);
    }

    @Override
    public List<Book> list2(Book book) {
        return bookMapper.list2(book);
    }

    @Override
    public List<Book> list3() {
        return bookMapper.list3();
    }

    @Override
    public List<Book> list4() {
        return bookMapper.list4();
    }

    @Override
    public List<Map> list5() {
        return bookMapper.list5();
    }

    @Override
    public Map list6(Map map) {
        return bookMapper.list6(map);
    }
}

test

public class BookServiceImplTest {
    private SqlSession sqlSession;
    private BookService bookService;
    @Before
    public void before(){
        sqlSession= SessionUtil.openSession();
        BookServiceImpl bookServiceImpl=new BookServiceImpl();
        BookMapper bookMapper= sqlSession.getMapper(BookMapper.class);
        bookServiceImpl.setBookMapper(bookMapper);
        bookService=bookServiceImpl;
    }
   

    @Test
    public void selectByPrimaryKey() {
        Book book = bookService.selectByPrimaryKey(1111);
        System.out.println(book);
    }
      @Test
      public void list1() {
          BookVo bookVo=new BookVo();
          bookVo.setBookIds(Arrays.asList(new Integer[]{1,3,5}));
          List<Book> books =this.bookService.list1(bookVo);
          for (Book book : books) {
              System.out.println(book);
          }
      }
    @Test
    public void list2() {
        Book book=new Book();
        book.setBname("圣墟");
        List<Book> books = this.bookService.list2(book);
        for (Book book1 : books) {
            System.out.println(book1);
        }
    }
    @Test
    public void list3() {
        List<Book> books = this.bookService.list3();
        for (Book book1 : books) {
            System.out.println(book1);
        }
    }
    @Test
    public void list4() {
        List<Book> books = this.bookService.list4();
        for (Book book1 : books) {
            System.out.println(book1);
        }
    }
    @Test
    public void list5() {
        List<Map> maps = this.bookService.list5();
        for (Map map : maps) {
            System.out.println(map);
        }
    }
    @Test
    public void list6() {
        Map map=new HashMap();
        map.put("bid",1);
        Map maps = this.bookService.list6(map);
        System.out.println(maps);
    }

     @After
    public void after(){
        sqlSession.commit();
        sqlSession.close();
    }

4.分页查询

package util

pageBean

package com.zking.mybatis01.uitl;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Map;

public class PageBean implements Serializable {

	private static final long serialVersionUID = 2422581023658455731L;

	//页码
	private int page=1;
	//每页显示记录数
	private int rows=5;
	//总记录数
	private int total=0;
	//是否分页
	private boolean isPagination=true;
	//上一次的请求路径
	private String url;
	//获取所有的请求参数
	private Map<String,String[]> map;
	
	public PageBean() {
		super();
	}
	
	//设置请求参数
	public void setRequest(HttpServletRequest req) {
		String page=req.getParameter("page");
		String rows=req.getParameter("rows");
		String pagination=req.getParameter("pagination");
		this.setPage(page);
		this.setRows(rows);
		this.setPagination(pagination);
		this.url=req.getContextPath()+req.getServletPath();
		this.map=req.getParameterMap();
	}
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Map<String, String[]> getMap() {
		return map;
	}

	public void setMap(Map<String, String[]> map) {
		this.map = map;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	
	public void setPage(String page) {
		if(null!=page&&!"".equals(page.trim()))
			this.page = Integer.parseInt(page);
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}
	
	public void setRows(String rows) {
		if(null!=rows&&!"".equals(rows.trim()))
			this.rows = Integer.parseInt(rows);
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}
	
	public void setTotal(String total) {
		this.total = Integer.parseInt(total);
	}

	public boolean isPagination() {
		return isPagination;
	}
	
	public void setPagination(boolean isPagination) {
		this.isPagination = isPagination;
	}
	
	public void setPagination(String isPagination) {
		if(null!=isPagination&&!"".equals(isPagination.trim()))
			this.isPagination = Boolean.parseBoolean(isPagination);
	}
	
	/**
	 * 获取分页起始标记位置
	 * @return
	 */
	public int getStartIndex() {
		//(当前页码-1)*显示记录数
		return (this.getPage()-1)*this.rows;
	}
	
	/**
	 * 末页
	 * @return
	 */
	public int getMaxPage() {
		int totalpage=this.total/this.rows;
		if(this.total%this.rows!=0)
			totalpage++;
		return totalpage;
	}
	
	/**
	 * 下一页
	 * @return
	 */
	public int getNextPage() {
		int nextPage=this.page+1;
		if(this.page>=this.getMaxPage())
			nextPage=this.getMaxPage();
		return nextPage;
	}
	
	/**
	 * 上一页
	 * @return
	 */
	public int getPreivousPage() {
		int previousPage=this.page-1;
		if(previousPage<1)
			previousPage=1;
		return previousPage;
	}

	@Override
	public String toString() {
		return "PageBean [page=" + page + ", rows=" + rows + ", total=" + total + ", isPagination=" + isPagination
				+ "]";
	}
}

sessionUtil

package com.zking.mybatis01.uitl;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

/**
 * @author 小李飞刀
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2018-12-10 21:59
 */
public class SessionUtil {
    private static SqlSessionFactory sessionFactory;
    private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
    static {
        sessionFactory = new SqlSessionFactoryBuilder().build(SessionUtil.class.getResourceAsStream("/mybatis.cfg.xml"));
    }

    public static SqlSession openSession() {
        SqlSession session = threadLocal.get();
        if (null == session) {
            session = sessionFactory.openSession();
            threadLocal.set(session);
        }
        return session;
    }

    public static void main(String[] args) {
        SqlSession session = openSession();
        System.out.println(session.getConnection());
        session.close();
//        System.out.println(session.getConnection());
    }
}

   为什么要重写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中
       <!-- 配置分页插件PageHelper, 4.0.0以后的版本支持自动识别使用的数据库 -->
       <plugin interceptor="com.github.pagehelper.PageInterceptor">
       </plugin>

    4.3 在你需要进行分页的Mybatis方法前调用PageHelper.startPage静态方法即可,紧跟在这个方法后的第一个Mybatis查询方法会被进行分页
       //设置分页处理
       if (null != pageBean && pageBean.isPaginate()) {
         PageHelper.startPage(pageBean.getCurPage(), pageBean.getPageRecord());
       }


       bookServiceImpl

 @Override
    public List<Map> list7(Map map, PageBean pageBean) {
        /**
         * jdbc:map->来自于jap
         * baseDao:
         *      1、获取到getCountSql->total->pageBean.setTotal
         *      2、setPageSql->list<T>->返回
         * hibernate:
         *      map->来自于jsp
         *      baseDao
         *           1、getCountHql->total->pageBean.setTotal
         *           2、不需要拼接出pageHql->hibernate.setFrist.../setMaxRes..
         * mybatis中借助github提供宝贵的pageHelper
         * pageHelper.start
         * 业务流程(this.bookMapper.list7(map))
         * pageBean.setTotal
         */
        if(pageBean!=null&&pageBean.isPagination())
            PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
            List<Map> maps = this.bookMapper.list7(map);
            if(pageBean!=null&&pageBean.isPagination()) {
                PageInfo pageInfo = new PageInfo();
                pageBean.setTotal(pageInfo.getTotal()+"");
            }
        return maps;
    }


    4.4 获取分页信息(二种方式)
      
     4.4.1 使用插件后,查询实际返回的是Page<E>,而非List<E>,Page继承了ArrayList,同时还包含分页相关的信息
          Page<Book> page = (Page<Book>)list;
          System.out.println("页码:" + page.getPageNum());
          System.out.println("页大小:" + page.getPageSize());
          System.out.println("总记录:" + page.getTotal());
     4.4.2 使用PageInfo
          PageInfo pageInfo = new PageInfo(list);
          System.out.println("页码:" + pageInfo.getPageNum());
          System.out.println("页大小:" + pageInfo.getPageSize());
          System.out.println("总记录:" + pageInfo.getTotal());

bookMpper


    /**
     * 讲解GitHub的分页插件
     * @param map
     * @return
     */
    List<Map> list7(Map map);

bookMapper.xml

 <select id="list7" resultType="java.util.Map" parameterType="java.util.Map">
    select
    <include refid="Base_Column_List"/>
    from t_mvc_book
    where 1=1
    <if test="bname!=null and bname!='' ">
      and bname like #{bname}
    </if>
  </select>

bookService

 Map list6(Map map);

test

 @Test
    /**
     * 拦截器进行分页
     */
    public void list7() {
        Map map=new HashMap();
        PageBean pageBean=new PageBean();
        pageBean.setPage(2);
//        map.put("start",7);
//        map.put("offset",13);
        map.put("bname","%圣墟%");
        List<Map> maps = this.bookService.list7(map,pageBean);
        for (Map map1 : maps) {
            System.out.println(map1);
        }
    }

5.特殊字符处理
    >(&gt;)   
    <(&lt;)  
    &(&amp;) 
 空格(&nbsp;)
 
 <![CDATA[ <= ]]> 

练习:
1)choose、set、where标签的使用(写例子)
2)mybatis实现批量insert/update/delete(选做)(注:批量修改时需要在jdbc.properties加上allowMultiQueries=true)
3)课堂代码3遍录屏
4)使用foreach标签遍历map集合 item是value index是key

bookMapper

 /**
     * choose的使用
     */
    List<Map> list9(Book book);

    /**
     * set的使用
     * @param book
     * @return
     */
    int update(Book book);
    int save(List<Book> books);
    int updateBook(List<Book> books);
    int remove(int[] bid);

bookMapper.xml

<select id="list9" resultType="java.util.Map" parameterType="com.zking.mybatis01.model.Book">
    select
    <include refid="Base_Column_List"/>
     from t_mvc_book
    <where>
      <choose>
        <when test="bname!=null and bname!=''">
          bname like #{bname}
        </when>
        <when test="price !=null and price!=''">
          price = #{price}
        </when>
        <otherwise>
          bid =1
        </otherwise>
      </choose>
    </where>
  </select>

 <update id="update">
    update t_mvc_book
    <set>
      <if test="bname!=null and bname!=''">
        bname=#{bname}
      </if>
    </set>
    where bid=#{bid}
  </update>


 <insert id="save" parameterType="java.util.List" keyProperty="bid">
      <selectKey resultType="java.lang.Integer" keyProperty="bid" order="AFTER">
        select @@IDENTITY
      </selectKey>
    <foreach collection="list" item="items" index="index" separator=";" >
      insert into t_mvc_book(bid,bname,price) values
      ( #{items.bid},
      #{items.bname},
      #{items.price})
    </foreach>
  </insert>


 <update id="updateBook" parameterType="java.util.List">
    <foreach collection="list" item="item" separator=";">
      update t_mvc_book
      <set>
        bname=#{item.bname},
        price=#{item.price},
      </set>
      where bid=#{item.bid}
    </foreach>
  </update>


<delete id="remove" parameterType="int">
    <foreach collection="array" item="item" separator=";">
      delete from t_mvc_book where bid in
      (#{item})
    </foreach>
  </delete>

bookService

 List<Map> list9(Book book);
    int update(Book book);
    int save(List<Book> books);
    int updateBook(List<Book> books);
    int remove(int[] bid);

bookServiceImpl

 @Override
    public List<Map> list9(Book book) {
        return this.bookMapper.list9(book);
    }

    @Override
    public int update(Book book) {
        return this.bookMapper.update(book);
    }

    @Override
    public int save(List<Book> books) {
        return this.bookMapper.save(books);
    }

    @Override
    public int updateBook(List<Book> books) {
        return this.bookMapper.updateBook(books);
    }

    @Override
    public int remove(int[] bid) {
        return this.bookMapper.remove(bid);
    }
}

test

 @Test
    public void list9() {
        Book book=new Book();
        List<Map> maps = this.bookService.list9(book);
        for (Map map1 : maps) {
            System.out.println(map1);
        }
    }
    @Test
    public void update() {
        Book book=new Book();
        book.setBname("煮冬瓜");
        book.setBid(1111);
        this.bookService.update(book);

    }
    @Test
    public void save() {
        List<Book> books=new ArrayList<>();
        books.add(new Book(17,"大冬瓜",34f));
        books.add(new Book(18,"大冬瓜",34f));;
        this.bookService.save(books);

    }
    @Test
    public void updateBook() {
        List<Book> books=new ArrayList<>();
        books.add(new Book(14,"圣墟第九章",14f));
        books.add(new Book(15,"圣墟第十章",14f));
        this.bookService.updateBook(books);

    }
    @Test
    public void removeBook() {
        int [] arr={18,1111};
        this.bookService.remove(arr);

    }

猜你喜欢

转载自blog.csdn.net/x_b_z123/article/details/85062684