版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012326462/article/details/82668427
用于数据库属性类型与java存储的类型做转换,例如枚举类型,在存储到数据库时或者在数据库取出来时,不用手动转换。
必须实现接口AttributeConverter<X,Y>,源码如下:
package javax.persistence;
/**
* A class that implements this interface can be used to convert
* entity attribute state into database column representation
* and back again.
* Note that the X and Y types may be the same Java type.
*
* @param X the type of the entity attribute
* @param Y the type of the database column
*
* @since Java Persistence 2.1
*/
public interface AttributeConverter<X,Y> {
/**
* Converts the value stored in the entity attribute into the
* data representation to be stored in the database.
*
* @param attribute the entity attribute value to be converted
* @return the converted data to be stored in the database column
*/
public Y convertToDatabaseColumn (X attribute);
/**
* Converts the data stored in the database column into the
* value to be stored in the entity attribute.
* Note that it is the responsibility of the converter writer to
* specify the correct dbData type for the corresponding column
* for use by the JDBC driver: i.e., persistence providers are
* not expected to do such type conversion.
*
* @param dbData the data from the database column to be converted
* @return the converted value to be stored in the entity attribute
*/
public X convertToEntityAttribute (Y dbData);
}
convertToDatabaseColumn(X attribute)用于把输入的类型转成数据库存储的类型
convertToEntityAttribute (Y dbData) 用于把数据库搜出来的类型转成实体中想要的类型
下面看实例:
我自己定义了一个枚举
package com.xhx.springboot.enums;
public enum GenderEnum{
MAN("1","男"),WOMAN("2","女");
GenderEnum(String code,String value){
this.code = code;
this.value = value;
}
private String code;
private String value;
private String description;
public String getCode() {
return this.code;
}
public String getValue() {
return this.value;
}
public String getDescription() {
return this.description;
}
}
定义一个转换器类,实现AttributeConverter<X,Y>接口
package com.xhx.springboot.converters;
import com.xhx.springboot.enums.GenderEnum;
import javax.persistence.AttributeConverter;
import java.util.Arrays;
public class GenderEnumConverter implements AttributeConverter<GenderEnum,String> {
@Override
public String convertToDatabaseColumn(GenderEnum genderEnum) {
return genderEnum.getCode();
}
@Override
public GenderEnum convertToEntityAttribute(String s) {
return Arrays.stream(GenderEnum.values()).filter(en -> en.getCode().equals(s)).findFirst().orElse(null);
}
}
在实体类属性上,加入@Convert注解,把转换器设置成上面实现的转换器。存储值时就会调用convertToDatabaseColumn方法,把code存入数据库,查询值时就会调用convertToEntityAttribute方法,转成枚举值,赋值到实体类中。
package com.xhx.springboot.entity;
import com.xhx.springboot.converters.GenderEnumConverter;
import com.xhx.springboot.enums.GenderEnum;
import javax.persistence.*;
/**
* @author xuhaixing
* @date 2018/4/28 10:29
*/
@Entity
@Table(name = "USER")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private int id;
private String name;
private int age;
@Convert(
converter = GenderEnumConverter.class
)
private GenderEnum gender;
@Version
private int version;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public GenderEnum getGender() {
return gender;
}
public void setGender(GenderEnum gender) {
this.gender = gender;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", version=" + version +
'}';
}
}
写了一个测试用例:调用controller接口,一个是添加数据,一个是查询数据
package com.xhx.springboot;
import com.xhx.springboot.controller.UserController;
import com.xhx.springboot.entity.User;
import com.xhx.springboot.enums.GenderEnum;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot5ApplicationTests {
@Autowired
private UserController userController;
@Test
public void testConvertToDB() {
User user = new User();
user.setName("小徐");
user.setAge(25);
user.setGender(GenderEnum.MAN);
userController.add(user); //GenderEnum类型已经转换
}
@Test
public void testDBToEntity() {
User user = userController.findById(2);//GenderEnum类型已经转换
System.out.println(user);
//User{id=2, name='小徐', age=25, gender=MAN, version=0}
}
}
执行第一个后,数据库中已经有值,如下图
执行第二个返回结果如上面注释。
我的代码地址 路径springboot/SpringBoot基础/springboot5-3/