Hello World

吞风吻雨葬落日 欺山赶海踏雪径

0%

SpringBoot 接入Mybatis-Plus

应用接入 Mybatis-Plus,做个简单记录。

Mybatis-Plus 官网

增加pom依赖

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

定义数据源

1
2
3
4
5
6
7
@Configuration
public class DataSourceConfig {
@Bean(name = "dataSource", initMethod = "init", destroyMethod = "destroy")
public DataSource dataSource() {
return xxx;
}
}

为mybatis-plus增加分页插件

1
2
3
4
5
6
7
8
9
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}

增加mapper包扫描

1
2
3
4
5
6
7
@MapperScan({"com.xxx.**.mapper"})
@SpringBootApplication(scanBasePackages = {"com.xxx"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

只需要新增三个实体,分别对应的是dao/mapper/entity

entity 数据库表对应的实体,注意加上@TableName注解指定对应的表明,另外主键最好加上@TableId,注意自增主键需要设置上 type = IdType.AUTO
让插入方法自动回填实体id。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@TableName("t_config")
public class ConfigDo {
/**
* 主键
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 创建时间
*/
private Date gmtCreate;
/**
* 修改时间
*/
private Date gmtModified;

......

mapper 需要继承BaseMapper 并指定实体类型,空接口即可。

1
2
3
public interface ConfigMapper extends BaseMapper<ConfigDo> {
// 扩展方法定义在此 ,注意加上 @Mapper 同时 在 resources 下的mapper中 配置对应的xml
}

如果需要扩展方法,需要加上 @Mapper注解,同时在 resources/mapper下配置对应xml 。

dao 简化直接继承 ServiceImpl ,因为 ServiceImpl 已经实现了 IService<T>接口,所以不需要额外增加接口了。

1
2
3
@Component
public class ConfigDao extends ServiceImpl<ConfigMapper, ConfigDo> {
}

最终暴露出dao提供给service使用

1
2
@Resource
ConfigDao configDao;

增加 MetaObjectHandler 自动填充创建与更新时间:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* 插入和更新自动填充的处理
*/
@Component
public class DateTypeMetaObjetHandler implements MetaObjectHandler {

/**
* The constant GMT_CREATE.
*/
private static final String GMT_CREATE = "gmtCreate";

/**
* The constant GMT_MODIFIED.
*/
private static final String GMT_MODIFIED = "gmtModified";


private static final String ENV = "env";

@Resource
AppEnvManger appEnvManger;

/**
* 插入的时候
* @param metaObject 元对象
*/
@Override
public void insertFill(MetaObject metaObject) {
Date now = new Date();
this.strictInsertFill(metaObject, GMT_CREATE, Date.class, now)
.strictInsertFill(metaObject, GMT_MODIFIED, Date.class, now)
.strictInsertFill(metaObject,ENV,String.class,appEnvManger.getDataBaseEnvTag());
}

/**
* 更新的时候
* @param metaObject 元对象
*/
@Override
public void updateFill(MetaObject metaObject) {
Date now = new Date();
this.strictUpdateFill(metaObject, GMT_MODIFIED, Date.class, now);
}
}

DO 层面只需要增加注解

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 创建时间
*/

@TableField(fill = FieldFill.INSERT)
private Date gmtCreate;

/**
* 修改时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date gmtModified;