写在前面
MyBatis 的增强方案确实有不少,甚至有种感觉是现在如果只用 “裸MyBatis”,不来点增强插件都不好意思了。前两天尝试了一下 MyBatis Generator。这次来点更加先进的 Mybatis-Plus,SQL语句都不用写了,分页也是自动完成,嗯,真香!
数据库准备
CREATE TABLE tbl_user
(
user_id BIGINT(20) NOT NULL COMMENT '主键ID',
user_name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
user_age INT(11) NULL DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (user_id)
) charset = utf8;
MyBatis-Plus加持
com.baomidou
mybatis-plus-boot-starter
3.1.0
org.projectlombok
lombok
com.alibaba
druid-spring-boot-starter
1.1.9
mysql
mysql-connector-java
runtime
8.0.12
主要是 Mybatis Plus、Lombok(不知道Lombok干嘛的?可以看这里)、Druid连接池 等依赖。
项目配置
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
新增 MyBatis Plus配置类
@Configuration
@MapperScan("cn.codesheep.springbtmybatisplus.mapper")
public class MyBatisConfig {
}
看到没,几乎零配置啊,下面就可以写业务逻辑了
业务编写
@Data
@TableName("tbl_user")
public class User {
@TableId(value = "user_id")
private Long userId;
private String userName;
private Integer userAge;
}
public interface UserMapper extends BaseMapper {
}
这里啥接口方法也不用写,就可以实现增删改查了!
Service接口:
public interface UserService extends IService {
int insertUser( User user );
int updateUser( User user );
int deleteUser( User user );
User findUserByName( String userName );
IPage getUserPage( Page page, User user );
}
Service实现:
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl implements UserService {
// 增
@Override
public int insertUser(User user) {
return baseMapper.insert( user );
}
// 改
@Override
public int updateUser(User user) {
return baseMapper.updateById( user );
}
// 删
@Override
public int deleteUser(User user) {
return baseMapper.deleteById( user.getUserId() );
}
// 查
@Override
public User findUserByName( String userName ) {
return baseMapper.getUserByName( userName );
}
}
@RestController
@RequestMapping("/user")
public class UserContorller {
@Autowired
private UserService userService;
// 增
@PostMapping( value = "/insert")
public Object insert( @RequestBody User user ) {
return userService.insertUser( user );
}
// 改
@PostMapping( value = "/update")
public Object update( @RequestBody User user ) {
return userService.updateUser( user );
}
// 删
@PostMapping( value = "/delete")
public Object delete( @RequestBody User user ) {
return userService.deleteUser( user );
}
// 查
@GetMapping( value = "/getUserByName")
public Object getUserByName( @RequestParam String userName ) {
return userService.findUserByName( userName );
}
}
通过以上几个简单的步骤,我们就实现了 tbl_user表的增删改查,传统 MyBatis的 XML文件一个都不需要写!
实际实验【乡爱加持】
很牛批的 logo就会出现
接下来通过 Postman来发送增删改查的请求
通过 Postman随便插入几条记录 POST
localhost:8089/user/insert
{"userId":3,"userName":"刘能","userAge":"58"}
{"userId":4,"userName":"赵四","userAge":"58"}
{"userId":5,"userName":"谢广坤","userAge":"58"}
{"userId":6,"userName":"刘大脑袋","userAge":"58"}
修改记录时需要带用户ID,比如我们修改 赵四 那条记录的名字为 赵四(ZhaoFour)
修改记录时同样需要带用户ID,比如删除ID=6 那条 刘大脑袋的记录
比如,按照名字来查询: GET
localhost:8089/user/getUserByName?userName=刘能
最关心的分页问题
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
public interface UserMapper extends BaseMapper {
// 普通查询
User getUserByName( String userName );
// 分页查询
IPage> getUsersPage( Page page, @Param("query") User user );
}
@Service
@AllArgsConstructor
public class UserServiceImpl extends ServiceImpl implements UserService {
// 查:普通查
@Override
public User findUserByName( String userName ) {
return baseMapper.getUserByName( userName );
}
// 分页查
@Override
public IPage getUserPage(Page page, User user) {
return baseMapper.getUsersPage( page, user );
}
}
@GetMapping( value = "/page")
public Object getUserPage( Page page, User user ) {
return userService.getUserPage( page, user );
}
实际实验一下,我们分页查询 年龄=58 的多条记录:
可以看到结果数据中,除了给到当前页数据,还把总记录条数,总页数等一并返回了,很是优雅呢 !