使用Mybatis

网友投稿 383 2022-07-22


目录一. 搭建基础项目二. 设置自动填充

一. 搭建基础项目

引入依赖

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

true

io.springfox

springfox-swagger2

2.7.0

com.github.xiaoymin

swagger-bootstrap-ui

1.9.6

com.baomidou

mybatis-plus-boot-starter

3.3.1.tmp

mysql

mysql-connector-java

8.0.26

controller

@RestController

public class TestController {

@Autowired

private IProjectService projectService;

@ApiOperation("新增项目")

@PostMapping("/")

public void addProjectWrite(@RequestBody Project project) {

projectService.save(project);

}

}

service

public interface IProjectService extends IService

}

serviceImpl

@Service

public class ProjectServiceImpl extends ServiceImpl implements IProjectService {

}

Mapper

@Service

public class ProjectServiceImpl extends ServiceImpl implements IProjectService {

}

Pojo

@Data

@EqualsAndHashCode(callSuper = false)

@Accessors(chain = true)

@TableName("ts_project")

@ApiModel(value="Project对象", description="撰写项目申请书的基本内容")

public class Project implements Serializable {

private static final long serialVersionUID = 1L;

@TableId(value = "id", type = IdType.AUTO)

private String id;

private Integer workNumber;

private Integer adminId;

private String name;

@ApiModelProperty(value = "创建时间")

@jsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date gmtCreate;

@ApiModelProperty(value = "更新时间")

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date gmtModified;

}

application.yml

server:

# 端口

port: 8081

spring:

# 数据源配置

datasource:

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://localhost:3306/tetndmDlQJst2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai

username: root

password: root

# Mybatis-plus配置

mybatis-plus:

#配置Mapper映射文件

mapper-locations: classpath*:/mapper/*Mapper.xml

# 配置MyBatis数据返回类型别名(默认别名是类名)

type-aliases-package: com.xxxx.server.pojo

configuration:

# 自动驼峰命名

map-underscore-to-camel-case: fals

启动类

@SpringBootApplication

@EnableScheduling

@MapperScan("com.xxxx.server.mapper")

public class TestApplication {

public static void main(String[] args) {

SpringApplication.run(TestApplicatitndmDlQJon.class,args);

}

}

搭建完成

此时执行操作,并不会在表中添加时间,如下:

二. 设置自动填充

创建MyMetaObjectHandler文件,实现自动填充

/**

* 自动填充时间

*/

@Component

public class MyMetaObjectHandler implements MetaObjectHandler {

@Override

public void insertFill(MetaObject metaObject) {

this.setFieldValByName("gmtCreate", new Date(), metaObject);

this.setFieldValByName("gmtModified", new Date(), metaObject);

}

@Override

public void updateFill(MetaObject metaObject) {

this.setFieldValByName("gmtModified", new Date(), metaObject);

}

}

修改pojo类

@Data

@EqualsAndHashCode(callSuper = false)

@Accessors(chain = true)

@TableName("ts_project")

@ApiModel(value="Project对象", description="撰写项目申请书的基本内容")

public class Project implements Serializable {

private static final long serialVersionUID = 1L;

@TableId(value = "id", type = IdType.AUTO)

private String id;

private Integer workNumber;

private Integer adminId;

private String name;

@ApiModelProperty(value = "创建时间")

@TableField(fill = FieldFill.INSERT)

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date gmtCreate;

@ApiModelProperty(value = "更新时间")

@TableField(fill = FieldFill.INSERT_UPDATE)

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date gmtModified;

}

在tndmDlQJgmtCreate上增加   @TableField(fill = FieldFill.INSERT) 表示创建时间。在gmtModified上增加 @TableField(fill = FieldFill.INSERT_UPDATE)表示修改时间。gmtCreate和gmtModified需要与自定义方法中的字段相匹配。

此时执行操作,会在表中添加时间,如下:

}

serviceImpl

@Service

public class ProjectServiceImpl extends ServiceImpl implements IProjectService {

}

Mapper

@Service

public class ProjectServiceImpl extends ServiceImpl implements IProjectService {

}

Pojo

@Data

@EqualsAndHashCode(callSuper = false)

@Accessors(chain = true)

@TableName("ts_project")

@ApiModel(value="Project对象", description="撰写项目申请书的基本内容")

public class Project implements Serializable {

private static final long serialVersionUID = 1L;

@TableId(value = "id", type = IdType.AUTO)

private String id;

private Integer workNumber;

private Integer adminId;

private String name;

@ApiModelProperty(value = "创建时间")

@jsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date gmtCreate;

@ApiModelProperty(value = "更新时间")

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date gmtModified;

}

application.yml

server:

# 端口

port: 8081

spring:

# 数据源配置

datasource:

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://localhost:3306/tetndmDlQJst2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai

username: root

password: root

# Mybatis-plus配置

mybatis-plus:

#配置Mapper映射文件

mapper-locations: classpath*:/mapper/*Mapper.xml

# 配置MyBatis数据返回类型别名(默认别名是类名)

type-aliases-package: com.xxxx.server.pojo

configuration:

# 自动驼峰命名

map-underscore-to-camel-case: fals

启动类

@SpringBootApplication

@EnableScheduling

@MapperScan("com.xxxx.server.mapper")

public class TestApplication {

public static void main(String[] args) {

SpringApplication.run(TestApplicatitndmDlQJon.class,args);

}

}

搭建完成

此时执行操作,并不会在表中添加时间,如下:

二. 设置自动填充

创建MyMetaObjectHandler文件,实现自动填充

/**

* 自动填充时间

*/

@Component

public class MyMetaObjectHandler implements MetaObjectHandler {

@Override

public void insertFill(MetaObject metaObject) {

this.setFieldValByName("gmtCreate", new Date(), metaObject);

this.setFieldValByName("gmtModified", new Date(), metaObject);

}

@Override

public void updateFill(MetaObject metaObject) {

this.setFieldValByName("gmtModified", new Date(), metaObject);

}

}

修改pojo类

@Data

@EqualsAndHashCode(callSuper = false)

@Accessors(chain = true)

@TableName("ts_project")

@ApiModel(value="Project对象", description="撰写项目申请书的基本内容")

public class Project implements Serializable {

private static final long serialVersionUID = 1L;

@TableId(value = "id", type = IdType.AUTO)

private String id;

private Integer workNumber;

private Integer adminId;

private String name;

@ApiModelProperty(value = "创建时间")

@TableField(fill = FieldFill.INSERT)

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date gmtCreate;

@ApiModelProperty(value = "更新时间")

@TableField(fill = FieldFill.INSERT_UPDATE)

@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "Asia/Shanghai")

@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

private Date gmtModified;

}

在tndmDlQJgmtCreate上增加   @TableField(fill = FieldFill.INSERT) 表示创建时间。在gmtModified上增加 @TableField(fill = FieldFill.INSERT_UPDATE)表示修改时间。gmtCreate和gmtModified需要与自定义方法中的字段相匹配。

此时执行操作,会在表中添加时间,如下:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:MyBatisPlus利用Service实现获取数据列表
下一篇:Springboot详解线程池与多线程及阻塞队列的应用详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~