beanutils用法,解决hibernate映射弊端

2023-12-10 08:32

本文主要是介绍beanutils用法,解决hibernate映射弊端,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Common BeanUtils组件方便了对JavaBean的使用。其中的一些类方法,使我们使用JavaBean得到了便利。

 

使用Common BeanUtils组件需要三个Jar包,分别是

commons-beanutils-1.8.0-BETA.jar

commons-logging-1.1.1.jar

commons-logging-api-1.1.1.jar


 

下面用四个例子说明该组件的三个优点。

 

例子一:

创建三个Java文件,分别为

Address.java

Profile.java

User.java

 

在写一个类文件递进调用函数,命名:BeanUtilsExample1.java,源码:

import java.util.Map;
import java.util.HashMap;
import java.util.GregorianCalendar;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;

public class BeanUtilsExample1 {
    
private User prepareData() {
        Profile profile 
= new Profile();
        profile.setEmail(
"shiyangxt@126.com");
        profile.setBirthDate(
new GregorianCalendar(3212910).getTime());
        Map
<String, String> phone = new HashMap<String, String>();
        phone.put(
"home""11011011");
        phone.put(
"office""82826905");
        profile.setPhone(phone);
        Address[] address 
= new Address("中国""北京""100120""天安门北大街888号"),
                
new Address("中国""广州""100120""石牌村666号") }
;
        profile.setAddress(address);

        User user 
= new User();
        user.setUserId(
new Long(123456789));
        user.setUsername(
"shiyang");
        user.setPassword(
"12345");
        user.setProfile(profile);
        
return user;
    }


    
public static void main(String[] args) {
        BeanUtilsExample1 example 
= new BeanUtilsExample1();
        User user 
= example.prepareData();
        
try {
            System.
out.println("输出对象的属性值---------------------------------");
            System.
out.println(BeanUtils.getProperty(user, "userId"));
            System.
out.println(BeanUtils.getProperty(user, "username"));//返回字符型
            System.out.println(PropertyUtils.getProperty(user, "username"));//返回对象类型
            System.out.println(BeanUtils.getProperty(user, "profile.email"));//重点
            System.out
                    .println(BeanUtils.getProperty(user, 
"profile.birthDate"));//重点
            System.out.println(BeanUtils.getProperty(user,
                    
"profile.phone(home)"));//重点
            System.out.println(BeanUtils.getProperty(user,
                    
"profile.phone(office)"));//重点
            System.out.println(BeanUtils.getProperty(user,
                    
"profile.address[0].city"));//重点
            System.out.println(BeanUtils.getProperty(user,
                    
"profile.address[1].city"));//重点

            User user2 
= new User();
            BeanUtils.copyProperties(user2, user);
            
//两层拷贝,基本类型复制值,对于引用类型(除String,封装类型外)复制地址值。
            System.out.println("输出复制属性的属性值-------------------------------");
            System.
out.println(BeanUtils.getProperty(user, "username"));
            System.
out
                    .println(BeanUtils.getProperty(user, 
"profile.birthDate"));//重点
            System.out.println(BeanUtils.getProperty(user,
                    
"profile.phone(home)"));//重点
            System.out.println(BeanUtils.getProperty(user,
                    
"profile.address[0].addr"));//重点

            System.
out.println("输出复制属性修改以后的属性值---------------------");
            BeanUtils.setProperty(user2, 
"userId"new Long(8888888));
            PropertyUtils.setProperty(user2, 
"username""ahah");
            BeanUtils.setProperty(user2, 
"profile.email""shiyangxt@126.com");//重点
            BeanUtils.setProperty(user2, "profile.birthDate",//重点
                    new GregorianCalendar(190025).getTime());
            BeanUtils.setProperty(user2, 
"profile.address[0]"new Address(
                    
"中国""深圳""600600""深北大道111号"));//重点
            System.out.println(BeanUtils.getProperty(user2, "userId"));
            System.
out.println(BeanUtils.getProperty(user2, "username"));
            System.
out.println(BeanUtils.getProperty(user2, "profile"));
            System.
out.println(BeanUtils.getProperty(user2, "profile.email"));//重点
            System.out.println(BeanUtils
                    .getProperty(user2, 
"profile.birthDate"));//重点
            System.out.println(BeanUtils.getProperty(user2,
                    
"profile.address[0].city"));//重点

            System.
out.println("与被复制属性值的对象的比较-------------------------------");
            System.
out.println(BeanUtils.getProperty(user, "userId"));
            System.
out.println(BeanUtils.getProperty(user, "username"));
            System.
out.println(BeanUtils.getProperty(user, "profile"));
            System.
out.println(BeanUtils.getProperty(user, "profile.email"));//重点
            System.out
                    .println(BeanUtils.getProperty(user, 
"profile.birthDate"));//重点
            System.out.println(BeanUtils.getProperty(user,
                    
"profile.address[0].city"));//重点
        }
 catch (Exception e) {
            e.printStackTrace();
        }

    }

}

从中可以看出,

调用一个属性中的方法,只需要加一个“.”即可。精简了操作。

还可以拷贝属性,但要注意是二层拷贝。

还要注意BeanUtils和PropertyUtils的区别。


例子二:

动态创建属性

文件名:BeanUtilsExample2,源码:

import java.util.GregorianCalendar;
import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.commons.beanutils.BeanUtils;

