MyBatisPlus基础环境搭建

2023-12-15 19:25:14

1 开发环境

IDE:IntelliJ IDEA 2022.2.1 (Ultimate Edition)
构建工具:maven 3.6.1
MySQL版本:MySQL 5.7
MyBatisPlus版本:MyBatisPlus 3.5.4.1
SpringBoot版本:3.2.0

2 工程创建

创建一个Maven工程giser-java-mybatis-plus-learning
基础依赖如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version>
    <relativePath/>
</parent>
<properties>
    <java.version>1.8</java.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.33</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!--
        mybatis-plus-boot-starter中自带的mybatis-spring版本太低,和springboot3.2不兼容,需要升级mybatis-spring版本
        error不兼容提示:Invalid value type for attribute ‘factoryBeanObjectType‘: java.lang.String
     -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.5.4.1</version>
        <exclusions>
            <exclusion>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>3.0.3</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

application.yml配置

spring:
  datasource:
    # 配置数据源类型
    type: com.zaxxer.hikari.HikariDataSource
    # 配置连接数据库信息
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db_mybatis_plus?characterEncoding=utf-8&useSSL=false
    username: root
    password: root

# mybatis-plus配置
mybatis-plus:
  configuration:
    # 引入日志
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3 基础测试

public interface UserMapper extends BaseMapper<User> {
}
@Data
public class User {
    private Long id;
    private String name;
    private Integer age;
    private String email;
}
@Slf4j
@SpringBootTest
class GiserJavaMybatisPlusLearningApplicationTests {

    @Resource
    private UserMapper userMapper;

    @Test
    void contextLoads() {
        List<User> userList = userMapper.selectList(null);
        log.info("查询结果为:{}", userList);
    }

}

文章来源:https://blog.csdn.net/SUNBOYmxbsH/article/details/134935733
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。