目标
1、mybatis动态sql
2、模糊查询
3、查询返回结果集的处理
4、分页查询
5、特殊字符处理
mybatis动态sql
1.1 if
1.2 trim
1.3 foreach
遍历集合,批量查询、通常用于in关键字
foreach
<!--foreach标签的使用-->
<select id="selectBooksIn" resultType="com.likang.model.Book" parameterType="java.util.List">
select * from t_mvc_book where bid in
<foreach collection="bookIds" item="bid" open="(" close=")" separator=",">
#{bid}
</foreach>
</select>
List<Book> selectBooksIn(@Param("bookIds") List bookIds);
2、模糊查询
#{…}
${…}
Concat
<select id="selectBooksLike1" resultType="com.likang.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like #{bname}
</select>
<select id="selectBooksLike2" resultType="com.likang.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like '${bname}'
</select>
<select id="selectBooksLike3" resultType="com.likang.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
</select>
List<Book> selectBooksLike1(@Param("bname")String bname);
List<Book> selectBooksLike2(@Param("bname")String bname);
List<Book> selectBooksLike3(@Param("bname")String bname);
@Test
public void selectBookslike1() {
String bname="斗破";
// List<Book> books = this.bookService.selectBooksLike1(StringUtils.toLikeStr(bname));
List<Book> books = this.bookService.selectBooksLike2(StringUtils.toLikeStr(bname));
// List<Book> books = this.bookService.selectBooksLike3(bname);
for (Book b : books) {
System.out.println(b);
}
}
查询返回结果集的处理
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型
3.1 使用resultMap返回自定义类型集合
3.2 使用resultType返回List
3.3 使用resultType返回单个对象
3.4 使用resultType返回List,适用于多表查询返回结果集
3.5 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
<select id="list1" resultMap="BaseResultMap">
select * from t_mvc_book
</select>
<select id="list2" resultType="com.likang.model.Book" >
select * from t_mvc_book
</select>
<select id="list3" resultType="com.likang.model.Book" parameterType="com.likang.model.vo.BookVo">
select * from t_mvc_book where bid in
<foreach collection="bookIds" item="bid" open="(" close=")" separator=",">
#{bid}
#{bid}
</foreach>
</select>
<select id="list4" resultType="java.util.Map" parameterType="java.util.Map">
select * from t_mvc_book where bid in
<foreach collection="bookIds" item="bid" open="(" close=")" separator=",">
#{bid}
</foreach>
</select>
<select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
select * from t_mvc_book where bid = #{bid}
</select>
//使用resultMap返回自定义类型集合
List<Book> list1();
//使用resultType返回List<T>
List<Book> list2();
//使用resultType返回单个对象
Book list3(BookVo bookVo);
// 使用resultType返回List<Map>,适用于多表查询返回结果集
List<Map> list4();
// 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
Map list5(Map book);
@Test
public void list(){
// List<Book> books=this.bookService.list1();
// List<Book> books=this.bookService.list2();
List list=new ArrayList();
list.add(1);
list.add(16);
// BookVo bookVo=new BookVo();
// bookVo.setBookIds(list);
// List<Book> books = this.bookService.list3(bookVo);
// for (Book b : books) {
// System.out.println(b);
// }
Map map=new HashMap();
// map.put("bookIds",list);
// List<Map> mapList = this.bookService.list4(map);
// for (Map m : mapList) {
// System.out.println(m);
// }
map.put("bid",1);
System.out.println(this.bookService.list5(map));
}
分页查询
1、导入pom依赖
2、Mybatis.cfg.xml配置拦截器
3、使用PageHelper进行分页
4、处理分页结果
pom依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
Mybatis.cfg.xml配置拦截器
<plugins>
<!-- 配置分页插件PageHelper, 4.0.0以后的版本支持自动识别使用的数据库 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
使用分页插件
<select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
select * from t_mvc_book where bname like #{bname}
</select>
Mapper
List<Map> listPager(Map map);
Service
List<Map> listPager(Map map, PageBean pageBean);
Override
public List<Map> listPager(Map map, PageBean pageBean) {
if (pageBean != null && pageBean.isPagination()){
PageHelper.startPage(pageBean.getPage(),pageBean.getRows());
}
List<Map> list = bookMapper.listPager(map);
if (pageBean != null && pageBean.isPagination()){
PageInfo pageInfo = new PageInfo(list);
System.out.println("总记录数:" + pageInfo.getTotal());
System.out.println("当前页:" + pageInfo.getPageNum());
System.out.println("页大小:" + pageInfo.getPageSize());
pageBean.setTotal(pageInfo.getTotal()+"");
System.out.println("总页数:" + pageBean.getMaxPage());
}
return list;
}
特殊字符处理
(>)
<(<)
&(&)
空格( )
<![CDATA[ <= ]]>
<select id="list6" resultType="com.likang.model.Book" parameterType="com.likang.model.vo.BookVo">
select * from t_mvc_book where <![CDATA[ price >#{min} and price <#{max} ]]>
</select>
<select id="list7" resultType="com.likang.model.Book" parameterType="com.likang.model.vo.BookVo">
select * from t_mvc_book where >#{min} and price <#{max}
</select>