1、mybatis动态sql
1、定义接口
/**
* Param
* 如果形参要在mapper.xml中使用就需要加Param注解
* map
* @param bookIds
* @return
*/
List<Book> selectBooksIn(@Param("bookIds") List bookIds);
2.动态sql
<select id="selectBooksIn" resultType="com.hzh.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>
3.foreach使用
2、模糊查询
1.三种模糊查询方式
/**
* mybatis对模糊查询共有三种方式:
* #{}
* ${}
* concat
* @param bname
* @return
*/
List<Book> selectBooksLike1(@Param("bname")String bname);
List<Book> selectBooksLike2(@Param("bname")String bname);
List<Book> selectBooksLike3(@Param("bname")String bname);
2.mapper.xml
<!--mybatis模糊查询-->
<select id="selectBooksLike1" resultType="com.hzh.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like #{bname}
</select>
<select id="selectBooksLike2" resultType="com.hzh.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like '${bname}'
</select>
<select id="selectBooksLike3" resultType="com.hzh.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
</select>
3、查询返回结果集的处理
1.处理结果集的五种情况方式
/**
* mybatis 处理结果集的五种情况方式
* @return
*/
List<Book> list1();
List<Book> list2();
List<Book> list3(BookVo bookVo);
List<Map> list4(Map map);
Map list5(Map map);
2.结果集处理方法
使用resultMap返回自定义类型集合
<select id="list1" resultMap="BaseResultMap">
select * from t_mvc_book
</select>
使用resultType返回List<T>
<select id="list2" resultType="com.hzh.model.Book">
select * from t_mvc_book
</select>
使用resultType返回单个对象
<select id="list3" resultType="com.hzh.model.Book" parameterType="com.hzh.model.vo.BookVo">
select * from t_mvc_book where bid in
<foreach collection="bookIds" item="bid" open="(" close=")" separator=",">
#{bid}
</foreach>
</select>
使用resultType返回List<Map>,适用于多表查询返回结果集
<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>
使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
<select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
select * from t_mvc_book where bid = #{bid}
</select>
4、分页查询
使用分页插件步奏
1、导入pom依赖
2、Mybatis.cfg.xml配置拦截器
3、使用PageHelper进行分页
4、处理分页结果
1.导入pom依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2.mybatis.cfg.xml中配置拦截器
3.使用分页插件
<select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
</select>
4.接口以及方法
List<Map> listPager(Map map);
<select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
select * from t_mvc_book where bname like #{bname}
</select>
5.分页实现类定义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;
}
6.测试分页效果
5、特殊字符处理
1.字符处理方法
>(>)
<(<)
&(&)
空格( )
<![CDATA[ <= ]]>
/*特殊字符的处理*/
List<Book> list6(BookVo bookVo);
<select id="list6" resultType="com.hzh.model.Book" parameterType="com.hzh.model.vo.BookVo">
select * from t_mvc_book where <![CDATA[ price > #{min} and price < #{max} ]]>
</select>
2.测试字符处理效果