SpringBoot整合MyBatis-Plus
MyBatis-Plus是国人开发的一个ORM框架,方便开发人员不用写SQL语句完成一些简单的数据库操作。
之前在学习springboot的时候,我操作数据库用的是Spring Data JPA这个是spring官方的一个ORM框架,直白的说,为什么不用JPA的原因,就是英语不好,对于官方的文档看不懂,然后我就选择了MyBatis-Plus。
使用maven进行安装:
<!--mybatis Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
这是所需要的依赖库。
配置application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/island-java?useSSL=false&serverTimezone=GMT%2B8
username: root
password: password
至此,基础配置已经完成了。
创建一个Mapper接口:
public interface UserMapper extends BaseMapper<UserModel> {
}
再创建一个User模型:
@Data
@TableName(value = "user")
public class UserModel {
@TableId(value = "id",type = IdType.AUTO)
private Long id;
private String nickname;
private String email;
private String password;
private String openid;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
private LocalDateTime deletedAt;
}
然后在启动类上添加@MapperScan
@SpringBootApplication
@MapperScan("com.example.demo.Dao") // UserMapper所在包
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
这样就可以使用MyBatis-Plus了。