目录
1 查询操作
1.1 接口方法定义
1.2 映射文件配置
1.3 UserService
1.4 UserController
2 添加操作
2.1 接口方式定义
2.2 映射文件配置
2.3 添加 commons-lang3 依赖
2.4 AssertUtil ⼯具类
2.5 ParamsException ⾃定义异常
2.6 UserService
2.7 ResultInfo
2.8 UserController
3 修改操作
3.1 接口方法定义
3.2 映射文件配置
3.3 UserService
3.4 UserController
4 删除操作
4.1 接口方法定义
4.2 映射文件配置
4.3 UserService
4.4 UserController
5 分页条件查询操作
5.1 UserQuery
5.2 接口方法定义
5.3 映射文件配置
5.4 UserService
5.5 UserController
6 PostMan 工具下载与使用
完成 SpringBoot 与 Mybatis 集成后,以⽤户表为例实现⼀套⽤户模块基本数据维护。
1 查询操作
1.1 接口方法定义
UserMapper 接⼝添加查询的⽅法:
public interface UserMapper {
// 通过⽤户ID查询⽤户
public User queryById(Integer id);
}
1.2 映射文件配置
<?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="com.xxxx.springboot.dao.UserMapper">
<select id="queryById" parameterType="int"
resultType="com.xxxx.springboot.po.User">
select
*
from
tb_user
where
id = #{id,jdbcType=INTEGER}
</select>
</mapper>
1.3 UserService
@Service
public class UserService {
@Resource
private UserMapper userMapper;
/**
* 通过⽤户ID查询⽤户
* @param id
* @return
*/
public User queryById(Integer id){
return userMapper.queryById(id);
}
}
1.4 UserController
@RestController
public class UserController {
@Resource
private UserService userService;
/**
* 根据⽤户ID查询⽤户对象
* @param userId
* @return
*/
@GetMapping("user/{userId}")
public User queryUserByUserId(@PathVariable Integer userId){
return userService.queryById(userId);
}
}
2 添加操作
2.1 接口方式定义
public interface UserMapper {
// 添加⽤户
public int save(User user);
}
2.2 映射文件配置
<?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="com.xxxx.springboot.dao.UserMapper">
<insert id="save" parameterType="com.xxxx.springboot.po.User">
insert into
tb_user
(user_name,user_pwd)
values
(#{userName},#{userPwd})
</insert>
</mapper>
2.3 添加 commons-lang3 依赖
如果需要使⽤ StringUtils ⼯具类,需要引⼊ commons-lang3 依赖。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
2.4 AssertUtil ⼯具类
package com.xxxx.springboot.utils;
import com.xxxx.springboot.exceptions.ParamsException;
public class AssertUtil {
/**
* 判断结果是否为true
* 如果结果为true,抛出异常
* @param userName
* @return
*/
public static void isTrue(Boolean flag, String msg){
if(flag){
throw new ParamsException(msg);
}
}
}
2.5 ParamsException ⾃定义异常
package com.xxxx.springboot.exceptions;
/**
* ⾃定义参数异常
*/
public class ParamsException extends RuntimeException {
private Integer code = 300;
private String msg = "参数异常!";
public ParamsException() {
super("参数异常!");
}
public ParamsException(String msg) {
super(msg);
this.msg = msg;
}
public ParamsException(Integer code) {
super("参数异常!");
this.code = code;
}
public ParamsException(Integer code, String msg) {
super(msg);
this.code = code;
this.msg = msg;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
2.6 UserService
@Service
public class UserService {
@Resource
private UserMapper userMapper;
/**
* 添加⽤户
* @param user
*/
public void saveUser(User user) {
AssertUtil.isTrue(StringUtils.isBlank(user.getUserName()), "⽤户名不能为
空!");
AssertUtil.isTrue(StringUtils.isBlank(user.getUserPwd()),"⽤户密码不能为
空!");
User temp = userMapper.queryUserByUserName(user.getUserName());
AssertUtil.isTrue(null != temp, "该⽤户已存在!");
AssertUtil.isTrue(userMapper.save(user) < 1,"⽤户记录添加失败!");
}
}
2.7 ResultInfo
package com.xxxx.springboot.po.vo;
public class ResultInfo {
private Integer code = 200;
private String msg = "操作成功";
private Object result;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getResult() {
return result;
}
public void setResult(Object result) {
this.result = result;
}
}
2.8 UserController
@RestController
public class UserController {
@Resource
private UserService userService;
/**
* 添加⽤户
* @param user
* @return
*/
@PutMapping("user")
public ResultInfo saveUser(@RequestBody User user){
ResultInfo resultInfo = new ResultInfo();
try {
userService.saveUser(user);
} catch (ParamsException e) {
e.printStackTrace();
resultInfo.setCode(e.getCode());
resultInfo.setMsg(e.getMsg());
}catch (Exception e) {
e.printStackTrace();
resultInfo.setCode(300);
resultInfo.setMsg("记录添加失败!");
}
return resultInfo;
}
}
3 修改操作
3.1 接口方法定义
public interface UserMapper {
// 修改⽤户
public int update(User user);
}
3.2 映射文件配置
<?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="com.xxxx.springboot.dao.UserMapper">
<update id="update" parameterType="com.xxxx.springboot.po.User">
update
tb_user
set
user_name =#{userName},user_pwd=#{userPwd}
where
id = #{id}
</update>
</mapper>
3.3 UserService
@Service
public class UserService {
@Resource
private UserMapper userMapper;
/**
* 修改⽤户
* @param user
*/
public void updateUser(User user) {
AssertUtil.isTrue(StringUtils.isBlank(user.getUserName()), "⽤户名不能为
空!");
AssertUtil.isTrue(StringUtils.isBlank(user.getUserPwd()),"⽤户密码不能为
空!");
User temp = userMapper.queryUserByUserName(user.getUserName());
AssertUtil.isTrue(null != temp && !
(temp.getId().equals(user.getId())), "该⽤户已存在!");
AssertUtil.isTrue(userMapper.update(user) < 1,"⽤户记录添加失败!");
}
}
3.4 UserController
@RestController
public class UserController {
@Resource
private UserService userService;
/**
* 修改⽤户
* @param user
* @return
*/
@PostMapping("user")
public ResultInfo updateUser(@RequestBody User user){
ResultInfo resultInfo = new ResultInfo();
try {
userService.updateUser(user);
} catch (ParamsException e) {
e.printStackTrace();
resultInfo.setCode(e.getCode());
resultInfo.setMsg(e.getMsg());
}catch (Exception e) {
e.printStackTrace();
resultInfo.setCode(300);
resultInfo.setMsg("记录更新失败!");
}
return resultInfo;
}
}
4 删除操作
4.1 接口方法定义
public interface UserMapper {
// 删除⽤户
public int deleteUserById(Integer id);
}
4.2 映射文件配置
<?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="com.xxxx.springboot.dao.UserMapper">
<delete id="deleteUserById" parameterType="int">
delete from
tb_user
where
id=#{id}
</delete>
</mapper>
4.3 UserService
@Service
public class UserService {
@Resource
private UserMapper userMapper;
/**
* 删除⽤户
* @param id
*/
public void deleteUser(Integer id){
AssertUtil.isTrue(null == id || null == userMapper.queryById(id),"待删
除记录不存在!");
AssertUtil.isTrue(userMapper.deleteUserById(id)<1,"⽤户删除失败!");
}
}
4.4 UserController
@RestController
public class UserController {
@Resource
private UserService userService;
/**
* 删除⽤户
* @param userId
* @return
*/
@DeleteMapping("user/{userId}")
public ResultInfo deleteUser(@PathVariable Integer userId){
ResultInfo resultInfo = new ResultInfo();
try {
userService.deleteUser(userId);
} catch (ParamsException e) {
e.printStackTrace();
resultInfo.setCode(e.getCode());
resultInfo.setMsg(e.getMsg());
}catch (Exception e) {
e.printStackTrace();
resultInfo.setCode(300);
resultInfo.setMsg("记录删除失败!");
}
return resultInfo;
}
}
5 分页条件查询操作
5.1 UserQuery
package com.xxxx.springboot.query;
public class UserQuery {
private Integer pageNum = 1; // 当前⻚
private Integer pageSize = 10; // 每⻚显示的数量
private String userName; // 查询条件:⽤户名
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
5.2 接口方法定义
public interface UserMapper {
// 通过条件,分⻚查询⽤户列表
public List<User> selectByParams(UserQuery userQuery);
}
5.3 映射文件配置
<?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="com.xxxx.springboot.dao.UserMapper">
<select id="selectByParams"
parameterType="com.xxxx.springboot.query.UserQuery"
resultType="com.xxxx.springboot.po.User">
select
*
from
tb_user
<where>
<if test="null != userName and userName !=''">
and user_name like concat('%',#{userName},'%')
</if>
</where>
</select>
</mapper>
5.4 UserService
@Service
public class UserService {
@Resource
private UserMapper userMapper;
/**
* 通过指定参数,分⻚查询⽤户列表
* @param userQuery
* @return
*/
public PageInfo<User> queryUserByParams(UserQuery userQuery){
PageHelper.startPage(userQuery.getPageNum(),userQuery.getPageSize());
return new PageInfo<User>(userMapper.selectByParams(userQuery));
}
}
5.5 UserController
@RestController
public class UserController {
@Resource
private UserService userService;
/**
* 通过指定参数,分⻚查询⽤户列表
* @param userQuery
* @return
*/
@GetMapping("user/list")
public PageInfo<User> list(UserQuery userQuery){
return userService.queryUserByParams(userQuery);
}
}
6 PostMan 工具下载与使用
在企业 web 应⽤开发中,对服务器端接⼝进⾏测试,通常借助接⼝测试⼯具,这⾥使⽤ Postman 接⼝测试⼯具来对后台 restful 接⼝进⾏测试。
Postman ⼯具下载地址 :http:// https://www.getpostman.com/apps,选中对应平台下载即可。
下载安装后,启动 Postman 根据后台接⼝地址发送响应请求即可对接⼝进⾏测试。