Mybatis动态SQL
If、trim、foreach
BookMapper
1 /** 2 * 如果形参要在mapper.xml中使用需要加上面注解 3 * map.name: zs age: 12 4 * @param bookIds 5 * @return 6 */ 7 List<Book> selectBooksIn(@Param("bookIds") List bookIds);
BookService
1 List<Book> selectBooksIn(List bookIds);
BookServiceImpl
1 @Override 2 public List<Book> selectBooksIn(List bookIds) { 3 return bookMapper.selectBooksIn(bookIds); 4 }
Bookmapper.xml
1 <select id="selectBooksIn" resultType="com.Mybatis.model.Book" parameterType="java.util.List"> 2 select * from t_mvc_book where bid in 3 <foreach collection="bookIds" item="bid" open="(" close=")" separator=","> 4 #{bid} 5 </foreach> 6 </select>
结果:
模糊查询
Mybatis对模糊查询共有三种方式
{}
${}
concat
#与$的区别
$会引起sql攻击
Java代码差别(#):%圣墟%
select * from t_mvc_book where bname like #{bname}
select * from t_mvc_book where bname like %圣墟%
select * from t_mvc_book where bname like ‘${bname}‘ 人为加的引导
%圣墟%正常传值
比如:‘%圣墟% or 1=1‘
select * from t_mvc_book where bname like ‘‘%圣墟% or 1=1‘‘
BookMapper
1 /** 2 *Mybatis对模糊查询共有三种方式 3 * #{} 4 * ${} 5 * concat 6 * @param bname 7 * @return 8 */ 9 List<Book> selectBooksLike1(@Param("bname")String bname); 10 List<Book> selectBooksLike2(@Param("bname")String bname); 11 List<Book> selectBooksLike3(@Param("bname")String bname);
BookService
1 /** 2 *Mybatis对模糊查询共有三种方式 3 * #{} 4 * ${} 5 * concat 6 * @param bname 7 * @return 8 */ 9 List<Book> selectBooksLike1(String bname); 10 List<Book> selectBooksLike2(String bname); 11 List<Book> selectBooksLike3(String bname);
Bookmapper.xml
1 <select id="selectBooksLike1" resultType="com.Mybatis.model.Book" parameterType="java.lang.String"> 2 select * from t_mvc_book where bname like #{bname} 3 </select> 4 5 <select id="selectBooksLike2" resultType="com.Mybatis.model.Book" parameterType="java.lang.String"> 6 select * from t_mvc_book where bname like ‘${bname}‘ 7 </select> 8 9 <select id="selectBooksLike3" resultType="com.Mybatis.model.Book" parameterType="java.lang.String"> 10 select * from t_mvc_book where bname like concat(concat(‘%‘,#{bname}),‘%‘) 11 </select>
BookServiceImpl
1 @Override 2 public List<Book> selectBooksLike1(String bname) { 3 return bookMapper.selectBooksLike1(bname); 4 } 5 6 @Override 7 public List<Book> selectBooksLike2(String bname) { 8 return bookMapper.selectBooksLike2(bname); 9 } 10 11 @Override 12 public List<Book> selectBooksLike3(String bname) { 13 return bookMapper.selectBooksLike3(bname); 14 }
测试:
Mybatis结果集处理的五种情况
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型
使用resultMap返回自定义类型集合
使用resultType返回List<T>
使用resultType返回单个对象
使用resultType返回List<Map>,适用于多表查询返回结果集
使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
BookMapper
1 List<Book> list1(); 2 List<Book> list2(); 3 List<Book> list3(BookVo bookVo); 4 List<Map> list4(Map map); 5 Map list5(Map map);
Bookmapper.xml
1 <select id="list1" resultMap="BaseResultMap"> 2 select * from t_mvc_book 3 </select> 4 <select id="list2" resultMap="BaseResultMap"> 5 select * from t_mvc_book 6 </select> 7 <select id="list3" resultType="com.Mybatis.model.Book" parameterType="com.Mybatis.model.vo.BookVo"> 8 select * from t_mvc_book where bid in 9 <foreach collection="bookIds" item="bid" open="(" close=")" separator=","> 10 #{bid} 11 </foreach> 12 </select> 13 <select id="list4" resultType="java.util.Map" parameterType="java.util.Map"> 14 select * from t_mvc_book where bid in 15 <foreach collection="bookIds" item="bid" open="(" close=")" separator=","> 16 #{bid} 17 </foreach> 18 </select> 19 <select id="list5" resultType="java.util.Map" parameterType="java.util.Map"> 20 select * from t_mvc_book where bid = #{bid} 21 </select>
BookService
1 List<Book> list1(); 2 List<Book> list2(); 3 List<Book> list3(BookVo bookVo); 4 List<Map> list4(Map map); 5 Map list5(Map map);
BookServiceImpl
1 @Override 2 public List<Book> list2() { 3 return bookMapper.list2(); 4 } 5 6 @Override 7 public List<Book> list3(BookVo bookVo) { 8 return bookMapper.list3(bookVo); 9 } 10 11 @Override 12 public List<Map> list4(Map map) { 13 return bookMapper.list4(map); 14 } 15 16 @Override 17 public Map list5(Map map) { 18 return bookMapper.list5(map); 19 }
测试:
分页查询
为什么要重写mybatis的分页?
Mybatis的分页功能很弱,它是基于内存的分页(查出所有记录再按偏移量offset和边界limit取结果),在大数据量的情况下这样的分页基本上是没有用的
1、导入pom依赖
1 <dependency> 2 <groupId>com.github.pagehelper</groupId> 3 <artifactId>pagehelper</artifactId> 4 <version>5.1.2</version> 5 </dependency>
Mybatis.cfg.xml配置拦截器
BookMapper
1 /** 2 * 分页 3 * @param map 4 * @return 5 */ 6 List<Map> listPager(Map map);
Bookmapper.xml
<select id="listPager" resultType="java.util.Map" parameterType="java.util.Map"> select * from t_mvc_book where bname like #{bname} </select>
BookService
1 /** 2 * 分页 3 * @param map 4 * @return 5 */ 6 List<Map> listPager(Map map, PageBean pageBean);
BookServiceImpl
1 @Override 2 public List<Map> listPager(Map map, PageBean pageBean) { 3 if (pageBean != null && pageBean.isPagination()){ 4 PageHelper.startPage(pageBean.getPage(),pageBean.getRows()); 5 } 6 List<Map> list = bookMapper.listPager(map); 7 if (pageBean != null && pageBean.isPagination()){ 8 PageInfo pageInfo = new PageInfo(list); 9 System.out.println("总记录数:" pageInfo.getTotal()); 10 System.out.println("当前页:" pageInfo.getPageNum()); 11 System.out.println("页大小:" pageInfo.getPageSize()); 12 pageBean.setTotal(pageInfo.getTotal() ""); 13 System.out.println("总页数:" pageBean.getMaxPage()); 14 } 15 return list; 16 }
测试:
特殊字符处理
>(>)
<(<)
&(&)
空格( )
<![CDATA[ <= ]]>
BookMapper
1 /** 2 * 特殊字符处理 3 * @param bookVo 4 * @return 5 */ 6 List<Book> list6(BookVo bookVo);
Bookmapper.xml
1 <select id="list6" resultType="com.Mybatis.model.Book" parameterType="com.Mybatis.model.vo.BookVo"> 2 select * from t_mvc_book where <![CDATA[ price > #{min} and price < #{max} ]]> 3 </select>
BookService
1 /** 2 * 特殊字符处理 3 * @param bookVo 4 * @return 5 */ 6 List<Book> list6(BookVo bookVo);
BookServiceImpl
1 @Override 2 public List<Book> list6(BookVo bookVo) { 3 return bookMapper.list6(bookVo); 4 }
测试: