JDBC--使用beanutils工具类操作JavaBean
1、在JavaEE中,Java类的属性通过getter,setter来定义;
2、可使用BeanUtils工具包来操作Java类的属性:
--Beanutils是由Apache公司开发,能够方便对Bean类进行简便的操作
--涉及到的包:
(1) BeanUtils相关包
commons-beanutils-1.8.3.jar
commons-beanutils-1.8.3-javadoc.jar
commons-beanutils-1.8.3-javadoc.jar
commons-beanutils-bean-collections-1.8.3.jar
commons-beanutils-core-1.8.3.jar
(2) Logic4j相关包
commons-logging.jar
3、使用举例:
Bean类Customer:
public class Customer {
private int id;
private String name;
private Date birth;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Customer(int id, String name, Date birth) {
super();
this.id = id;
this.name = name;
this.birth = birth;
}
public Customer() {
}
}
使用BeanUtils的setProperty()和getProperty()方法设置和获取属性:
public void testBeanUtils() throws Exception{
Customer customer = new Customer();
//设置customer的name属性
BeanUtils.setProperty(customer, "name", "Bob");
System.out.println(customer);
//获取customer的属性值
String name = (String)BeanUtils.getProperty(customer, "name");
System.out.println(name);
}
4、在JDBC中,当我们在编写DAO中的通用方法来进行查询时,可以使用BeanUtils类来为属性赋值(第32行):
public <T> T get(Class<T> clazz, String sql, Object ...args){
T entity = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
conn = DAO.getConnection();
ps = conn.prepareStatement(sql);
for(int i = 0; i < args.length; i++){
ps.setObject(i + 1, args[i]);
}
rs = ps.executeQuery();
Map<String, Object> map = new HashMap<String, Object>();
ResultSetMetaData rsmd = rs.getMetaData();
if(rs.next()){
for(int i = 0; i < rsmd.getColumnCount(); i++){
String columnLabel = rsmd.getColumnLabel(i + 1);
Object columnValue = rs.getObject(columnLabel);
map.put(columnLabel, columnValue);
}
}
if(map.size() > 0){
entity = clazz.newInstance();
for(Map.Entry<String, Object> entry : map.entrySet()){
String fieldName = entry.getKey();
Object value = entry.getValue();
//使用BeanUtils工具类来为属性赋值
BeanUtils.setProperty(entity, fieldName, value);
/**使用反射的方式为属性赋值
Field field = clazz.getDeclaredField(key);
field.setAccessible(true);
field.set(entity, value);*/
}
return entity;
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return entity;
}
JDBC--使用beanutils工具类操作JavaBean的更多相关文章
- 使用BeanUtils工具类操作Java bean
1.类的属性: 1).在Java EE中,类的属性通过setter和getter定义:类中的setter(getter)方法去除set(get)后剩余的部分就是类的属性 2).而之前叫的类的属性,即成 ...
- 第13天 JSTL标签、MVC设计模式、BeanUtils工具类
第13天 JSTL标签.MVC设计模式.BeanUtils工具类 目录 1. JSTL的核心标签库使用必须会使用 1 1.1. c:if标签 1 1.2. c:choos ...
- DbUtils是Apache出品一款简化JDBC开发的工具类
DbUtils - DbUtils是Apache出品一款简化JDBC开发的工具类 - 使用DbUtils可以让我们JDBC的开发更加简单 - DbUtils的使用: ...
- BeanUtils 工具类
一.BeanUtils 概述 BeanUtils 是阿帕奇提供的一套专门用于将一些数据封装到java对象中的工具类; 名词:javaBean:特定格式的java类称为java ...
- JavaWeb 之 BeanUtils 工具类
在上一个用户登录案例中,当从浏览器接收参数后,还需要创建 JavaBean 类,对其的属性每一项赋值,如果属性少,可以手动完成,但是当属性非常多,这时就发现非常不方便,在这里提供一个可以封装 Java ...
- 利用BeanUtils工具类封装表单数据
一.BeanUtils工具类的使用 1.首先导入BeanUtils工具类的jar包 commons-beanutils-1.8.0.jar commons-logging-1.1.1.jar 2.se ...
- JDBC封装的工具类
1. JDBC封装的工具类 public class JDBCUtil { private static Properties p = new Properties(); private static ...
- 丢弃掉那些BeanUtils工具类吧,MapStruct真香!!!
在前几天的文章<为什么阿里巴巴禁止使用Apache Beanutils进行属性的copy?>中,我曾经对几款属性拷贝的工具类进行了对比. 然后在评论区有些读者反馈说MapStruct才是真 ...
- jdbc连接的工具类
在不实用框架的情况下,有一个jdbc的工具类来进行数据库的连接就再好不过了,下面提供这个工具类DBUtil.java package org.jdbc.test; import java.io.Inp ...
随机推荐
- 【安全运维】Vim的基本操作
i 插入模式 : 末行模式 a 光标后插入 A 切换行末 I 切换行首 o 换行 O 上一行 p 粘贴 u 撤销 yy 复制 4yy 复制四行 dd (剪切)删除一行 2dd (剪切)删除两行 D 剪 ...
- PyQt5复杂控件(树控件、选项卡控件(滚动条控件、多文档控件、停靠控件)
1.树控件的基本使用方法QTreeWidget'''QTreeWidget树控件的使用方法添加图标,添加表格,添加复选框等'''from PyQt5.QtWidgets import *from Py ...
- 27 JavaScript的引入&注释&弹窗&变量常量&数据类型及转换&内存&垃圾回收
JS的引入: 1 内部引入 绑定元素事件如onclick="" 绑定锚点如href="JavaScript:void(0)" script标签引入,注意:如果标 ...
- 吴裕雄 python 神经网络——TensorFlow 队列操作
import tensorflow as tf q = tf.FIFOQueue(2, "int32") init = q.enqueue_many(([0, 10],)) x = ...
- 【Python矩阵及其基础操作】【numpy matrix】
一.矩阵生成 1.numpy.matrix: import numpy as np x = np.matrix([ [1, 2, 3],[4, 5, 6] ]) y = np.matrix( [1, ...
- 关于python 3.x import matplotlib as plt ImportError: DLL load failed: 找不到指定的模块
windows 10下使用conda update --all更新过后,就出现这样的问题了,各种包不能用了,然后在stackoverflow上搜到有人也遇到相同的问题,并通过其中的回答找到了原因,这里 ...
- ES建立索引步骤, 1,index 2.mapping 3,别名
1.建立索引PUT /index_trans_detail 2.建立mappingPOST /index_trans_detail/type_trans_detail/_mapping{ " ...
- Wcf托管在IIS中,HttpContext.Current为空
config中需要配置 <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 另需要在服务类上加 ...
- Centos7虚拟环境virtualenv与virtualenvwrapper的安装及基本使用
一.使用虚拟环境的原因 在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题:亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难.此时,我们需 ...
- 从零构建以太坊(Ethereum)智能合约到项目实战——第24章 IPFS + 区块链
P93 .1-IPFS环境配置P94 .2-IPFS+P .IPNS+P .个人博客搭建 - 如何在IPFS新增一个文件P95 .3-IPFS+P .IPNS+P .个人博客搭建 - 通过ipfs创建 ...