欢迎光临我的博客[http://poetize.cn],前端使用Vue2,聊天室使用Vue3,后台使用Spring Boot
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://