项目结构如下:
注意不需要任何XML文件
1.导入依赖
除了Spring Boot创建时自带的依赖,还需要加入:
<!-- Mybatis Plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</dependency>
<!-- 数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.31</version>
</dependency>
<!-- 辅助工具,可以不添加,在实体类中自行生成Getter/Setter方法-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
driverClassName有这样不同版本对应关系
driverClassName: com.mysql.jdbc.Driver # mysql-connector-java 5.x及之前版本中的驱动类名driverClassName: com.mysql.cj.jdbc.Driver # mysql-connector-java 6.x及后续版本中的驱动类名
2.配置application.yaml文件
spring:datasource:driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/xxxusername: rootpassword: 'xxx'
3.创建实体(entity)
@Data
public class User {private Long id;private String name;private String password;@TableLogic@TableField("is_deleted")private Boolean deleted;private Date creatTime;private Date lastUpdateTime;
}
4.创建数据访问层(mapper)对象
public interface UserMapper extends BaseMapper<User> {
}
5.在启动类添加扫描注解
@MapperScan("com.example.mapper")