1. 项目环境配置
首先,需要确保项目中已经集成了 spring boot 和 mybatis-plus。下面是一些基本的配置步骤:
1.1 引入必要的依赖
在 pom.xml
中添加 mybatis-plus 的依赖:
<dependencies> <!-- mybatis-plus --> <dependency> <groupid>com.baomidou</groupid> <artifactid>mybatis-plus-boot-starter</artifactid> <version>3.5.1</version> </dependency> <!-- 其他必要依赖 --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <!-- mysql 驱动 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> <scope>runtime</scope> </dependency> </dependencies>
1.2 配置数据源
在 application.yml
或 application.properties
文件中配置数据库连接:
spring: datasource: url: jdbc:mysql://localhost:3306/your_database?useunicode=true&characterencoding=utf-8&servertimezone=utc username: your_username password: your_password driver-class-name: com.mysql.cj.jdbc.driver mybatis-plus: mapper-locations: classpath:/mapper/*.xml type-aliases-package: com.example.entity global-config: db-config: id-type: auto
2. 创建基础实体类
为数据库中的表创建一个基础实体类 baseentity
,在该类中定义创建时间和更新时间字段,并使用 mybatis-plus 的注解配置自动填充。
package com.example.utils; import com.baomidou.mybatisplus.annotation.fieldfill; import com.baomidou.mybatisplus.annotation.tablefield; import com.fasterxml.jackson.annotation.jsonignore; import com.fasterxml.jackson.annotation.jsoninclude; import lombok.data; import java.io.serializable; import java.util.date; import java.util.hashmap; import java.util.map; @data public class baseentity implements serializable { private static final long serialversionuid = 1l; /** * 搜索值(暂时忽略) */ @jsonignore @tablefield(exist = false) private string searchvalue; /** * 创建者 */ @tablefield(fill = fieldfill.insert) private string createby; /** * 创建时间 */ @tablefield(fill = fieldfill.insert) private date createtime; /** * 更新者 */ @tablefield(fill = fieldfill.insert_update) private string updateby; /** * 更新时间 */ @tablefield(fill = fieldfill.insert_update) private date updatetime; /** * 请求参数(暂时忽略) */ @jsoninclude(jsoninclude.include.non_empty) @tablefield(exist = false) private map<string, object> params = new hashmap<>(); }
3. 实现 metaobjecthandler 接口
为了使 baseentity
中的自动填充注解生效,我们需要实现 metaobjecthandler
接口,并将其配置为 spring 的一个 bean。
3.1 创建 mymetaobjecthandler 类
package com.example.config; import com.baomidou.mybatisplus.core.handlers.metaobjecthandler; import org.apache.ibatis.reflection.metaobject; import org.springframework.stereotype.component; import java.util.date; @component public class mymetaobjecthandler implements metaobjecthandler { @override public void insertfill(metaobject metaobject) { // 填充创建时间和更新时间 this.strictinsertfill(metaobject, "createtime", date.class, new date()); this.strictinsertfill(metaobject, "updatetime", date.class, new date()); // 可以根据业务需求获取当前用户,填充创建者和更新者 this.strictinsertfill(metaobject, "createby", string.class, getcurrentuser()); this.strictinsertfill(metaobject, "updateby", string.class, getcurrentuser()); } @override public void updatefill(metaobject metaobject) { // 更新时只填充更新时间和更新者 this.strictupdatefill(metaobject, "updatetime", date.class, new date()); this.strictupdatefill(metaobject, "updateby", string.class, getcurrentuser()); } // 示例方法:获取当前用户信息 private string getcurrentuser() { // 这里可以集成 spring security 或其他上下文获取实际的当前用户 return "system"; // 这是一个占位符,实际应用中会替换为真实用户信息 } }
3.2 配置生效
通过在 mymetaobjecthandler
类上使用 @component
注解,spring 会自动管理这个类,并在实体插入和更新时调用它来填充字段。
4. 实体类继承 baseentity
现在,你的具体实体类可以继承 baseentity
,从而自动获得 createtime
和 updatetime
字段的自动填充功能。
package com.example.entity; import com.baomidou.mybatisplus.annotation.tablename; import com.example.utils.baseentity; import lombok.data; @data @tablename("your_table") public class yourentity extends baseentity { // 其他字段 private string name; private integer age; }
5. 处理数据插入与更新
在 service 或 mapper 层,执行插入或更新操作时,createtime
和 updatetime
字段会自动填充。
5.1 service 层示例
@service public class yourentityservice { @autowired private yourentitymapper yourentitymapper; public void saveentity(yourentity entity) { yourentitymapper.insert(entity); } public void updateentity(yourentity entity) { yourentitymapper.updatebyid(entity); } }
6. 验证自动填充功能
通过简单的单元测试或集成测试,验证 createtime
和 updatetime
字段在插入和更新时是否正确自动填充。
7. 其他注意事项
- 字段类型: 确保数据库中的
createtime
和updatetime
字段类型与 java 实体类中的类型相匹配(通常是datetime
类型)。 - 时间格式: 如果需要统一时间格式,可以在配置文件中设置 spring boot 的全局时间格式,或使用
@jsonformat
注解指定序列化时的格式。
spring: jackson: date-format: yyyy-mm-dd hh:mm:ss time-zone: gmt+8
总结
通过本文的教程,你可以轻松地在 spring boot 项目中使用 mybatis-plus 自动填充创建时间和更新时间字段。该方法充分利用了 mybatis-plus 的自动填充机制,并结合 spring boot 的优势,使开发过程更加简洁高效。如果遇到问题,务必检查 metaobjecthandler
是否正确注册,字段类型是否匹配,以及数据库配置是否正确。
以上就是mybatis-plus自动填充字段的详细教程的详细内容,更多关于mybatis-plus自动填充字段的资料请关注代码网其它相关文章!
发表评论