preface

In the article “MyBatis use and lack of thinking”, pointed out the use of MyBatis insufficiency. One of them is that each entity needs to write, add, delete, change and query SQL, which feels like repetitive work.

If you are not familiar with the use of MyBatis, you can first check the above article.

This paper will solve this problem by using MyBatis-Plus, which is an enhancement tool of MyBatis. On the basis of MyBatis, it only enhances and does not change, so as to simplify development and improve efficiency.

The specific implementation

Maven rely on

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.2 rainfall distribution on 10-12</version>
</dependency>
Copy the code

The persistence layer

@Mapper
public interface UserDao extends BaseMapper<User> {}Copy the code

Yes, you read that right, you don’t have to define any interface methods, you don’t have to write any SQL statements.

Just inherit the interface BaseMapper, and generics are entity classes.

The service layer

public interface UserService extends IService<User> {}Copy the code
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserDao.User> implements UserService {}Copy the code

Similarly, service layer interfaces inherit from IService, and generics are entity classes. The service layer implementation class inherits from ServiceImpl, and the generics are the Dao interface and the entity class.

Entity class

@TableName("sys_user")
@Getter
@Setter
public class User extends Common {

    @TableId(type = IdType.AUTO)
    private Long id;

    private String account;

    private String password;

    private String nickname;

    private String email;

    private String phone;
}
Copy the code
  • @tablename Specifies the name of the corresponding database table
  • @tableId (type = idtype.auto) specifies the unique ID and the auto-increment type

At this point, you are ready to invoke the interface from the control layer.

Control layer

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public ApiResult getUser(@PathVariable("id") Long id) {
        return ApiResult.success(userService.getById(id));
    }

    @PostMapping
    public ApiResult addUser(@RequestBody @Valid User user) {
        setCreateInfo(user);
        return ApiResult.success(userService.save(user));
    }

    @DeleteMapping("/{id}")
    public ApiResult deleteUser(@PathVariable("id") Long id) {
        return ApiResult.success(userService.removeById(id));
    }

    @PutMapping
    public ApiResult updateUser(@RequestBody @Valid User user) {
        setModifyInfo(user);
        returnApiResult.success(userService.updateById(user)); }}Copy the code

Unit testing

In the MockMvc Unit Testing article, you simulated the interface through MockMvc.

Here we can directly use the previous test case and execute the result:

conclusion

Development is greatly simplified by using MyBatis-Plus, which is a boon for lazy people!

Although the default implementation only supports simple CURD, complex SQL needs to be implemented yourself, but this saves a lot of work.

Above, thanks for reading, if you feel helpful, might as well click a like!

The source code

Github.com/zhuqianchan…

Review past

  • Build backend frameworks from scratch – keep updating