龙空技术网

springboot2-mybatis-plus-公共字段自动填充

codewang 597

前言:

现时大家对“oracle判断字段包含逗号”大约比较着重,姐妹们都需要学习一些“oracle判断字段包含逗号”的相关文章。那么小编同时在网络上收集了一些对于“oracle判断字段包含逗号””的相关知识,希望各位老铁们能喜欢,各位老铁们快快来了解一下吧!

一、pom文件:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="" xmlns:xsi="" xsi:schemaLocation=" "> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo-mybatis</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-mybatis</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <mybatisplus.version>3.0.5</mybatisplus.version> <druid.version>1.1.9</druid.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatisplus.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>

二、application.yml配置:

spring: # 数据源配置 datasource: # Druid连接池 druid: # serverTimezone=Asia/Shanghai、CTT url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Hongkong&useSSL=false username: root password: root initial-size: 5 # 最大连接数 max-active: 400 # 最小连接数 min-idle: 1 # 获取连接等待超时的时间 max-wait: 60000# MyBatis-Plus配置mybatis-plus: # *Mapper.xml文件路径 mapper-locations: classpath*:/mybatis/*Mapper.xml # 实体扫描,多个package用逗号或者分号分隔 type-aliases-package: com.example.mybatis.entity,com.example.mybatis.mapper global-config: db-config: # 全局默认主键类型 id-type: uuid # 字段策略,null值的字段不会拼接到SQL中 field-strategy: not_null # 是否开启大写命名,默认false capital-mode: true configuration: # DB列名到JavaBean属性名映射,符合规则无需使用@TableField注解指定数据库字段名(没开启这个,SQL需要写as: select user_id as userId) map-underscore-to-camel-case: true # 配置文件中的所有映射器已经配置的任何缓存 cache-enabled: true # 配置JdbcTypeForNull, Oracle数据库必须配置 jdbc-type-for-null: 'null'

三、entity类:

package com.example.mybatis.entity;import com.baomidou.mybatisplus.annotation.*;import java.util.Date;@TableName("t_account")public class Account { private static final long serialVersionUID = 1L; /** * 主键 */ @TableId private String id; /** * 创建人 */ @TableField(fill = FieldFill.INSERT) private String createdBy; /** * 创建时间 */ @TableField(fill = FieldFill.INSERT) private Date createdDate; /** * 最后更新人 */ @TableField(fill = FieldFill.INSERT_UPDATE) private String lastUpdatedBy; /** * 最后更新时间 */ @TableField(fill = FieldFill.INSERT_UPDATE) private Date lastUpdatedDate; /** * 逻辑删除标记,取值0/1,1表示被删除的数据 */ @TableLogic @TableField(fill = FieldFill.INSERT) private Integer removeFlag; /** * 逻辑删除时间戳,新增纪录时默认为0,删除记录时取当前系统时间戳 */ @TableField(fill = FieldFill.INSERT) private Integer removeTimestamp; /** * 账号 */ private String account; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCreatedBy() { return createdBy; } public void setCreatedBy(String createdBy) { this.createdBy = createdBy; } public Date getCreatedDate() { return createdDate; } public void setCreatedDate(Date createdDate) { this.createdDate = createdDate; } public String getLastUpdatedBy() { return lastUpdatedBy; } public void setLastUpdatedBy(String lastUpdatedBy) { this.lastUpdatedBy = lastUpdatedBy; } public Date getLastUpdatedDate() { return lastUpdatedDate; } public void setLastUpdatedDate(Date lastUpdatedDate) { this.lastUpdatedDate = lastUpdatedDate; } public Integer getRemoveFlag() { return removeFlag; } public void setRemoveFlag(Integer removeFlag) { this.removeFlag = removeFlag; } public Integer getRemoveTimestamp() { return removeTimestamp; } public void setRemoveTimestamp(Integer removeTimestamp) { this.removeTimestamp = removeTimestamp; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; }}

四、enum类:

