码迷,mamicode.com
首页 > 其他好文 > 详细

Halo(十)

时间:2019-11-28 21:08:04      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:nal   throw   枚举   十分   一对一   uppercase   stat   cti   Fix   

Spring Converter(转换器)

@FunctionalInterface
public interface Converter<S, T> {  //一对一转换
    @Nullable
    T convert(S source);
}

该接口非常简单,只定义了一个转换方法,两个泛型参数则是需要转换的两个类型。
在单独处理两个类型的转换时这是首选,即一对一。


但是倘若有同一父类(或接口)的类型需要进行类型转化,为每个类型都写一个Converter显然是十分不理智的。
对于这种情况,Spring提供了一个ConverterFactory接口。

public interface ConverterFactory<S, R> {   //一对多转换
    <T extends R> Converter<S, T> getConverter(Class<T> targetType);
}

字符串转换成枚举类

public class StringToEnumConverterFactory implements ConverterFactory<String, Enum> {

    @SuppressWarnings("unchecked")
    @Override
    public <T extends Enum> Converter<String, T> getConverter(Class<T> targetType) {
        return new StringToEnumConverter(targetType);
    }

    private static class StringToEnumConverter<T extends Enum>
            implements Converter<String, T> {

        private Class<T> enumType;

        private StringToEnumConverter(Class<T> enumType) {
            this.enumType = enumType;
        }

        @SuppressWarnings("unchecked")
        @Override
        public T convert(String source) {
            return (T) Enum.valueOf(this.enumType, source.toUpperCase());
        }
    }
}


注册转换器:

    public class WebMvcAutoConfiguration implements WebMvcConfigurer {
        @Override
        public void addFormatters(FormatterRegistry registry) {
            registry.addConverterFactory(new StringToEnumConverterFactory());
        }
    }

Hibernate 继承映射

单表继承映射(一张表)

每个子类对应一张表的继承映射(两张表)

每个类对应一张表的继承映射(三张表)

三种映射方式的比较:

  第一种方式,只有一张表,数据冗余较多,但查询效率高,数据量不是非常大的时候,推荐使用。

  第二种方式,每个子类对应一张表,数据冗余比较少,查询效率不高,主键不能设置成自增。

  第三种方式,每个类对应一张表,数据冗余较少,查询效率比第二种方式稍高,需要维护的表的个数较多。

Signal Table 多个entity对应同一张表,用一个字段区分(第一种方式)

//定义辨别者列的列名为person_type,列类型为字符串
@DiscriminatorColumn(name="person_type", discriminatorType=DiscriminatorType.STRING)
//指定该实体对应的记录在辨别者列的值为"person"
@DiscriminatorValue("普通人")
public class Person


@DiscriminatorValue("顾客")
public class Customer extends Person

@DiscriminatorValue("员工")
public class Employee extends Person


/** 基础评论 Entity */
@Data
@Entity(name = "BaseComment")
@Table(name = "comments")
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER, columnDefinition = "int default 0")
public class BaseComment extends BaseEntity {}


@Entity(name = "PostComment")
@DiscriminatorValue("0")
public class PostComment extends BaseComment {}

@Entity(name = "SheetComment")
@DiscriminatorValue("1")
public class SheetComment extends BaseComment {}

@Entity(name = "JournalComment")
@DiscriminatorValue("2")
public class JournalComment extends BaseComment {}

Spring Boot 解析 YAML

server:
    name: app
    address:
        - 192.168.1.1
        - 192.168.1.2

@Component
@ConfigurationProperties(prefix = "server")
public class Properties {
    private String name;
    private List<String> address = new ArrayList<>();
}


server:
    list:
      - name: app1
        address:
            - 10.11.1.1
            - 10.11.1.2
      - name: app2
        address:
            - 10.10.1.1
            - 10.10.1.2

@Component
@ConfigurationProperties(prefix = "server")
public class Properties {
    private List<Ld> list = new ArrayList<>();
}
public class Ld {
    private String name;
    private List<String> address = new ArrayList<>();
}


或者


/** YAML文件转换器 */
public interface ThemePropertyResolver {
    ThemeProperty resolve(String content) throws IOException;
}

@Service
public class YamlThemePropertyResolver implements ThemePropertyResolver {

    private final ObjectMapper yamlMapper;

    public YamlThemePropertyResolver() {
        yamlMapper = new ObjectMapper(new YAMLFactory());
        //未知属性不报错
        yamlMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    @Override
    public ThemeProperty resolve(String content) throws IOException {
        return yamlMapper.readValue(content, ThemeProperty.class);
    }
}

@Data
public class ThemeProperty {
    private String id;
    private String name;

    @Data
    public static class Author {
        private String name;
        private String website;
    }
}

id: ld
name: ld
author:
  name: ld
  website: https://

Halo(十)

标签:nal   throw   枚举   十分   一对一   uppercase   stat   cti   Fix   

原文地址:https://www.cnblogs.com/loveer/p/11953506.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!