mybatis动态sql
If、trim、foreach
If: if是条件,如果传的值不为空,那么这个字段就可以发生改变;如果传的值为空,那么执行sql的时候这个字段就看不到了
**trim:**去空格
举例:
<insert id="insertSelective" parameterType="com.xieminglu.model.Book" >
insert into t_mvc_book
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="bid != null" >
bid,
</if>
<if test="bname != null" >
bname,
</if>
<if test="price != null" >
price,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="bid != null" >
#{bid,jdbcType=INTEGER},
</if>
<if test="bname != null" >
#{bname,jdbcType=VARCHAR},
</if>
<if test="price != null" >
#{price,jdbcType=REAL},
</if>
</trim>
</insert>
foreach: 如果形参要在mapper.xml中使用就需要加上注解
如:(@Param(“bookIds”))
List<Book> selectBooksIn(@Param("bookIds") List bookIds);
<select id="selectBooksIn" resultType="com.javaxl.model.Book" parameterType="java.util.List">
select * from t_mvc_book where bid in
<foreach collection="bookIds" open="(" close=")" separator="," item="bid">
#{bid}
</foreach>
</select>
模糊查询
mybatis三种模糊查询
#{...}
${...}
Concat
注意:#{...}自带引号,${...}有sql注入的风险
List<Book> selectBooksList1(@Param("bname") String bname);
List<Book> selectBooksList2(@Param("bname") String bname);
List<Book> selectBooksList3(@Param("bname") String bname);
<select id="selectBooksList1" resultType="com.xieminglu.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like #{bname}
</select>
<select id="selectBooksList2" resultType="com.xieminglu.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like '${bname}'
</select>
<select id="selectBooksList3" resultType="com.xieminglu.model.Book" parameterType="java.lang.String">
select * from t_mvc_book where bname like concat(concat('%',#{bname}),'%')
</select>
三种查询结果都一样
重点:MyBatis中#和$的区别()
1. #将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。
如:order by #user_id#,如果传入的值是111,那么解析成sql时的值为order by '111',
如果传入的值是id,则解析成的sql为order by "id".
2. $将传入的数据直接显示生成在sql中。
如:order by $user_id$,如果传入的值是111,那么解析成sql时的值为order by user_id,
如果传入的值是id,则解析成的sql为order by id.
3. #方式能够很大程度防止sql注入。
4. $方式无法防止Sql注入。
5. $方式一般用于传入数据库对象,例如传入表名.
6. 一般能用#的就别用$.
查询返回结果集的处理
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型
3.1 使用resultMap返回自定义类型集合
3.2 使用resultType返回List<T>
3.3 使用resultType返回单个对象
3.4 使用resultType返回List<Map>,适用于多表查询返回结果集
3.5 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
/*
* mybatis处理结果集的五种情况
* */
/**
* 使用resultMap返回自定义类型集合
* @return
*/
List<Book> list1();
/**
* 使用resultType返回List<T>
* @return
*/
List<Book> list2();
/**
* 使用resultType返回单个对象
* @return
*/
List<Book> list3(BookVo bookVo);
/**
* 使用resultType返回List<Map>,适用于多表查询返回结果集
* @return
*/
List<Map> list4(Map map);
/**
* 使用resultType返回Map<String,Object>,适用于多表查询返回单个结果集
* @return
*/
Map list5(Map book);
<select id="list1" resultMap="BaseResultMap">
select * from t_mvc_book
</select>
<select id="list2" resultType="com.xieminglu.model.Book">
select * from t_mvc_book
</select>
<select id="list3" resultType="com.xieminglu.model.Book" parameterType="com.xieminglu.model.BookVo">
select * from t_mvc_book where bid in
<foreach collection="bookIds" item="bid" open="(" close=")" separator=",">
#{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>
@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(12);
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));
}
分页查询
为什么要重写mybatis的分页?
Mybatis的分页功能很弱,它是基于内存的分页(查出所有记录再按偏移量offset和边界limit取结果),在大数据量的情况下这样的分页基本上是没有用的
使用分页插件步奏
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>
顺序按着下面这个图片来,否则顺序不对的话会报错
使用PageHelper进行分页
Mapper层
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>
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[ <= ]]>
相关代码配置
/*
* mybatis特殊字符处理
* */
List<Book> list6(BookVo bookVo);
<select id="list6" resultType="com.xieminglu.model.Book" parameterType="com.xieminglu.model.BookVo">
select * from t_mvc_book where price > #{min} and price < #{max}
</select>
或
<select id="list6" resultType="com.xieminglu.model.Book" parameterType="com.xieminglu.model.BookVo">
select * from t_mvc_book where <![CDATA[ price > #{min} and price < #{max} ]]>
</select>