在mybatis的xml中使用枚举

有一个业务的查询有很多状态,需要判断不同的状态参数来动态拼接SQL,但是如果写死状态在xml中会很不得体,所以改成用枚举获取判断。简单的写个Demo学习一下

pom.xml中引入lombokmybatis-plus

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
枚举状态
1
2
3
4
5
6
7
8
9
10
11
12
13
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum StatusEnum {

ONE(1,"demo one"),
TWO(2,"demo two");

private final Integer code;
private final String description;
}

数据库实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@TableName("sys_user")
@Data
public class SysUser {
private Long userId;
private Long deptId;
private String loginName;
private String userName;
private String userType;
private String email;
}

Controller

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import common.Result;
import entity.SysUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/sysUser")
public class SysUserController {

@Resource
private SysUserMapper sysUserMapper;

@GetMapping("/getSysUserByCode")
public Result getSysUserByCode(@RequestParam("code") Integer code){
SysUser sysUser = sysUserMapper.selectUserWithEnumJudge(code);
return Result.ok(sysUser);
}
}

Mapper

1
2
3
4
5
6
7
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import entity.SysUser;
import org.apache.ibatis.annotations.Param;

public interface SysUserMapper extends BaseMapper<SysUser> {
SysUser selectUserWithEnumJudge(@Param("code") Integer code);
}

xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="options.mybatis.SysUserMapper">

<select id="selectUserWithEnumJudge" resultType="entity.SysUser">
select user_id,dept_id,login_name,user_name,user_type,email from sys_user
<where>
<if test="code != null and code == @enums.StatusEnum@ONE.getCode()">
and user_name = 'admin'
</if>
<if test="code != null and code == @enums.StatusEnum@TWO.getCode()">
and user_name = 'fix'
</if>
</where>
</select>

</mapper>

数据库记录

请求接口

结果:

1
2
3
4
5
6
7
8
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@667d3020] was not registered for synchronization because synchronization is not active
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3e784cb9] will not be managed by Spring
==> Preparing: select user_id,dept_id,login_name,user_name,user_type,email from sys_user WHERE user_name = 'admin'
==> Parameters:
<== Columns: user_id, dept_id, login_name, user_name, user_type, email
<== Row: 1, 103, admin, admin, 00, admin@admin.com
<== Total: 1

再次请求

结果:

1
2
3
4
5
6
7
8
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5470f3e5] was not registered for synchronization because synchronization is not active
JDBC Connection [com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl@3e784cb9] will not be managed by Spring
==> Preparing: select user_id,dept_id,login_name,user_name,user_type,email from sys_user WHERE user_name = 'fix'
==> Parameters:
<== Columns: user_id, dept_id, login_name, user_name, user_type, email
<== Row: 2, 105, fix, fix, 00, fix@fix.com
<== Total: 1

尝试两次不同的请求,都是生效的