1.mybatis动态sql
1.1 if
1.2 trim
1.3 foreach
遍历集合,批量查询、通常用于in关键字
bookmapper.xml:
bookserverceImpTest:测试
结果:
1.4 其他
choose/set/where
2.模糊查询(3种方式)
2.1 参数中直接加入%%
2.2 *使用${...}代替#{...}(不建议使用该方式,有SQL注入风险)
关键:#{...}与${...}区别?
参数类型为字符串,#会在前后加单引号['],$则直接插入值*
注:
1) mybatis中使用OGNL表达式传递参数
2) 优先使用#{...}
3) ${...}方式存在SQL注入风险
2.3 SQL字符串拼接CONCAT
bookMapper:
<select id="selectBooksLike1" resultType="com.hjc.model.Book" parameterType="java.lang.String">
select * from t_mvc_book
<where>
sname like #{sname}
</where>
</select>
<select id="selectBooksLike2" resultType="com.hjc.model.Book" parameterType="java.lang.String">
select * from t_mvc_book
<where>
sname like '${sname}'
</where>
</select>
<select id="selectBooksLike3" resultType="com.hjc.model.Book" parameterType="java.lang.String">
select * from t_mvc_book
<where>
sname like concat('%',#{sname},'%')
</where>
</select>
在bookserverTestImp中测试
@Test
public void selectBookslike() {
// for (Book book : this.bookService.selectBooksLike1("%白夜行%")) {
// System.out.println(book);
// }
// for (Book book : this.bookService.selectBooksLike2("%白%")) {
// System.out.println(book);
// }
for (Book book : this.bookService.selectBooksLike3("%夜%")) {
System.out.println(book);
}
}
结果:
3.查询返回结果集
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>,适用于多表查询返回单个结果集
bookMapper:
<select id="List1" resultMap="BaseResultMap">
select * from t_mvc_book
</select>
<select id="List2" resultType="com.hjc.model.Book">
select * from t_mvc_book
</select>
<select id="List3" resultType="com.hjc.model.Book" parameterType="com.hjc.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">
select * from t_mvc_book
</select>
<select id="List5" resultType="java.util.Map" parameterType="java.util.Map">
select * from t_mvc_book where bid = #{bid}
</select>
测试:
@Test
public void list1() {
for (Book book : this.bookService.List1()) {
System.out.println(book);
}
}
@Test
public void list2() {
for (Book book : this.bookService.List2()) {
System.out.println(book);
}
}
@Test
public void list3() {
BookVo bookVo=new BookVo();
List bookIds=new ArrayList();
bookIds.add(2);
bookIds.add(3);
bookVo.setBookIds(bookIds);
for (Book book : this.bookService.List3(bookVo)) {
System.out.println(book);
}
}
@Test
public void list4() {
for (Map map : this.bookService.List4()) {
System.out.println(map);
}
}
@Test
public void list5() {
Map map=new HashMap();
map.put("bid",2);
System.out.pri
4.分页查询
为什么要重写mybatis的分页?
Mybatis的分页功能很弱,它是基于内存的分页(查出所有记录再按偏移量offset和边界limit取结果),在大数据量的情况下这样的分页基本上是没有用的
使用分页插件步奏
1、导入pom依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2、Mybatis.cfg.xml配置拦截器
<plugins>
<!-- 配置分页插件PageHelper, 4.0.0以后的版本支持自动识别使用的数据库 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
3、使用PageHelper进行分页
@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.getPageNum());
System.out.println("显示当前内容数量:"+pageInfo.getPageSize());
System.out.println("符合当前的总条数:"+pageInfo.getTotal());
pageBean.setTotal(pageInfo.getTotal()+"");
}
return list;
}
4、处理分页结果
5.特殊字符处理
>(>)
<(<)
&(&)
空格( )
<![CDATA[ <= ]]><select id="list6" resultType="com.javaxl.model.Book" parameterType="com.javaxl.model.BookVo">
select * from t_mvc_book
<where>
<if test="null != min and min != ''">
<![CDATA[ and #{min} < price ]]>
</if>
<if test="null != max and max != ''">
<![CDATA[ and #{max} > price ]]>
</if>
</where>
</select>
<select id="list7" resultType="com.javaxl.model.Book" parameterType="com.javaxl.model.BookVo">
select * from t_mvc_book
<where>
<if test="null != min and min != ''">
and #{min} < price
</if>
<if test="null != max and max != ''">
and #{max} > price
</if>
</where>
</select>