Configuration of Mybatis and Mybatis-Plus

Table of contents

1. Configuration of Mybatis in springMVC

1. Add dependencies on MyBatis and MyBatis-Spring

 2. Configure data source

3. Configure MyBatis

4. Write Mapper interface and corresponding XML file

 2. YML configuration of mybatis in springnboot

1. Add MyBatis and MyBatis-Spring-Boot-Starter dependencies

2. Configure data source

3. Write Mapper interface and corresponding XML file

4. Write the corresponding XML file userMapper.xml

3. Configuration of mybatis-plus in springmvc

1. Add the dependency of MyBatis-Plus

2. Configure data source

 3. Use the annotations and functions provided by MyBatis-Plus

 4. Run Spring MVC 

4. Configuration of mybatis-plus in springboot

1. Add the dependency of MyBatis-Plus

2. Configure data source

3. Use the annotations and functions provided by MyBatis-Plus

 4. Run Spring MVC 

Console displays sql log


1. Configuration of Mybatis in springMVC

1. Add dependencies on MyBatis and MyBatis-Spring

<dependencies>
    <!-- MyBatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
    <!-- MyBatis-Spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis-spring.version}</version>
    </dependency>
</dependencies>

 2. Configure data source

Add the configuration of the data source in or application.properties. application.ymlFor example, the configuration example using the MySQL database is as follows:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=your_username
spring.datasource.password=your_password

3. Configure MyBatis

application-context.xmlAdd MyBatis configuration information in , the example is as follows :

<!-- MyBatis 配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="com.example.entity"/>
    <property name="mapperLocations" value="classpath:mapper/**/*.xml"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.example.mapper"/>
</bean>

Here, typeAliasesPackagespecify the package name where the entity class is located, and mapperLocationsspecify the location of the Mapper file. MapperScannerConfigurerUsed to specify the package name where the Mapper interface is located. Here com.example.mapperis the package name of the example. You need to replace it with the actual package name.

4. Write Mapper interface and corresponding XML file

Define Mapper interface

// 定义 Mapper 接口
public interface UserMapper {
    @Select("SELECT * FROM user WHERE id = #{id}")
    public User getUserById(int id);
}

The corresponding XML file userMapper.xml

// 对应的 XML 文件 userMapper.xml
<?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.example.mapper.UserMapper">
    <select id="getUserById" resultType="com.example.entity.User">
        SELECT * FROM user WHERE id = #{id}
    </select>
</mapper>

 2. YML configuration of mybatis in springnboot

1. Add MyBatis and MyBatis-Spring-Boot-Starter dependencies

<dependencies>
    <!-- MyBatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>
    <!-- MyBatis-Spring-Boot-Starter -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>${mybatis.spring.boot.starter.version}</version>
    </dependency>
</dependencies>

2. Configure data source

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=your_username
spring.datasource.password=your_password

3. Write Mapper interface and corresponding XML file

// 定义 Mapper 接口
@Mapper
public interface UserMapper {
    @Select("SELECT * FROM user WHERE id = #{id}")
    User getUserById(int id);
}

4. Write the corresponding XML file userMapper.xml

// 对应的 XML 文件 userMapper.xml
<?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.example.mapper.UserMapper">
    <select id="getUserById" resultType="com.example.entity.User">
        SELECT * FROM user WHERE id = #{id}
    </select>
</mapper>

3. Configuration of mybatis-plus in springmvc

1. Add the dependency of MyBatis-Plus

<dependencies>
    <!-- MyBatis-Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${mybatis.plus.version}</version>
    </dependency>
</dependencies>

2. Configure data source

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=your_username
spring.datasource.password=your_password

# 配置MyBatis-Plus属性
mybatis-plus.mapper-locations=classpath*:/mapper/**/*Mapper.xml
mybatis-plus.type-enums-package=com.example.enums

 3. Use the annotations and functions provided by MyBatis-Plus

// 定义实体类
@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String username;
    private String password;
    private Integer age;
}
// 定义 Mapper 接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 可以直接使用继承自 BaseMapper 的方法,不需要编写具体的 SQL 语句
}

 4. Run Spring MVC 

Note: MyBatis-Plus will map to the corresponding table in the database according to the naming rules of the entity class by default. If the entity class and table name are inconsistent, you can use @TableNameannotations to specify. At the same time, @TableIdthe annotation specifies the primary key generation strategy.

4. Configuration of mybatis-plus in springboot

1. Add the dependency of MyBatis-Plus

<dependencies>
    <!-- MyBatis-Plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>${mybatis.plus.version}</version>
    </dependency>
</dependencies>

2. Configure data source

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/my_database?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
spring.datasource.username=your_username
spring.datasource.password=your_password

# 配置MyBatis-Plus属性
mybatis-plus.mapper-locations=classpath*:/mapper/**/*Mapper.xml
mybatis-plus.type-enums-package=com.example.enums

3. Use the annotations and functions provided by MyBatis-Plus

// 定义实体类
@Data
@TableName("user")
public class User {
    @TableId(type = IdType.AUTO)
    private Long id;
    private String username;
    private String password;
    private Integer age;
}
// 定义 Mapper 接口
@Mapper
public interface UserMapper extends BaseMapper<User> {
    // 可以直接使用继承自 BaseMapper 的方法,不需要编写具体的 SQL 语句
}

 4. Run Spring MVC 

Note: MyBatis-Plus will map to the corresponding table in the database according to the naming rules of the entity class by default. If the entity class and table name are inconsistent, you can use @TableNameannotations to specify. At the same time, @TableIdthe annotation specifies the primary key generation strategy.

Console displays sql log

 Springboot configures mybatis console to display sql log, inapplication.properties

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

Springboot configures mybatis-plus console to display sql log, inapplication.properties

logging.level.com.baomidou.mybatisplus=DEBUG
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

Guess you like

Origin blog.csdn.net/qi341500/article/details/132655395