hibernate 数据库列别名自动映射pojo属性名
package com.pccw.business.fcm.common.hibernate; import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import org.hibernate.HibernateException;
import org.hibernate.property.ChainedPropertyAccessor;
import org.hibernate.property.PropertyAccessor;
import org.hibernate.property.PropertyAccessorFactory;
import org.hibernate.property.Setter;
import org.hibernate.transform.ResultTransformer; /**
* 自定义的数据库字库转换成POJO
*/
public class ExtColumnToBean implements ResultTransformer {
private static final long serialVersionUID = 1L;
private final Class resultClass;
private Setter[] setters;
private PropertyAccessor propertyAccessor;
private List<Field> fields = new ArrayList<Field>();
BigDecimal bigDecimal = null; public ExtColumnToBean(Class resultClass) {
if (resultClass == null)
throw new IllegalArgumentException("resultClass cannot be null");
this.resultClass = resultClass;
propertyAccessor = new ChainedPropertyAccessor(new PropertyAccessor[] {
PropertyAccessorFactory.getPropertyAccessor(resultClass, null),
PropertyAccessorFactory.getPropertyAccessor("field") });
} // 结果转换时,HIBERNATE调用此方法
public Object transformTuple(Object[] tuple, String[] aliases) {
Object result; try {
if (setters == null) {// 取得目标POJO类的所有SETTER方法
setters = new Setter[aliases.length];
for (int i = 0; i < aliases.length; i++) {
String alias = aliases[i];
if (alias != null) {
setters[i] = getSetterByColumnName(alias);
}
}
}
result = resultClass.newInstance(); // 这里使用SETTER方法填充POJO对象
for (int i = 0; i < aliases.length; i++) {
if (setters[i] != null) {
Class[] parameterTypes = setters[i].getMethod().getParameterTypes();
if(parameterTypes == null || parameterTypes.length == 0){
continue;
}
//pojo set方法默认只有一个参数
if(parameterTypes[0].equals(Integer.class)){
if(tuple[i] instanceof BigDecimal){
bigDecimal = (BigDecimal)tuple[i];
setters[i].set(result, bigDecimal.intValue(), null);
}
}else if(parameterTypes[0].equals(Long.class)){
if(tuple[i] instanceof BigDecimal){
bigDecimal = (BigDecimal)tuple[i];
setters[i].set(result, bigDecimal.longValue(), null);
}
}else if(parameterTypes[0].equals(Double.class)){
if(tuple[i] instanceof BigDecimal){
bigDecimal = (BigDecimal)tuple[i];
setters[i].set(result, bigDecimal.doubleValue(), null);
}
}else{
setters[i].set(result, tuple[i], null);
}
}
}
} catch (InstantiationException e) {
throw new HibernateException("Could not instantiate resultclass: " + resultClass.getName());
} catch (IllegalAccessException e) {
throw new HibernateException("Could not instantiate resultclass: " + resultClass.getName());
} return result;
} /**
* 根据数据库字段名在POJO查找JAVA属性名 如:USER_ID 如果没有对应的属性名返回null
*
* @param alias
* 数据库字段名
* @return
*/
private Setter getSetterByColumnName(String alias) {
// 取得POJO所有属性名
// Field[] fields = resultClass.getDeclaredFields();
if (fields.isEmpty()) {
this.getClassField(resultClass);
}
if (fields == null || fields.size() == 0) {
throw new RuntimeException("实体" + resultClass.getName() + "不含任何属性");
}
// 把字段名中所有的下杠去除
String proName = alias.replaceAll("_", "").toLowerCase();
for (int i = 0; i < fields.size(); i++) {
Field field = fields.get(i);
//System.out.println(field.getName().toLowerCase());
if (field.getName().toLowerCase().equals(proName)) {// 去除下杠的字段名如果和属性名对得上,就取这个SETTER方法
return propertyAccessor.getSetter(resultClass, field.getName());
}
}
return null;
} @SuppressWarnings("unchecked")
public List transformList(List collection) {
return collection;
} private void getClassField(Class c) {
Field[] fs = c.getDeclaredFields();
if (fs != null && fs.length > 0) {
List li = Arrays.asList(fs);
fields.addAll(li);
}
Class superclass = c.getSuperclass();
if (superclass != null) {// 简单的递归一下
getClassField(superclass);
}
} }
hibernate 数据库列别名自动映射pojo属性名的更多相关文章
- SpringBoot注解把配置文件自动映射到属性和实体类实战
SpringBoot注解把配置文件自动映射到属性和实体类实战 简介:讲解使用@value注解配置文件自动映射到属性和实体类 1.配置文件加载 方式一 1.Controller上面配置 @Propert ...
- 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-7.接口配置文件自动映射到属性和实体类配置
笔记 7.接口配置文件自动映射到属性和实体类配置 简介:使用@value注解配置文件自动映射到属性和实体类 1.添加 @Component或者Configuration 注解: ...
- SpringBoot------注解把配置文件自动映射到属性和实体类
1.映射到属性 package top.ytheng.demo.controller; import org.springframework.beans.factory.annotation.Valu ...
- SpringBoot配置文件自动映射到属性和实体类(8)
一.配置文件加载 1.Controller中配置并指向文件 @Controller @PropertySource(value = { "application.properties&quo ...
- 002.Oracle数据库 , 列别名
/*Oracle数据库查询日期在两者之间*/ SELECT OCCUR_DATE as "我是一列" FROM LM_FAULT WHERE ( ( OCCUR_DATE > ...
- MyBatis 中使用数据库查询别名进行映射
方法1 XXMapper.xml <mapper namespace="com.hfepc.dao.andon.AndonExceptionKanbanVOMapper" & ...
- Hibernate (开源对象关系映射框架)
一.基本介绍1.它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm(对象关系映射)框架,hibernate可以自动生成SQL语句,自动执行: Hibern ...
- Mybatis映射文件的自动映射与手动映射问题
Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...
- MyBatis(5)——解决属性名与列名不一致的问题
解决属性名与列名不一致的问题 问题描述: 当实体类的属性与数据库的列名不对应时取不到该列数据 说明:MyBatis会根据查询的列名设值(列名的setter方法),然后以此列名为做查询等操作,在此过程中 ...
随机推荐
- SQL Server:查询当前服务器有多少连接请求
有时DBA需要检查当前服务器有多少连接请求,以及连接请求的登录名,客户端版本,主机名,程序名等等之类的信息,我们就可以对服务器的连接状况有所了解,防止不明用户使用. SQL Server本身提供了这么 ...
- Java入门知识点:
1.跨平台性主要原理是:在需要运行的java应用程序的操作系统上安装了一个对应操作系统对应版本的JVM(Java Virtual Machine)java虚拟机即可,由JVM来负责Java程序的在该系 ...
- HTML-a
链接的其他使用 电话 <a href="tel:(phonenumber)">Tel</a> 短信 <a href="sms:(phonen ...
- Javascript实现Linq查询方式
Linq是.net平台一个重要的技术,全称Language Integrated Query.通过构建快速查询语句,可快速从数据库或集合中筛选数据集.以查询数据库相同的方式操作内存数据. 在ECMAS ...
- DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)
题目传送门 题意:长度为L的金条,将n根金棍尽可能放上去,要求重心在L上,使得价值最大,最多有两条可以长度折半的放上去. 分析:首先长度可能为奇数,先*2.然后除了两条特殊的金棍就是01背包,所以dp ...
- HDU1247 Hat’s Words(Trie树)
常规做法是枚举每个字符串每个位置,时间复杂度O(n*len*len),(建字典树O(n*len)). 然而我看这题第一眼想的是时间复杂度O(n*len)的算法..就是建正反两棵字典树,每个字符串跑分别 ...
- python IDLE 改变窗口背景颜色
初学Python,想必大家拿来练习最多的IDE就是Python自带的IDLE了,但是默认的代码配色及语法高亮主题确实很不适应. 能不能把IDLE配置成像sublime_text那样的主题呢? 答案是当 ...
- 使用HttpsURLConnection发送POST请求
重写X509TrustManager private static TrustManager myX509TrustManager = new X509TrustManager() { @Overri ...
- const放在函数前和放在函数后
template < class T, class container = vector<T> > class MyClass{ private: T value; publi ...
- Graph database_neo4j 底层存储结构分析(8)
3.8 示例1:neo4j_exam 下面看一个简单的例子,然后看一下几个主要的存储文件,有助于理解<3–neo4j存储结构>描述的neo4j 的存储格式. 3.8.1 neo4j ...