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方法),然后以此列名为做查询等操作,在此过程中 ...
随机推荐
- 【HTML5】特性
HTML5 建立的一些规则: 新特性应该基于 HTML.CSS.DOM 以及 JavaScript. 减少对外部插件的需求(比如 Flash) 更优秀的错误处理 更多取代脚本的标记 HTML5 应该独 ...
- 启动mysql出现了error the server quit without updating pid file (/var/lib/mysql/localhost.localdomain.pid)
原来是我的mysql日志太多,所以去/data/log/mysql目录(这个目录是从/etc/my.cnf中的log-error确定的)下删除了 rm -rf mysql_binary_log.*的日 ...
- Hark的数据结构与算法练习之鸡尾酒排序
算法说明 鸡尾酒排序又叫定向冒泡排序,鸡尾酒搅拌排序,搅拌排序,涟漪排序,回来排序,快乐小时排序. 鸡尾酒排序是交换排序的一种,它是冒泡排序的一个轻微的变种.冒泡是从低向高比较排序,鸡尾酒从低向高,从 ...
- Selenium自动化中DOM,XPATH,CSS定位Web页面对象的优劣性分析
加速IE浏览器自动化执行效率:Selenium自动化中DOM,XPATH,CSS定位Web页面对象的优劣性分析 1.技术背景 在Web应用中,用户通过键盘在输入框中输入值和鼠标点击按钮,链 ...
- Ajax介绍
AJAX AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. AJA ...
- Winedt打开tex文件报错error reading的解决方案
我刚装就发现winedt打开一些.tex文件时会出现reading error,然后看不到任何文字(网上有人讨论打开是乱码的问题,但是我的是完全看不到任何东西),我的系统winxp,网上有人说好像是和 ...
- sql截取查询
select left(songno,3) as songno from song //截取前3位 select distinct right(left(songno,6),3) as Files ...
- 【python游戏编程之旅】第四篇---pygame中加载位图与常用的数学函数。
本系列博客介绍以python+pygame库进行小游戏的开发.有写的不对之处还望各位海涵. 在上一篇博客中,我们学习了pygame事件与设备轮询.http://www.cnblogs.com/msxh ...
- BZOJ3217 : ALOEXT
替罪羊树套Trie,Trie合并用线段树合并,注意常数优化. 顺便AC800题纪念~~~ #include<cstdio> #include<cmath> #include&l ...
- 【wikioi】1403 新三国争霸(dp+kruskal)
http://wikioi.com/problem/1403/ 一开始的确感觉和bzoj1003很像,不同的是这里还要求联通,求最小的边. 我们可以想到用最小生成树(为嘛我自己想不到呢..) 我们可以 ...