com.baomidou
mybatis-plus-boot-starter
3.3.2
(3)指定数据库的驱动
mysql
mysql-connector-java
runtime
- 在application.yml中进行配置数据库(数据库名plus)
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/plus?useSSL=false&serverTimezone=UTC
username: root
password: root
(4)创建实体类 定义属性 指定主键的类型
public class User {
/**
-
指定主键的方式
-
value:主键字段的名称,如果是id可以不用写
-
type:指定主键的类型,主键的值如何生成:IdType.AUTO表示自动增长
*/
@TableId(value =“id”,type = IdType.AUTO)
private Integer id;
private String name;
private String email;
private Integer age;
(5)创建dao接口,需要继承BaseMapper<实体.class>
/**
-
@author 王恒杰
-
@Description:
-
自定义的mapper,dao接口
-
1.实现BaseMapper
-
2.要指定实体类对象
-
BaseMapper是mybatis-plus的对象,定义了17个方法(crud)
*/
public interface UserMapper extends BaseMapper {
}
(6)在springboot的启动类上,加入@MappperScan(value=“指定dao接口的包名”);
/**
-
@author 王恒杰
-
@MapperScan:扫描器,指定Mapper所在的包名
*/
@SpringBootApplication
@MapperScan(value = “com.tjcu.mapper”)
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
(7)测试使用
在测试类或Service注入Dao接口,框架使用动态代理 创建Dao的实现类对象 。
调用