数据库准备
1 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加持
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <dependency > <groupId > com.baomidou</groupId > <artifactId > mybatis-plus-boot-starter</artifactId > <version > 3.1.0</version > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > druid-spring-boot-starter</artifactId > <version > 1.1.9</version > </dependency > <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <scope > runtime</scope > <version > 8.0.12</version > </dependency >
主要是 Mybatis Plus、Lombok(不知道Lombok干嘛的?可以看这里 )、Druid连接池 等依赖。
MyBatis Plus配置
项目配置
1 2 3 mybatis-plus: mapper-locations: classpath:/mapper/*Mapper.xml
新增 MyBatis Plus配置类
1 2 3 4 @Configuration @MapperScan("cn.codesheep.springbtmybatisplus.mapper") public class MyBatisConfig {}
看到没,几乎零配置啊,下面就可以写业务逻辑了
业务编写
1 2 3 4 5 6 7 8 @Data @TableName("tbl_user") public class User { @TableId(value = "user_id") private Long userId; private String userName; private Integer userAge; }
1 public interface UserMapper extends BaseMapper <User> {}
这里啥接口方法也不用写,就可以实现增删改查了!
Service接口:
1 2 3 4 5 6 7 public interface UserService extends IService <User> { int insertUser ( User user ) ; int updateUser ( User user ) ; int deleteUser ( User user ) ; User findUserByName ( String userName ) ; IPage getUserPage ( Page page, User user ) ; }
Service实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @Service @AllArgsConstructor public class UserServiceImpl extends ServiceImpl <UserMapper, User> 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 ); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 @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文件一个都不需要写!
最关心的分页问题
1 2 3 4 @Bean public PaginationInterceptor paginationInterceptor () { return new PaginationInterceptor (); }
1 2 3 4 5 6 7 8 public interface UserMapper extends BaseMapper <User> { User getUserByName ( String userName ) ; IPage<List<User>> getUsersPage ( Page page, @Param("query") User user ) ; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 @Service @AllArgsConstructor public class UserServiceImpl extends ServiceImpl <UserMapper, User> 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 ); } }
1 2 3 4 @GetMapping( value = "/page") public Object getUserPage ( Page page, User user ) { return userService.getUserPage( page, user ); }