zoukankan      html  css  js  c++  java
  • springboot 码表转换 typeHandler

    数据库中很多码表,比如人员性别(男女),数据有效性(是否)……

    在前后端分离的接口调试中, 码表都是通过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型

  • 相关阅读:
    JavaScript优美的特性
    指尖初体验之主屏幕操作
    XHTML下css+div布局总结
    CSS表常用小技巧
    让两个DIV居中显示
    用CSS做滑动门
    CSS控制文本自动换行
    符合css规范的下拉菜单(兼容ff\ie6\ie7)
    层垂直居中于浏览器
    用css来强制按比例压缩图片的高度或宽度
  • 原文地址:https://www.cnblogs.com/heshana/p/15507141.html
Copyright © 2011-2022 走看看