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属性名的更多相关文章

  1. SpringBoot注解把配置文件自动映射到属性和实体类实战

    SpringBoot注解把配置文件自动映射到属性和实体类实战 简介:讲解使用@value注解配置文件自动映射到属性和实体类 1.配置文件加载 方式一 1.Controller上面配置 @Propert ...

  2. 小D课堂-SpringBoot 2.x微信支付在线教育网站项目实战_2-7.接口配置文件自动映射到属性和实体类配置

    笔记 7.接口配置文件自动映射到属性和实体类配置     简介:使用@value注解配置文件自动映射到属性和实体类 1.添加 @Component或者Configuration 注解:        ...

  3. SpringBoot------注解把配置文件自动映射到属性和实体类

    1.映射到属性 package top.ytheng.demo.controller; import org.springframework.beans.factory.annotation.Valu ...

  4. SpringBoot配置文件自动映射到属性和实体类(8)

    一.配置文件加载 1.Controller中配置并指向文件 @Controller @PropertySource(value = { "application.properties&quo ...

  5. 002.Oracle数据库 , 列别名

    /*Oracle数据库查询日期在两者之间*/ SELECT OCCUR_DATE as "我是一列" FROM LM_FAULT WHERE ( ( OCCUR_DATE > ...

  6. MyBatis 中使用数据库查询别名进行映射

    方法1 XXMapper.xml <mapper namespace="com.hfepc.dao.andon.AndonExceptionKanbanVOMapper" & ...

  7. Hibernate (开源对象关系映射框架)

    一.基本介绍1.它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm(对象关系映射)框架,hibernate可以自动生成SQL语句,自动执行: Hibern ...

  8. Mybatis映射文件的自动映射与手动映射问题

    Mapper XML 文件 MyBatis 的真正强大在于它的映射语句,也是它的魔力所在.由于它的异常强大,映射器的 XML 文件就显得相对简单.如果拿它跟具有相同功能的 JDBC 代码进行对比,你会 ...

  9. MyBatis(5)——解决属性名与列名不一致的问题

    解决属性名与列名不一致的问题 问题描述: 当实体类的属性与数据库的列名不对应时取不到该列数据 说明:MyBatis会根据查询的列名设值(列名的setter方法),然后以此列名为做查询等操作,在此过程中 ...

随机推荐

  1. 常用的 Python 爬虫技巧总结

    用python也差不多一年多了,python应用最多的场景还是web快速开发.爬虫.自动化运维:写过简单网站.写过自动发帖脚本.写过收发邮件脚本.写过简单验证码识别脚本. 爬虫在开发过程中也有很多复用 ...

  2. mysql忘记root密码

    skip-grant-tables:非常有用的mysql启动参数 介绍一个非常有用的mysql启动参数—— --skip-grant-tables.顾名思义,就是在启动mysql时不启动grant-t ...

  3. 枚举 POJ 2965 The Pilots Brothers' refrigerator

    题目地址:http://poj.org/problem?id=2965 /* 题意:4*4的矩形,改变任意点,把所有'+'变成'-',,每一次同行同列的都会反转,求最小步数,并打印方案 DFS:把'+ ...

  4. 模拟 POJ 2632 Crashing Robots

    题目地址:http://poj.org/problem?id=2632 /* 题意:几个机器人按照指示,逐个朝某个(指定)方向的直走,如果走过的路上有机器人则输出谁撞到:如果走出界了,输出谁出界 如果 ...

  5. BZOJ1769 : [Ceoi2009]tri

    将所有点极角排序,建立线段树,线段树每个节点维护该区间内所有点组成的上下凸壳. 对于一个查询,二分查找出相应区间的左右端点,在线段树上得到$O(\log n)$个节点,在相应凸壳上三分查找出与斜边叉积 ...

  6. objective-c 条件运算符

    条件运算符 val1!=0 ? val1:val2 等价于 val1?val2

  7. 【wikioi】1026 逃跑的拉尔夫

    题目链接 算法:BFS 14.01.02 PS: 本人再次脑残,BFS又是写得那么脓肿,突然发现我原来什么搜索都是不会的呀.. //2014-02-05已更新 ******************** ...

  8. 【wikioi】1108 方块游戏(模拟)

    http://wikioi.com/problem/1108/ 这题有点变态,因为他根本没有策略! 还是说这题不是实时的?反正这题很变态,是在一个时间段同时消除所有的行列斜边,同一时间!!!!!! 所 ...

  9. log4j与commons-logging,slf4j的关系

    前面有一篇日志中简单的介绍了 log4j,同时也介绍了它与commons-logging的关系,但是突然冒出来一个slf4j,并且slf4j有取代commons-logging的趋势,所以,我们可以推 ...

  10. CustomValidator验证的使用方法

    <asp:TextBox ID="txtNum" runat="server" Width="400px" ></asp: ...