mybatisPlus Basics

Generic Mapper interface

  • The Mapper interface inherits from the BaseMapper interface, and the BaseMapper interface provides many operation methods related to addition, deletion, modification, and query of a single table. In this chapter, some methods in the Mapper interface are introduced.

Simply insert data -insert

  • Insert a piece of data
@Test
void insert(){
    
    
    User user = new User();
    user.setId(6L);
    user.setName("Mike");
    user.setAge(33);
    user.setEmail("[email protected]");
    userMapper.insert(user);
}

Simply delete data - deleteById

  • Delete data by id
@Test
void deleteOne(){
    
    
    userMapper.deleteById(6L);
}

Simply modify data - updateById

  • Test to modify all data
@Test
void updateById(){
    
    
    User user = new User();
    user.setId(6L);
    user.setName("迈克");
    user.setAge(35);
    user.setEmail("[email protected]");
    userMapper.updateById(user);
}
  • Test to modify some data
@Test
void updateById(){
    
    
    User user = new User();
    user.setId(6L);
    user.setName("Mike");
    user.setAge(35);
    user.setEmail("[email protected]");
    userMapper.updateById(user);
}

Simple query data - selectById

  1. Query by Id
@Test
    void selectById(){
    
    
        User user1 = userMapper.selectById(6L);
        System.out.println(user1);
    }
  1. query all
@Test
void selectList() {
    
    
    List<User> userList = userMapper.selectList(null);
    System.out.println(userList);
}

Common service interface

  • MybatisPlus also provides the IService interface and the corresponding implementation class ServiceImpl. This implementation class has provided some corresponding Service-related methods. In some scenarios, we can directly use the methods provided by ServiceImpl to implement the corresponding functions.

  • The IService interface contains some service-related additions, deletions, modifications, and queries.
    insert image description here

  • The ServiceImpl implementation class provides the implementation of service-related addition, deletion, modification and query methods
    insert image description here

  • The UserService interface inherits from the IService interface

  • The UserServiceImpl class inherits ServiceImpl<UserMapper, User>

  • The relationship is as follows:
    insert image description here

  • Inject the UserService object and test related methods

@Autowired
private UserService userService;

Service interface - insert data

@Test
void insertService(){
    
    
    User user = new User();
    user.setId(7L);
    user.setName("zhangsan");
    user.setAge(35);
    user.setEmail("[email protected]");

    userService.save(user);
}

Service interface - delete data

@Test
void deleteService(){
    
    
    userService.removeById(7L);
}

Service interface - modify data

@Test
void updateService(){
    
    
    User user = new User();
    user.setId(6L);
    user.setAge(40);
    userService.updateById(user);
}

Service interface - query data

@Test
void selectService(){
    
    
    List<User> userList = userService.selectList();
    System.out.println(userList);
}

custom interface method

  • By inheriting the Service interface provided by MybatisPlus, you can not only expand your own service method, but also use some existing service methods
  • In addition to providing us with these rich interface methods, MybatisPlus can also write custom interface methods for our own needs. We can achieve the desired SQL requirements by writing SQL statements ourselves.

Custom Mapper interface method

  • Abstract methods are provided in the Mapper interface
@Mapper
public interface UserMapper extends BaseMapper<User> {
    
    
       User selectByName(String name);
}
  • Provide mapping configuration files and corresponding SQL statements
<?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.powernode.mapper.UserMapper">

     <select id="selectByName" resultType="com.powernode.domain.User">
        select * from user where name = #{value}
     </select>
</mapper>
  • Test the custom Mapper interface method
@Test
void myMethod(){
    
    
    User user = userMapper.selectByName("Jone");
    System.out.println(user);
}
  • The grammatical specification of the custom interface is the same as the grammatical specification of Mybatis written before, so it can be seen that MybatisPlus is non-invasive (the introduction of MybatisPlus will not cause any changes to the original grammar.)

Guess you like

Origin blog.csdn.net/yang2330648064/article/details/131940377