package com.example.mybatis.enums;import java.util.Date;public enum MyBatisAutoFillFieldEnum { /** * 创建时间 */ CREATED_DATE("createdDate", new Date()), /** * 创建人 */ CREATED_BY("createdBy","auto_by_admin"), /** * 最后更新时间 */ LAST_UPDATED_DATE("lastUpdatedDate",new Date()), /** * 更新人 */ LAST_UPDATED_BY("lastUpdatedBy","auto_by_admin"), /** * 逻辑删除标记,取值0/1,1表示被删除的数据 */ REMOVE_FLAG("removeFlag",0), /** * 逻辑删除时间戳,新增纪录时默认为0,删除记录时取当前系统时间戳 */ REMOVE_TIMESTAMP("removeTimestamp",0) ; private String fieldName; private Object value; MyBatisAutoFillFieldEnum(String fieldName, Object value) { this.fieldName = fieldName; this.value = value; } public String getFieldName() { return fieldName; } public void setFieldName(String fieldName) { this.fieldName = fieldName; } public Object getValue() { return value; } public void setValue(Object value) { this.value = value; }}

五、公共字段处理handler:

package com.example.mybatis.handler;import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;import com.example.mybatis.enums.MyBatisAutoFillFieldEnum;import org.apache.ibatis.reflection.MetaObject;import org.springframework.stereotype.Component;@Componentpublic class MyBatisAutoFillHandler implements MetaObjectHandler{ @Override public void insertFill(MetaObject metaObject) { // 创建时间 this.setFieldValByName(MyBatisAutoFillFieldEnum.CREATED_DATE.getFieldName(), MyBatisAutoFillFieldEnum.CREATED_DATE.getValue(),metaObject); // 创建人 this.setFieldValByName(MyBatisAutoFillFieldEnum.CREATED_BY.getFieldName(), MyBatisAutoFillFieldEnum.CREATED_BY.getValue(),metaObject); // 更新时间 this.setFieldValByName(MyBatisAutoFillFieldEnum.LAST_UPDATED_DATE.getFieldName(), MyBatisAutoFillFieldEnum.LAST_UPDATED_DATE.getValue(),metaObject); // 更新人 this.setFieldValByName(MyBatisAutoFillFieldEnum.LAST_UPDATED_BY.getFieldName(), MyBatisAutoFillFieldEnum.LAST_UPDATED_BY.getValue(),metaObject); // 删除标志 this.setFieldValByName(MyBatisAutoFillFieldEnum.REMOVE_FLAG.getFieldName(), MyBatisAutoFillFieldEnum.REMOVE_FLAG.getValue(),metaObject); // 删除时间戳 this.setFieldValByName(MyBatisAutoFillFieldEnum.REMOVE_TIMESTAMP.getFieldName(), MyBatisAutoFillFieldEnum.REMOVE_TIMESTAMP.getValue(),metaObject); } @Override public void updateFill(MetaObject metaObject) { // 更新时间 this.setFieldValByName(MyBatisAutoFillFieldEnum.LAST_UPDATED_DATE.getFieldName(), MyBatisAutoFillFieldEnum.LAST_UPDATED_DATE.getValue(),metaObject); // 更新人 this.setFieldValByName(MyBatisAutoFillFieldEnum.LAST_UPDATED_BY.getFieldName(), MyBatisAutoFillFieldEnum.LAST_UPDATED_BY.getValue(),metaObject); }}

六、mapper接口:

package com.example.mybatis.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;import com.example.mybatis.entity.Account;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface AccountMapper extends BaseMapper<Account> {}

七、AccountMapper.xml文件:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ""><mapper namespace="com.example.mybatis.mapper.AccountMapper"> <resultMap id="BaseResultMap" type="com.example.mybatis.entity.Account"> <id column="id" property="id" /> <result column="account" property="account" /> <result column="created_date" property="createdDate" /> <result column="last_updated_date" property="lastUpdatedDate" /> <result column="created_by" property="createdBy" /> <result column="last_updated_by" property="lastUpdatedBy" /> <result column="remove_flag" property="removeFlag" /> </resultMap></mapper>

标签: #oracle判断字段包含逗号