mybatis动态sql
if
如果 name 不为空,就进行if体的拼接
<if test="name != null" >
#{name,jdbcType=VARCHAR}, <! -- 这就是if体 -->
</if>
trim
一样的sql语句拼接:prefix前缀,suffi 后缀。suffixOverrides 后缀覆盖
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="pwd != null" >
pwd,
</if>
</trim>
foreach
foreach 就是循环的意思,collection代表要被循环参数集合。open和close代表开始和结束拼接字符串。separator代表item之间的分割符。item就是当前正在循环的变量定义。就当java中的foreach看就能看懂了
<select id="selectByIn" resultType="com.cpc.model.User" parameterType="java.util.List">
select * from user where id in
<foreach collection="userIds" open="(" close=")" separator="," item="uid">
#{uid}
</foreach>
</select>
List<User> selectByIn(@Param("userIds") List userIds);
测试:
模糊查询
这里演示 3种 like查询的方式
接口:
List<User> selectBylike1(@Param("uname") String uname);
List<User> selectBylike2(@Param("uname") String uname);
List<User> selectBylike3(@Param("uname") String uname);
映射文件:
<!--推荐使用这种-->
<select id="selectBylike1" resultType="com.cpc.model.User" parameterType="java.lang.String">
select * from user where name like #{uname}
</select>
<!--此方式不会自动加 双引号,存在sql注入的风险-->
<select id="selectBylike2" resultType="com.cpc.model.User" parameterType="java.lang.String">
select * from user where name like '${uname}'
</select>
<!--这种方式也能实现,不过比较麻烦,一般每谁去使用 -->
<select id="selectBylike3" resultType="com.cpc.model.User" parameterType="java.lang.String">
select * from user where name like concat('%', concat('${uname}', '%'))
</select>
注意:#{…}自带引号,${…}有sql注入的风险
测试:
@Test
public void selectByLike(){
// List<User> list = this.userService.selectBylike1(StringUtil.toLikeStr("张"));
// List<User> list = this.userService.selectBylike2(StringUtil.toLikeStr("张"));
List<User> list = this.userService.selectBylike3("张");
for (User user : list) {
System.out.println(user);
}
}
ps:StringUtil.toLikeStr 方法就是拼接 百分号
查询返回结果集的处理
resultMap:适合使用返回值是自定义实体类的情况
resultType:适合使用返回值的数据类型是非自定义的,即jdk的提供的类型
下面会使用到这个BaseResultMap
<resultMap id="BaseResultMap" type="com.cpc.model.User" >
<constructor >
<idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="name" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="pwd" jdbcType="VARCHAR" javaType="java.lang.String" />
</constructor>
</resultMap>
UserVo 用来存放包括数据库表映射字段以及多余查询条件所用到的属性
public class UserVo extends User{
private List<String> userIds;
private int max;
private int min;
public int getMin() {
return min;
}
public void setMin(int min) {
this.min = min;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public List<String> getUserIds() {
return userIds;
}
public void setUserIds(List<String> userIds) {
this.userIds = userIds;
}
}
使用resultMap返回自定义类型集合
接口:
List<User> list1();
映射文件:
<select id="list1" resultMap="BaseResultMap">
select * from user
</select>
测试:
@Test
public void list(){
List<User> list = this.userService.list1();
for (User user : list) {
System.out.println(user);
}
}
使用resultType返回List
接口:
List<User> list2();
映射文件:
<select id="list2" resultType="com.cpc.model.User">
select * from user
</select>
测试:
@Test
public void list(){
List<User> list = this.userService.list2();
for (User user : list) {
System.out.println(user);
}
}
使用resultType返回单个对象
接口:
User list3(UserVo userVo);
映射文件:
<select id="list3" resultType="com.cpc.model.User" parameterType="com.cpc.model.UserVo">
select * from user where id in
<foreach collection="userIds" open="(" close=")" separator="," item="uid">
#{uid}
</foreach>
</select>
测试:
@Test
public void list(){
UserVo userVo = new UserVo();
List list = new ArrayList<>();
list.add(1);
userVo.setUserIds(list);
User user = this.userService.list3(userVo);
System.out.println(user);
}
使用resultType返回List
适用于多表查询返回结果集
接口:
List<Map> list4(Map map);
映射文件:
<select id="list4" resultType="java.util.Map" parameterType="java.util.Map">
select * from user
<where>
<if test="null != uname and uname != ''">
and name like #{uname}
</if>
</where>
</select>
测试:
@Test
public void list(){
Map map = new HashMap();
map.put("uname", StringUtil.toLikeStr("张"));
List<Map> maps = this.userService.list4(map);
for(Map m : maps){
System.out.println(m);
}
}
使用resultType返回Map<String,Object>
适用于多表查询返回单个结果集
接口:
Map list5(Map map);
映射文件:
<select id="list5" resultType="java.util.Map" parameterType="java.util.Map">
select * from user
<where>
<if test="null != uid and uid != ''">
and id = #{uid}
</if>
</where>
</select>
测试:
@Test
public void list(){
Map map = new HashMap();
map.put("uid", 1);
Map m = this.userService.list5(map);
System.out.println(m);
}
分页查询
使用分页插件步奏
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>
在业务逻辑成中写一个 listPager 的分页方法
@Override
public List<Map> listPager(Map map, PageBean pageBean) {
//如果分页对象不为空就继续分页操作
if(pageBean != null && pageBean.isPagination()){
PageHelper.startPage(pageBean.getPage(), pageBean.getRows());
}
List<Map> list = this.userMapper.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());
}
return list;
}
测试
@Test
public void listPage(){
Map map = new HashMap();
map.put("uname", StringUtil.toLikeStr("张"));
PageBean pageBean = new PageBean();
pageBean.setPage(3);
pageBean.setRows(1);
List<Map> maps = this.userService.listPager(map, pageBean);
for(Map m : maps){
System.out.println(m);
}
}
特殊字符处理
方式一:
大于:>
小于:<
空格:&
接口
List<Map> list6(UserVo userVo);
xml 配置
<select id="list6" resultType="java.util.Map" parameterType="com.cpc.model.UserVo">
select * from user
<where>
<if test="null != min and min != ''">
and id > #{min}
</if>
<if test="null != max and max !=''">
and id < #{max}
</if>
</where>
</select>
测试:
@Test
public void sqlSpecial(){
UserVo userVo = new UserVo();
userVo.setMax(3);
userVo.setMin(1);
List<Map> maps = this.userService.list6(userVo);
for(Map m : maps){
System.out.println(m);
}
}
方式二:
List<Map> list7(UserVo userVo);
xml 配置
<select id="list7" resultType="java.util.Map" parameterType="com.cpc.model.UserVo">
select * from user
<where>
<if test="null != min and min != ''">
<![CDATA[ and id > #{min} ]]>
</if>
<if test="null != max and max !=''">
<![CDATA[ and id < #{max} ]]>
</if>
</where>
</select>
测试:
@Test
public void sqlSpecial(){
UserVo userVo = new UserVo();
userVo.setMax(3);
userVo.setMin(1);
List<Map> maps = this.userService.list7(userVo);
for(Map m : maps){
System.out.println(m);
}
}