目标
1、mybatis动态sql
2、模糊查询
3、查询返回结果集的处理
4、分页查询
5、特殊字符处理
mybatis动态sql
If、trim、foreach
<select id="selectByIn" resultType="com.ly.model.Student" parameterType="java.util.List">
select * from tb_stu where sid in
<foreach collection="sids" open="(" close=")" separator="," item="sid">
#{sid}
</foreach>
</select>
List<Student> selectByIn(@Param("sids") List sids);
模糊查询
#{...}
${...}
Concat
注意:#{…}自带引号,${…}有sql注入的风险
<select id="selectBylike1" resultType="com.ly.model.Student" parameterType="java.lang.String">
select * from tb_stu where sname like #{sname}
</select>
<select id="selectBylike2" resultType="com.ly.model.Student" parameterType="java.lang.String">
select * from tb_stu where sname like '${sname}'
</select>
<select id="selectBylike3" resultType="com.ly.model.Student" parameterType="java.lang.String">
select * from tb_stu where sname like concat(concat('%',#{sname}),'%')
</select>
List<Student> selectBylike1(@Param("sname") String sname);
List<Student> selectBylike2(@Param("sname") String sname);
List<Student> selectBylike3(@Param("sname") String sname);
查询返回结果集的处理
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型
1 使用resultMap返回自定义类型集合
2 使用resultType返回List
3 使用resultType返回单个对象
4 使用resultType返回List
<select id="list1" resultMap="BaseResultMap">
select * from tb_stu
</select>
<select id="list2" resultType="com.ly.model.Student">
select * from tb_stu
</select>
<select id="list3" resultType="com.ly.model.Student" parameterType="com.ly.model.StudentVo">
select * from tb_stu where sid in
<foreach collection="sids" open="(" close=")" separator="," item="sid">
#{sid}
</foreach>
</select>
<select id="list4" resultType="java.util.Map" parameterType="java.util.Map">
select * from tb_stu
<where>
<if test="null != sname and sname != ''">
and sname like #{sname}
</if>
</where>
</select>
<select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
select * from tb_stu
<where>
<if test="null != sid and sid != ''">
and sid = #{sid}
</if>
</where>
</select>
测试代码
@Test
public void list() {
//返回一个resultmap但是使用list<T>
// List<Student> stus = this.stuService.list1();
//返回一个resulttype但是使用list<T>接收
// List<Student> stus = this.stuService.list2();
// for (Student stu : stus) {
// System.out.println(stu);
// }
//返回一个resulttype但是使用T接收
/*StudentVo studentVo = new StudentVo();
List list = new ArrayList();
list.add(1);
studentVo.setSids(list);
Student student = this.stuService.list3(studentVo);
System.out.println(student);*/
//返回的是resulttype,然后用list<Mao>进行接收
Map map = new HashMap();
/*map.put("sname",StringUtils.toLikeStr("宜章"));
List<Map> list = this.stuService.list4(map);
for (Map m : list) {
System.out.println(m);
}*/
//返回的是resulttype,然后用Map进行接收
map.put("sid",1);
Map m = this.stuService.list5(map);
System.out.println(m);
}
分页查询
为什么要重写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>
使用分页插件
<select id="listPager" resultType="java.util.Map" parameterType="java.util.Map">
select * from tb_stu where sname like concat(concat('%',#{sname}),'%')
</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 = this.studentMapper.list4(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;
}
测试代码
@Test
public void listPager() {
Map map = new HashMap();
map.put("sname",StringUtils.toLikeStr("宜章"));
PageBean pageBean = new PageBean();
List<Map> list = this.stuService.listPager(map, pageBean);
for (Map m : list) {
System.out.println(m);
}
}
特殊字符处理
>(>)
<(<)
&(&)
空格( )
<![CDATA[ <= ]]>
package com.ly.model;
import java.util.List;
/**
* @author 毅哥哥
* @site
* @company
* @create 2019-09-21 12:47
* vo类用来存放包括数据库表映射字段以及多余查询条件所需属性
*/
public class StudentVo {
private List<String> sids;
private float min;
private float max;
public float getMin() {
return min;
}
public void setMin(float min) {
this.min = min;
}
public float getMax() {
return max;
}
public void setMax(float max) {
this.max = max;
}
public List<String> getSids() {
return sids;
}
public void setSids(List<String> sids) {
this.sids = sids;
}
}
<select id="list6" resultType="java.util.Map" parameterType="com.ly.model.StudentVo">
select * from tb_stu
<where>
<if test="null != min and min != ''">
and sid > #{min}
</if>
<if test="null != max and max != ''">
and sid < #{max}
</if>
</where>
</select>
<select id="list7" resultType="java.util.Map" parameterType="com.ly.model.StudentVo">
select * from tb_stu
<where>
<if test="null != min and min != ''">
<![CDATA[ and sid > #{min} ]]>
</if>
<if test="null != max and max != ''">
<![CDATA[ and sid < #{max} ]]>
</if>
</where>
</select>
// 处理特殊字符的方式
List<Map> list6(StudentVo studentVo);
List<Map> list7(StudentVo studentVo);
@Test
public void sqlSpecical() {
StudentVo studentVo = new StudentVo();
studentVo.setMax(6);
studentVo.setMin(2);
// List<Map> list = this.stuService.list6(studentVo);
List<Map> list = this.stuService.list7(studentVo);
for (Map map : list) {
System.out.println(map);
}
}