public class BeanUtilsExample2 {
    
//动态创建属性
    public static void main(String args[]) throws Exception {

        LazyDynaBean hh 
= new LazyDynaBean();
        hh.
set("country""中国");
        hh.
set("city""北京");
        hh.
set("postCode""100120");
        hh.
set("addr""aaaaaaa");

        LazyDynaBean bb 
= new LazyDynaBean();
        bb.
set("phone""home""11011011");
        bb.
set("phone""office""111111");
        bb.
set("email""sh@126.com");
        bb.
set("address"0, hh);
        bb.
set("birthDate"new GregorianCalendar(1990329).getTime());

        LazyDynaBean tt 
= new LazyDynaBean();
        tt.
set("userId"new Long(8888888));
        tt.
set("gggg""施杨");
        tt.
set("password""sgsgsgsg");
        tt.
set("dddd", bb);

        System.
out.println(BeanUtils.getProperty(tt, "gggg"));
        System.
out.println(BeanUtils.getProperty(tt, "dddd.birthDate"));
        System.
out.println(BeanUtils.getProperty(tt,
                
"dddd.address[0].addr"));
        System.
out
                .println(BeanUtils.getProperty(tt, 
"dddd.phone(office)"));
    }

}

例子三:

连接Mysql数据库

文件名BeanUtilsExample3.java源码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.ResultSetDynaClass;

public class BeanUtilsExample3 {
    
public static void main(String args[]) throws Exception {
        Connection conn 
= getConnection();
        PreparedStatement ps 
= conn
                .prepareStatement(
"select id,title,time from guestbook2 order by id desc");
        ResultSet rs 
= ps.executeQuery();

        ResultSetDynaClass rsdc 
= new ResultSetDynaClass(rs);//重点,二次封装,对连接对象有依赖
        Iterator itr = rsdc.iterator();
        
while (itr.hasNext()) {
            DynaBean bean 
= (DynaBean) itr.next();
            System.
out.print(bean.get("id"+ "\t");
            System.
out.print(bean.get("title"+ "\t");
            System.
out.println(bean.get("time"));
        }

        conn.close();
    }


    
private static Connection getConnection() {
        String url 
= "jdbc:mysql://localhost:3306/guestbook";
        String username 
= "root";
        String password 
= "hicc";
        Connection conn 
= null;
        
try {
            Class.forName(
"com.mysql.jdbc.Driver");
            conn 
= DriverManager.getConnection(url, username, password);
        }
 catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
 catch (SQLException e) {
            e.printStackTrace();
        }

        
return conn;
    }

}

例子四:

文件BeanUtilsExample4.java,源码:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Iterator;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.RowSetDynaClass;

public class BeanUtilsExample4 {
    
public static void main(String args[]) throws Exception {
        Connection conn 
= getConnection();
        PreparedStatement ps 
= conn
                .prepareStatement(
"select id,title,time from guestbook2 order by id desc");
        ResultSet rs 
= ps.executeQuery();

        RowSetDynaClass rsdc 
= new RowSetDynaClass(rs);
        
//重点,与ResultSetDynaClass的区别
        conn.close();//重点,关闭连接后仍能读取
        Iterator itr = rsdc.getRows().iterator();
        
while (itr.hasNext()) {
            DynaBean bean 
= (DynaBean) itr.next();
            System.
out.print(bean.get("id"+ "\t");
            System.
out.print(bean.get("title"+ "\t");
            System.
out.println(bean.get("time"));
        }

    }


    
private static Connection getConnection() {
        String url 
= "jdbc:mysql://localhost:3306/guestbook";
        String username 
= "root";
        String password 
= "hicc";
        Connection conn 
= null;
        
try {
            Class.forName(
"com.mysql.jdbc.Driver");
            conn 
= DriverManager.getConnection(url, username, password);
        }
 catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
 catch (SQLException e) {
            e.printStackTrace();
        }

        
return conn;
    }

}


这篇关于beanutils用法,解决hibernate映射弊端的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/476572

相关文章

MySQL 中的 CAST 函数详解及常见用法

《MySQL中的CAST函数详解及常见用法》CAST函数是MySQL中用于数据类型转换的重要函数,它允许你将一个值从一种数据类型转换为另一种数据类型,本文给大家介绍MySQL中的CAST... 目录mysql 中的 CAST 函数详解一、基本语法二、支持的数据类型三、常见用法示例1. 字符串转数字2. 数字

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

解读GC日志中的各项指标用法

《解读GC日志中的各项指标用法》:本文主要介绍GC日志中的各项指标用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、基础 GC 日志格式(以 G1 为例)1. Minor GC 日志2. Full GC 日志二、关键指标解析1. GC 类型与触发原因2. 堆

MySQL数据库中ENUM的用法是什么详解

《MySQL数据库中ENUM的用法是什么详解》ENUM是一个字符串对象,用于指定一组预定义的值,并可在创建表时使用,下面:本文主要介绍MySQL数据库中ENUM的用法是什么的相关资料,文中通过代码... 目录mysql 中 ENUM 的用法一、ENUM 的定义与语法二、ENUM 的特点三、ENUM 的用法1

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

MySQL之InnoDB存储引擎中的索引用法及说明

《MySQL之InnoDB存储引擎中的索引用法及说明》:本文主要介绍MySQL之InnoDB存储引擎中的索引用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录1、背景2、准备3、正篇【1】存储用户记录的数据页【2】存储目录项记录的数据页【3】聚簇索引【4】二

mysql中的数据目录用法及说明

《mysql中的数据目录用法及说明》:本文主要介绍mysql中的数据目录用法及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、版本3、数据目录4、总结1、背景安装mysql之后,在安装目录下会有一个data目录,我们创建的数据库、创建的表、插入的

深度解析Python装饰器常见用法与进阶技巧

《深度解析Python装饰器常见用法与进阶技巧》Python装饰器(Decorator)是提升代码可读性与复用性的强大工具,本文将深入解析Python装饰器的原理,常见用法,进阶技巧与最佳实践,希望可... 目录装饰器的基本原理函数装饰器的常见用法带参数的装饰器类装饰器与方法装饰器装饰器的嵌套与组合进阶技巧