今天主要是对上次自动生成的映射文件进行编写
新加了两个工具类
PageBean:
package com.lihao.util;
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=10;
//总记录数
private int total=0;
//是否分页
private boolean isPagination=true;
//上一次的请求路径
private String url;
//获取所有的请求参数
private Map 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 getMap() {
return map;
}
public void setMap(Map 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
+ "]";
}
}
StringUtils:
package com.lihao.util;
/**
* @author 浩梦溪
* @create 2019-09-20 16:43
*/
public class StringUtils {
public static String toLikeStr(String str){
return "%"+str+"%";
}
}
BookVo
vo类,就是value Object,用来处理查询条件所需要用到的而又不是数据库字段的属性
package com.lihao.model;
import java.util.List;
/**
* @author 浩梦溪
* @create 2019-09-20 19:55
*
* vo用来存放包裹数据库表映射字段以及多余查询条件所需属性
*/
public class BookVo {
private List bookIds;
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 getBookIds() {
return bookIds;
}
public void setBookIds(List bookIds) {
this.bookIds = bookIds;
}
}
BookMapper:
模糊查定义参数需要声明名(@Param),在xml中需要声明参数类型
package com.lihao.mapper;
import com.lihao.model.Book;
import com.lihao.model.BookVo;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface BookMapper {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
List selectByIn(@Param("bookIds") List bookIds);
/*模糊查*/
List selectBylike1(@Param("bname") String bname);
List selectBylike2(@Param("bname") String bname);
List selectBylike3(@Param("bname") String bname);
/*返回结果集处理*/
// 3.1 使用resultMap返回自定义类型集合
List list1();
// 3.2 使用resultType返回List
List list2();
// 3.3 使用resultType返回单个对象
Book list3(BookVo bookVo);
// 3.4 使用resultType返回List
BookMapper.xml:
bid, bname, price
delete from t_mvc_book
where bid = #{bid,jdbcType=INTEGER}
insert into t_mvc_book (bid, bname, price
)
values (#{bid,jdbcType=INTEGER}, #{bname,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
)
insert into t_mvc_book
bid,
bname,
price,
#{bid,jdbcType=INTEGER},
#{bname,jdbcType=VARCHAR},
#{price,jdbcType=REAL},
update t_mvc_book
bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL},
where bid = #{bid,jdbcType=INTEGER}
update t_mvc_book
set bname = #{bname,jdbcType=VARCHAR},
price = #{price,jdbcType=REAL}
where bid = #{bid,jdbcType=INTEGER}
动态sql、结果集处理和特殊字符处理上面注释写的都很清楚了,后面主要提一下分页的插件
导入分页插件pom:
com.github.pagehelper
pagehelper
5.1.2
将pagehelper插件配置到mybatis中,注意要配在运行环境之前
BookService:
package com.lihao.service;
import com.lihao.model.Book;
import com.lihao.model.BookVo;
import com.lihao.util.PageBean;
import java.util.List;
import java.util.Map;
/**
* @author 浩梦溪
* @create 2019-09-19 20:24
*/
public interface BookService {
int deleteByPrimaryKey(Integer bid);
int insert(Book record);
int insertSelective(Book record);
Book selectByPrimaryKey(Integer bid);
int updateByPrimaryKeySelective(Book record);
int updateByPrimaryKey(Book record);
List selectByIn( List bookIds);
/*模糊查*/
List selectBylike1(String bname);
List selectBylike2(String bname);
List selectBylike3(String bname);
/*返回结果集处理*/
List list1();
List list2();
Book list3(BookVo bookVo);
List list4(Map map);
Map list5(Map book);
/*分页*/
List listPager(Map map, PageBean pageBean);
/*特殊字符处理*/
List list6(BookVo bookVo);
List list7(BookVo bookVo);
}
BookServiceImpl:
package com.lihao.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lihao.mapper.BookMapper;
import com.lihao.model.Book;
import com.lihao.model.BookVo;
import com.lihao.service.BookService;
import com.lihao.util.PageBean;
import java.util.List;
import java.util.Map;
/**
* @author 浩梦溪
* @create 2019-09-19 20:26
*/
public class BookServiceImpl implements BookService {
private BookMapper bookMapper;
public BookMapper getBookMapper() {
return bookMapper;
}
public void setBookMapper(BookMapper bookMapper) {
this.bookMapper = bookMapper;
}
@Override
public int deleteByPrimaryKey(Integer bid) {
return bookMapper.deleteByPrimaryKey(bid);
}
@Override
public int insert(Book record) {
return bookMapper.insert(record);
}
@Override
public int insertSelective(Book record) {
return bookMapper.insertSelective(record);
}
@Override
public Book selectByPrimaryKey(Integer bid) {
return bookMapper.selectByPrimaryKey(bid);
}
@Override
public int updateByPrimaryKeySelective(Book record) {
return bookMapper.updateByPrimaryKeySelective(record);
}
@Override
public int updateByPrimaryKey(Book record) {
return bookMapper.updateByPrimaryKey(record);
}
@Override
public List selectByIn(List bookIds) {
return bookMapper.selectByIn(bookIds);
}
@Override
public List selectBylike1(String bname) {
return bookMapper.selectBylike1(bname);
}
@Override
public List selectBylike2(String bname) {
return bookMapper.selectBylike2(bname);
}
@Override
public List selectBylike3(String bname) {
return bookMapper.selectBylike3(bname);
}
@Override
public List list1() {
return bookMapper.list1();
}
@Override
public List list2() {
return bookMapper.list2();
}
@Override
public Book list3(BookVo bookVo) {
return bookMapper.list3(bookVo);
}
@Override
public List list4(Map map) {
return bookMapper.list4(map);
}
@Override
public Map list5(Map book) {
return bookMapper.list5(book);
}
@Override
public List listPager(Map map, PageBean pageBean) {
if(pageBean != null && pageBean.isPagination()){
PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
}
List list = this.bookMapper.list4(map);
if(pageBean != null && pageBean.isPagination()){
PageInfo pageInfo=new PageInfo(list);
System.out.println("当前的页码:"+pageInfo.getPageNum());
System.out.println("页数据量:"+pageInfo.getSize());
System.out.println("总记录数:"+pageInfo.getTotal());
pageBean.setTotal(pageInfo.getTotal()+"");
}
return list;
}
@Override
public List list6(BookVo bookVo) {
return bookMapper.list6(bookVo);
}
@Override
public List list7(BookVo bookVo) {
return bookMapper.list7(bookVo);
}
}
JUnit测试代码:
package com.lihao.test;
import com.lihao.mapper.BookMapper;
import com.lihao.model.Book;
import com.lihao.model.BookVo;
import com.lihao.service.BookService;
import com.lihao.service.impl.BookServiceImpl;
import com.lihao.util.PageBean;
import com.lihao.util.SessionUtil;
import com.lihao.util.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author 小李飞刀
* @site www.javaxl.com
* @company
* @create 2019-09-19 15:35
*/
public class MapperSqlTest {
private BookService bookService;
private SqlSession sqlSession;
@Before
public void setUp() throws Exception {
BookServiceImpl bookServiceImpl = new BookServiceImpl();
sqlSession = SessionUtil.openSession();
bookServiceImpl.setBookMapper(sqlSession.getMapper(BookMapper.class));
this.bookService = bookServiceImpl;
}
@After
public void tearDown() throws Exception {
sqlSession.commit();
sqlSession.close();
}
@Test
public void selectByIn() {
List list=new ArrayList();
list.add(2);
list.add(5);
list.add(27);
list.add(19);
List books = this.bookService.selectByIn(list);
for (Book book : books) {
System.out.println(book);
}
}
/**
* 模糊查
*/
@Test
public void selectBylike() {
// List books = this.bookService.selectBylike1("%圣墟%");
// List books = this.bookService.selectBylike1(StringUtils.toLikeStr("圣墟"));
/*${}需要在外面加引号,存在sql攻击的可能*/
// List books = this.bookService.selectBylike2("%圣墟%");
List books = this.bookService.selectBylike3("圣墟");
for (Book book : books) {
System.out.println(book);
}
}
/**
* 结果集处理
*/
@Test
public void list() {
/*1、返回一个resultMap但是使用list*/
// List books = this.bookService.list1();
/*2、返回的是resulttype使用list接收*/
// List books = this.bookService.list2();
// for (Book b : books) {
// System.out.println(b);
// }
/*3、返回的是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);
//
/*4、返回的是resulttype,然后用List进行接收*/
Map map=new HashMap<>();
// map.put("bname", StringUtils.toLikeStr("圣墟"));
// List list=this.bookService.list4(map);
// for (Map m : list) {
// System.out.println(m);
// }
/*5、返回的是resultType,然后用List进行接收*/
map.put("bid",27);
Map m = this.bookService.list5(map);
System.out.println(m);
}
/**
* 分页
*/
@Test
public void listPage(){
Map map=new HashMap();
map.put("bname", StringUtils.toLikeStr("圣墟"));
PageBean pageBean=new PageBean();
List list = this.bookService.listPager(map, pageBean);
for (Map m : list) {
System.out.println(m);
}
}
/**
* 特殊字符处理
*/
@Test
public void sqlSpecial(){
BookVo bookVo=new BookVo();
bookVo.setMax(30);
bookVo.setMin(20);
// List list = this.bookService.list6(bookVo);
List list = this.bookService.list7(bookVo);
for (Map m : list) {
System.out.println(m);
}
}
}