- 浏览: 204841 次
- 性别:
- 来自: 深圳
文章分类
最新评论
-
jongde1:
Axure太难学了,分享mockplus工具,有兴趣可以去了解 ...
Axure RP 原型设计工具 -
di1984HIT:
这里面提到了好几种解决办法。
Spring AOP对日志记录、Exception日志记录 -
di1984HIT:
学习一下。
spring struts2 零配置 -
di1984HIT:
不错,不错啊
Struts2防止表单重复提交 -
di1984HIT:
kettle怎么样啊。
Kettle初探
一般情况下在进行jdbc编程的时候避免不了的要写n多的bean类,用来封装数据库获取的查询结果集,同时又要写n多的rowMapper或者handler来操作装配结果集中的数据进入bean中,最近研究了下自己写了一个对java bean自动进行rowMapper or handler的类 AutoBoxingRowMapper(其实现了spring jdbc的rowMapper),这是一个通用的rowMapper,大多数情况下可以帮助我们省去写n多的rowMapper类。
(注:在应用该类之前应该约定你的java bean中的属性名字的与sql查询中列名不区分大小写匹配,该类保证结果集与bean之间满足toLowerCase(columnName) = toLowerCase(propertyName)时才自动装配)
代码如下:
应用时,DAO中类似代码示例:
(注:在应用该类之前应该约定你的java bean中的属性名字的与sql查询中列名不区分大小写匹配,该类保证结果集与bean之间满足toLowerCase(columnName) = toLowerCase(propertyName)时才自动装配)
代码如下:
package com.loansystem.common.util; import java.beans.IntrospectionException; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.support.JdbcUtils; /** * * 对查询数据结果集进行bean自动装箱 * (注:结果集与bean之间满足toLowerCase(columnName) = toLowerCase(propertyName))时才自动装箱 */ public class AutoBoxingRowMapper implements RowMapper { // protected final Log logger = LogFactory.getLog(this.getClass()); private boolean stringNotEmpty = true;//结果集中由字段值全部以string类型返回其值,并且为空时以空串""返回 private Class<Object> beanClass;//即将要进行包装的bean的Class private Map<String,PropertyDescriptor> beanPropertyMap = new HashMap<String,PropertyDescriptor>();//即将要进行包装的bean的属性描述,以Map形式保存 public boolean isStringNotEmpty() { return stringNotEmpty; } public void setStringNotEmpty(boolean stringNotEmpty) { this.stringNotEmpty = stringNotEmpty; } public Class<Object> getBeanClass() { return beanClass; } public void setBeanClass(Class<Object> beanClass) { this.beanClass = beanClass; } public Map<String, PropertyDescriptor> getBeanPropertyMap() { return beanPropertyMap; } public void setBeanPropertyMap(Map<String, PropertyDescriptor> beanPropertyMap) { this.beanPropertyMap = beanPropertyMap; } /** * 自动装配sql查询结果集的各列值到beanClass所指定的bean中的对应属性上,满足toLowerCase(columnName) = toLowerCase(propertyName)时进行装配 * @param beanClass - 要装配的bean的java.lang.Class */ @SuppressWarnings({"rawtypes", "unchecked"}) public AutoBoxingRowMapper(Class beanClass){ super(); this.beanClass = beanClass; //初始化beanClass对应的bean的所有属性 PropertyDescriptor[] props = null; try { props = Introspector.getBeanInfo(this.getBeanClass(), Object.class) .getPropertyDescriptors(); if (props != null) { for (int i = 0; i < props.length; i++) { this.beanPropertyMap.put(props[i].getName().toLowerCase().trim(), props[i]); } } } catch (IntrospectionException e) { // logger.error(e, e); } } /** * 自动装配sql查询结果集的各列值到beanClass所指定的bean中的对应属性上,满足toLowerCase(columnName) = toLowerCase(propertyName)时进行装配 * @param beanClass - 要装配的bean的java.lang.Class * @param stringNotEmpty - 是否将结果集中由字段值全部以string类型返回其值,并且为空时以空串""返回,default true */ @SuppressWarnings({"rawtypes"}) public AutoBoxingRowMapper(Class beanClass,boolean stringNotEmpty){ this(beanClass); this.stringNotEmpty = stringNotEmpty; } public Object mapRow(ResultSet rs, int rowNum) throws SQLException { Object defaultInstance = null; try { defaultInstance = this.getBeanClass().newInstance();//获取bean的实例 } catch (Exception e) { // logger.error(e, e); } if(defaultInstance == null){ // logger.error("attempt to new a instance of class named : " + this.getBeanClass().getName() + " but failed!!!"); return null; } //获取元数据 ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); PropertyDescriptor propertyDescriptor = null; try { for (int i = 1; i <= columnCount; i++) { String key = getColumnKey(JdbcUtils.lookupColumnName(rsmd, i));//获取resultset结果集中第i列对应的列名 Object obj = getColumnValue(rs, i, key);//获取resultset结果集中第i列对应的值 propertyDescriptor = this.beanPropertyMap.get(key); if(propertyDescriptor == null){//当前列名key对应的属性在bean中找不到,给予警告 // logger.warn("no corresponding property defined in class : " + this.getBeanClass().getName() + " for sql query resultset column '" + key + "' !!!"); continue; }else{////当前列名key对应的属性在bean中找到,则调用其setXxx方法设置其值 propertyDescriptor.getWriteMethod().invoke(defaultInstance, obj); } } } catch (Exception e) { // logger.error(e, e); } return defaultInstance; } protected String getColumnKey(String columnName) { return columnName == null ? null : columnName.toLowerCase().trim(); } @SuppressWarnings("rawtypes") protected Object getColumnValue(ResultSet rs, int index, String columnName) throws SQLException { Object val = null; if(stringNotEmpty){//所有列以string类型解析(前提做到要约定你的bean中的所有属性都定义成string类型) val = rs.getString(index) == null ? "" : rs.getString(index); }else{//所有类型以其bean类中对应的属性类型进行解析 PropertyDescriptor propertyDescriptor = this.beanPropertyMap.get(columnName); if(propertyDescriptor != null){ Class propertyTypeClass = propertyDescriptor.getPropertyType(); String className = propertyTypeClass.getName(); if(className.equals("int") || className.equals(Integer.class.getName())){ val = rs.getInt(index); return val; }else if(className.equals("short") || className.equals(Short.class.getName())){ val = rs.getShort(index); return val; }else if(className.equals("byte") || className.equals(Byte.class.getName())){ val = rs.getByte(index); return val; }else if(className.equals("long") || className.equals(Long.class.getName())){ val = rs.getLong(index); return val; }else if(className.equals("float") || className.equals(Float.class.getName())){ val = rs.getFloat(index); return val; }else if(className.equals("double") || className.equals(Double.class.getName())){ val = rs.getDouble(index); return val; }else if(className.equals("char") || className.equals(Character.class.getName())){ val = rs.getString(index); return val; }else if(className.equals(String.class.getName())){ val = rs.getString(index); return val; }else if(className.equals(java.util.Date.class.getName())){ val = rs.getDate(index); return val; }else if(className.equals(java.math.BigDecimal.class.getName())){ val = rs.getBigDecimal(index); return val; }else{//Object val = rs.getObject(index); return val; } }else{ val = JdbcUtils.getResultSetValue(rs, index); } } return val; } }
应用时,DAO中类似代码示例:
List<Object> list = this.getJdbcTemplate().query(sql, new AutoBoxingRowMapper(Department.class,false)); Map<String,Object> map = this.callProcedure(procName, new Object[]{deptNo,0,5}, new AutoBoxingRowMapper(Employee.class,false),true);
发表评论
-
Linux下部署多个Tomcat多个域名
2015-12-12 19:02 3644一、安装JDK 1、安装jdk-7u79-linux-x64. ... -
linux下安装swftools和openOffice
2015-07-03 17:09 697最近公司实现一个仿豆丁网百度文库阅读器的功能,需要用到两个软件 ... -
redis Java develop
2014-10-23 18:01 6021. http://javacrazyer.iteye.com ... -
验证码 原理 破解
2014-08-20 17:52 579验证码 原理 破解 reference: http://bl ... -
HttpClient 学习经验
2014-08-14 11:01 596HttpClient学习经验 HttpCl ... -
P2P resources
2013-12-09 18:24 9231.P2P导航收录 http://www.p2peye.com ... -
运用加密技术保护Java源代码
2013-06-08 08:38 1066运用加密技术保护Java源代码 http://www.ibm ... -
log4j.properties配置详解
2013-03-15 17:00 0log4j.properties配置详解 Log4J的配置文 ... -
Struts 2 studing
2012-12-28 17:28 7051. Struts 2的基石——拦截器(Interceptor ... -
J2EE项目异常处理
2012-12-26 11:08 1041J2EE项目异常处理 为什 ... -
如何将基于 Struts、Spring 和 Hibernate 的应用从 Tomcat 迁移到 WebSphere Application Server
2012-12-21 10:28 1140引言 现在很多的企业都 ... -
详解spring事务属性
2012-12-20 10:22 779Spring声明式事务让我们从复杂的事务处理中得到解脱。使得我 ... -
Java List Copy,Remove容易出现的问题
2012-11-15 03:08 967懒程序员,在代码越写越多的情况下,总想着使用把代码精简一 ... -
MySQL---ORACLE序列解决方案
2012-11-13 00:55 1157MySQL自增长与Oracle序列的区别: 自增长只能用于表 ... -
使用反射循环查找所有父类属性
2012-11-02 01:28 2090使用反射循环查找所有父类属性 ... -
list,set,map,数组间的相互转换
2012-11-02 01:25 919list,set,map,数 ... -
URL encoding 乱码处理
2012-10-10 15:53 849搞了两三天的乱码处理,试了很多方法,过滤器啊,编码转换啊,试来 ... -
JAVA 按任意角度旋转图片,并生成新的旋转后图片
2012-07-11 11:03 3612JAVA 按任意角度旋转图片,并生成新的旋转 ... -
QR Code
2012-06-12 12:29 01. 在线生成QR Code 网站 http://www. ... -
convert BufferedImage to byte[]
2012-06-12 11:26 1204How to convert BufferedImage to ...
相关推荐
Java利用Freemarker模板自动生成dto、dao、rowmapper、bo、service代码,代码可以直接复制到项目中使用,代码是基于pring框架的
利用java小窗口读取mdb数据库中的数据,并将其显示出来
它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。 模板编写为FreeMarker Template Language (FTL)。它是简单的,专用的语言, 不是 像PHP那样成熟的编程语言。 那就意味着要...
SBORM只是针对spring jdbc的一些不方便的地方,做了一些封装,更加简化日常的开发工作,基于spring jdbc的RowMapper自动实现对象映射,也勉强算的上叫ORM,只是大部分功能已经由spring jdbc实现了。 平时不太...
* 调用此方法,需要自定义一个类实现RowMapper接口,该接口返回泛型,该泛型为该方法返回值集合的泛型 * @param sql * :SQL语句,参数用"?"表示 * @param rm * :rm为RowMapper接口的实现类所产生的对象 ...
主要是如何利用@Query注释来完成增加,删除,的操作
RowMapper RowMapperResultSetExtractor RuleBasedTransactionAttribute RuntimeBeanNameReference RuntimeBeanReference RuntimeTestWalker SavepointManager ScheduledExecutorFactoryBean ...
3.5.2 使用属性setting方法进行注入 21 3.5.3 装配list集合 22 3.5.4 装配set集合 22 3.5.5 装配map 22 3.5.6 装配Properties 23 3.6 注解注入 23 注解注入拓展: 23 3.6.1 @Autowired 26 3.6.2 @Qualifier 27 3.6.3...
import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import org.springframework.dao.DataAccessException; import org.springframework.jdbc.core.RowMapper; ...
spring 中对大数据的处理,包括clob,blob的数据。比之jdbc下简便很多。
19.6.1.使用Localizer进行自动本地化(“推”模型) 19.6.2.使用Localizer 19.6.3.手动应用资源(“拉”模型的本地化) 19.6.4.在Web应用程序中进行图像本地化 19.6.5.全局资源 19.6.6.用户语言文化管理 19.6.6.1. ...
springjdbc-constructor-mapper.zip,spring的rowmapper实现,用于对其构造函数进行注释的类
mysql-connector-java-5.1.9.jar spring-dao.jar spring-jdbc.jar 2,修改项目目录下的:red5-web.properties 添加:db.driver=com.mysql.jdbc.Driver db.url=jdbc:mysql://localhost:3306/test db.username=...
springMVC4.0.6+spring4.0.6+hibernate4.3.6框架,基本的包结构,dao基础类的封装,rowmapper,分页等工具类的封装。
fmgVen是配置Java ORM工具的约定。 fmgVen是重量级ORM工具的替代方法,在该工具中,映射需要配置,并且由于延迟加载而产生许多问题,等等。它无需明确的映射配置... 它是Spring JdbcTemplate / RowMapper的高级形式。
easyjdbc-spring-boot-startereasyjdbc-spring-boot-starter,支持部分常用的JPA注解,使得经过注解的实体可以像Hibernate,jpa一样进行增、删、改和获取。SQL构造工具、链式API等让查询操作更为灵活。动态实体映射...
n 源码分析-BeanFactory类视图和常用工厂说明 n 源码分析-AnnotationConfigApplicationContext的register方法 n 源码分析-AnnotationConfigApplicationContext的scan方法 n 源码分析-AbstractApplicationContext的...
rowMapper public double findBookPriceByIsbn(String isbn); //更新书的库存,使书号对应的库存减num,若库存不足,则给出提示,并且不更新 public void updateBookStock(String isbn, int num); //更新用户的...
基于spring jdbc的RowMapper自动实现对象映射,大部分功能已经由spring jdbc实现了。2、数据类型的设计:集成了JFinal和SBORM的优点,对字段的大小写不敏感,对强类型实体、弱类型实体和非实体的支持。3、统一的接口...