数据库中很多码表,比如人员性别(男女),数据有效性(是否)……
在前后端分离的接口调试中, 码表都是通过1,2 ……数字进行传递, 不明了, 也不方便进行数据的比对核验。
springboot的typeHandler , 主要是让接口通过码表键值对中的值进行传输, 所有属性的含义一目了然。
1.配置文件添加mybatis扫描的typeHandler类
mybatis:
# 搜索指定包别名
typeAliasesPackage: com.guideir.workRecode.pojo
# 配置mapper的扫描,找到所有的mapper.xml映射文件
mapperLocations: classpath:mapper/**/*Mapper.xml
typeHandlersPackage: com.workRecode.typeHandler
configuration:
mapUnderscoreToCamelCase: true
2.添加码表的枚举
/** * TODO * * @author hs * @date 2021/10/28 13:56 */ public enum ProjectTypeEnum { 开发中项目(1,"开发中项目" ), 维护中项目(2,"维护中项目") ; private final Integer code; private final String description; private ProjectTypeEnum(Integer code, String description) { this.code = code; this.description = description; } public Integer getCode() { return code; } public String getDescription() { return description; } public static ProjectTypeEnum getEnumByCode(Integer code){ for(ProjectTypeEnum projectTypeEnum: ProjectTypeEnum.values()){ if(projectTypeEnum.getCode() == code){ return projectTypeEnum; } } return null ; } }
3. pojo中的对象属性改变
import java.util.Date;
@Data
public class GdProject {
private String id;
private String projectName;
private ProjectTypeEnum projectType;
private String PIC;
private String description;
private String createUser;
@DateTimeFormat (pattern="yyyy-MM-dd")
@JsonFormat (timezone = "GMT+8",pattern = "yyyy-MM-dd")
private Date createTime;
private Integer isEnable;
public GdProject(String id, String projectName, ProjectTypeEnum projectType, String PIC, String description, String createUser, Date createTime, Integer isEnable) {
this.id = id;
this.projectName = projectName;
this.projectType = projectType;
this.PIC = PIC;
this.description = description;
this.createUser = createUser;
this.createTime = createTime;
this.isEnable = isEnable;
}
public GdProject(String id, String projectName, Integer projectType, String PIC, String description, String createUser, Date createTime, Integer isEnable) {
this.id = id;
this.projectName = projectName;
this.projectType = ProjectTypeEnum.getEnumByCode(projectType);
this.PIC = PIC;
this.description = description;
this.createUser = createUser;
this.createTime = createTime;
this.isEnable = isEnable;
}
public GdProject() {
super();
}
}
4.
package com.workRecode.typeHandler; /** * TODO * * @author hs * @date 2021/10/20 17:13 */ @MappedJdbcTypes(JdbcType.INTEGER) @MappedTypes(value = ProjectTypeEnum.class) public class ProjectTypeHandler extends BaseTypeHandler<ProjectTypeEnum> { @Override public void setNonNullParameter(PreparedStatement preparedStatement, int i, ProjectTypeEnum projectTypeEnum, JdbcType jdbcType) throws SQLException { preparedStatement.setInt(i,projectTypeEnum.getCode()); } @Override public ProjectTypeEnum getNullableResult(ResultSet resultSet, String s) throws SQLException { Integer code = resultSet.getInt(s); return ProjectTypeEnum.getEnumByCode(code); } @Override public ProjectTypeEnum getNullableResult(ResultSet resultSet, int i) throws SQLException { Integer code = resultSet.getInt(i); return ProjectTypeEnum.getEnumByCode(code); } @Override public ProjectTypeEnum getNullableResult(CallableStatement callableStatement, int i) throws SQLException { Integer code = callableStatement.getInt(i); return ProjectTypeEnum.getEnumByCode(code); } }
通过上面几部操作后测试
请求路径中传入是中午释义 , 数据库中保存的为int型