1.mybatis动态sql
1.1 if
1.2 trim
trim:格式化标签 simpleDateFormart new date->是一个长整型
作用:构造可运行的sql语句
prifex:前缀
sufix:后缀
suffixOverrides:减除最后一个逗号
1.3 foreach
bookVo
package com.xxx.mybatis01.vo;
import com.xxx.mybatis01.model.Book;
import java.util.List;
/**
* @author Zhang
* vo层的母的在于不破坏原有的实体类与数据库的映射关系(结构关系)
*/
public class BookVo extends Book {
private List bookIds;
public List getBookIds() {
return bookIds;
}
public void setBookIds(List 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
3.3 使用resultType返回单个对象
3.4 使用resultType返回List
3.5 使用resultType返回Map
代码
bookMapper
/**
* 讲解foreach
* @param bookVo
* @return
*/
List list1(BookVo bookVo);
/**
* 讲解模糊查询
* 1、#{} ognl 传入的字符串自带''
* 2、${}占位符 如果没有''会报错 会形成sql注入
* 3、concat 拼接两个字符串
* @param book
* @return
*/
List list2(Book book);
/**
* 使用resultMap返回自定义类型集合
* @return
*/
List list3();
/**
* 使用resultType返回自定义类型集合List
* @return
*/
List list4();
/**
* 使用resultType返回List
bookMapper.xml
bookService
package com.xxx.service;
import com.xxx.mybatis01.model.Book;
import com.xxx.mybatis01.uitl.PageBean;
import com.xxx.mybatis01.vo.BookVo;
import java.util.List;
import java.util.Map;
/**
* @author Zhang
*/
public interface BookService {
int insert(Book record);
Book selectByPrimaryKey(Integer bid);
List list1(BookVo bookVo);
List list2(Book book);
List list3();
List list4();
List list5();
Map list6(Map map);
}
bookServiceImpl
package com.xxx.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.mybatis01.mapper.BookMapper;
import com.xxx.mybatis01.model.Book;
import com.xxx.mybatis01.uitl.PageBean;
import com.xxx.mybatis01.vo.BookVo;
import com.xxx.service.BookService;
import java.util.List;
import java.util.Map;
/**
* @author Zhang
*/
public class BookServiceImpl implements BookService {
private BookMapper bookMapper;
public Book selectByPrimaryKey(Integer bid) {
return this.bookMapper.selectByPrimaryKey(bid);
}
@Override
public List list1(BookVo bookVo) {
return bookMapper.list1(bookVo);
}
@Override
public List list2(Book book) {
return bookMapper.list2(book);
}
@Override
public List list3() {
return bookMapper.list3();
}
@Override
public List list4() {
return bookMapper.list4();
}
@Override
public List 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 books =this.bookService.list1(bookVo);
for (Book book : books) {
System.out.println(book);
}
}
@Test
public void list2() {
Book book=new Book();
book.setBname("圣墟");
List books = this.bookService.list2(book);
for (Book book1 : books) {
System.out.println(book1);
}
}
@Test
public void list3() {
List books = this.bookService.list3();
for (Book book1 : books) {
System.out.println(book1);
}
}
@Test
public void list4() {
List books = this.bookService.list4();
for (Book book1 : books) {
System.out.println(book1);
}
}
@Test
public void list5() {
List 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.xxx.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 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
+ "]";
}
}
sessionUtil
package com.xxx.mybatis01.uitl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* @author
*/
public class SessionUtil {
private static SqlSessionFactory sessionFactory;
private static ThreadLocal threadLocal = new ThreadLocal();
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 导入分页插件
4.2 将pagehelper插件配置到mybatis中
4.3 在你需要进行分页的Mybatis方法前调用PageHelper.startPage静态方法即可,紧跟在这个方法后的第一个Mybatis查询方法会被进行分页
//设置分页处理
if (null != pageBean && pageBean.isPaginate()) {
PageHelper.startPage(pageBean.getCurPage(), pageBean.getPageRecord());
}
bookServiceImpl
@Override
public List list7(Map map, PageBean pageBean) {
/**
* jdbc:map->来自于jap
* baseDao:
* 1、获取到getCountSql->total->pageBean.setTotal
* 2、setPageSql->list->返回
* 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 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
Page
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 list7(Map map);
bookMapper.xml
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 maps = this.bookService.list7(map,pageBean);
for (Map map1 : maps) {
System.out.println(map1);
}
}
5.特殊字符处理
>(>)
<(<)
&(&)
空格( )
练习:
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 list9(Book book);
/**
* set的使用
* @param book
* @return
*/
int update(Book book);
int save(List books);
int updateBook(List books);
int remove(int[] bid);
bookMapper.xml
update t_mvc_book
bname=#{bname}
where bid=#{bid}
select @@IDENTITY
insert into t_mvc_book(bid,bname,price) values
( #{items.bid},
#{items.bname},
#{items.price})
update t_mvc_book
bname=#{item.bname},
price=#{item.price},
where bid=#{item.bid}
delete from t_mvc_book where bid in
(#{item})
bookService
List list9(Book book);
int update(Book book);
int save(List books);
int updateBook(List books);
int remove(int[] bid);
bookServiceImpl
@Override
public List list9(Book book) {
return this.bookMapper.list9(book);
}
@Override
public int update(Book book) {
return this.bookMapper.update(book);
}
@Override
public int save(List books) {
return this.bookMapper.save(books);
}
@Override
public int updateBook(List 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 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 books=new ArrayList<>();
books.add(new Book(17,"大冬瓜",34f));
books.add(new Book(18,"大冬瓜",34f));;
this.bookService.save(books);
}
@Test
public void updateBook() {
List 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);
}