SpringBoot uses MybatisPlus

Foreword: MybatisPlus is an enhancement tool for MyBatis. On the basis of MyBatis, only enhancements are made without changes, and it is born to simplify development and improve efficiency.


One, import pom

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.3.0</version>
</dependency>

Two, placement

Configure annotations in the entry file
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")

@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(QuickStartApplication.class, args);
    }
}

Three, mapper needs to inherit BaseMapper

public interface User2Mapper extends BaseMapper<User> {
    
    
}

Four, use

When used in service, automatically assemble the corresponding mapper, and then use it

@Service
public class UserServiceImpl implements UserService {
    
    
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
    
    
        System.out.println("使用了MybatisPlus形式编写");
        return userMapper.selectList(null);
    }

The related origin and more detailed use of MybatisPlus is not the focus of this article, you can check the official website .

Some good cases and usage skills can be viewed on the blog .

Guess you like

Origin blog.csdn.net/weixin_42656358/article/details/108725265