Mybatis直接映射字段值到枚举中

1、mysql数据库字段

Mybatis直接映射字段值到枚举中_第1张图片

2、实体类

@Data
@EqualsAndHashCode(callSuper=false)
public class UserManager {
	private long id;
	private String name;
	private UserEnum userType;
}

3、枚举

public enum UserEnum{
	MANAGER_USER_TYPE("管理员"),
	NORMAL_USER_TYPE("普通用户");
			
	private String userType;
	
	private UserEnum(String userType){
		
		this.userType = userType;
	}
	
	public String getEnumName(){ 
		return this.name();
	}
	
	public String getTypeName(){
		return this.userType;
	}

}

4、测试类

public class TestEnumMapper {
	InputStream inp ;
	SqlSessionFactory build;
	SqlSession openSession;
	@Before
	public void before() throws IOException{
		inp = Resources.getResourceAsStream("SqlMapConfig.xml");
		build = new SqlSessionFactoryBuilder().build(inp);
		openSession = build.openSession(true);//设置自动提交
	}

    //测试映射枚举
	@Test
	public void testEnum(){
		UserManagerMapper userManagerMapper = openSession.getMapper(UserManagerMapper.class);
		List queryAll = userManagerMapper.queryAll();
		for (UserManager userManager : queryAll) {
                        System.out.println(userManager.getName());
			System.out.println(userManager.getUserType().getTypeName());
			
		}
	}

5、结果

Mybatis直接映射字段值到枚举中_第2张图片

你可能感兴趣的:(Mybatis直接映射字段值到枚举中)