MyBatis Plus Database Connection Guide

MyBatis Plus is an ORM framework based on MyBatis, which simplifies the process of connecting to and operating with databases. Below are the steps to connect to a database:

Add dependency: Add MyBatis Plus dependency to the project’s pom.xml file. You can search for mybatis-plus-boot-starter in the Maven Central Repository and add it to the project dependencies.

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>最新版本</version>
</dependency>

Configure data source: Configure a data source in the project’s configuration file, using any data source that conforms to the JDBC specification, such as Druid or HikariCP.

3. Setting up MyBatis Plus: Add configuration options for MyBatis Plus in the project’s configuration file.

# 数据库类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
# 数据库驱动
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

# MyBatis Plus 配置项
# 指定 MyBatis Plus 的 mapper 扫描路径
mybatis-plus.mapper-locations=classpath*:mapper/**/*.xml
# 实体类扫描路径
mybatis-plus.type-aliases-package=com.example.entity
# MyBatis Plus 日志配置
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

Create entity classes: create classes corresponding to database tables, and use annotations such as @TableName to specify the table name and @TableField to specify the field names in the database.

@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

Write the Mapper interface: Create a Mapper interface for the corresponding entity class. By inheriting the BaseMapper interface, you can easily access common database operations methods.

public interface UserMapper extends BaseMapper<User> {

}

Usage: Inject the corresponding Mapper interface where database operations are needed, and you can use the database operation methods provided by MyBatis Plus.

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

    @Override
    public User getUserById(Long userId) {
        return userMapper.selectById(userId);
    }

    @Override
    public List<User> getUserList() {
        return userMapper.selectList(null);
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int deleteUser(Long userId) {
        return userMapper.deleteById(userId);
    }
}

These are the fundamental steps for connecting to a database using MyBatis Plus: configuring the data source and MyBatis Plus settings, and then using the corresponding Mapper interface to perform database operations.

bannerAds