zoukankan      html  css  js  c++  java
  • DBUtils与BeanUtils

    1.DBUtils 

    官方文档    https://commons.apache.org/proper/commons-dbutils/examples.html

    1)依赖

    <dependency>
        <groupId>commons-dbutils</groupId>
        <artifactId>commons-dbutils</artifactId>
        <version>1.7</version>
    </dependency>
    

    2)创建表

    create table  persons(
    id  int primary key  auto_increment,
    name  varchar(10),
    age int
    )engine=InnoDB default charset=utf8
    

     3)导入jar

    <dependency>
    			<groupId>commons-dbutils</groupId>
    			<artifactId>commons-dbutils</artifactId>
    			<version>1.7</version>
    </dependency>
    

     4)DBUtils的基本用法

    public class DBUtilsTest {
    	public static void main(String[] args) throws ClassNotFoundException, IOException, SQLException {
    		Connection connection = PrepareStatementTest.getConnection("config.properties");
    		connection.setAutoCommit(false);
    		//其中可以放入数据源去定义,那么会相应的少去con的设置和管理
    		QueryRunner queryRunner = new QueryRunner();
    		// 增加
    		// String sql="insert into persons(name,age) values(?,?)";
    		// int execute = queryRunner.execute(connection, sql, "丽丽","23");
    		// 删除
    		// String sql ="delete from persons where id=?";
    		// int execute = queryRunner.execute(connection, sql, 2);
    		// 修改
    		// String sql ="update persons set name=? ,age=? where id=?";
    		// int execute = queryRunner.execute(connection, sql, "苗苗",10,1);
    		// queryRunner.update(conn, sql, params)
    		// System.out.println(execute);
    		// 添加
    		// String sql="insert into persons(name,age) values(?,?)";
    		// 返回值是 ResultSetHandler<T>的值
    		// queryRunner.insert(connection, sql, new ResultSetHandler<String>() {
    		// @Override
    		// public String handle(ResultSet rs) throws SQLException {
    		// System.out.println("插入数据");//输出插入数据
    		// return null;
    		// }
    		// },"丽丽",22);
    		String sql = "select * from persons  where id>=1";
    		// 把返回值装入person对象 或者返回第一条装入对象
    		// Person query = queryRunner.query(connection,sql,new
    		// BeanHandler<>(Person.class));
    		// 查询的所有对象放入集合
    		//返回值是 ResultSetHandler<T>的值
    		List<Person> query = queryRunner.query(connection, sql, new BeanListHandler<>(Person.class));
    		System.out.println(query);
    		connection.commit();
    		DbUtils.rollbackAndClose(connection);
    		/* ResultSetHandler<T>的子类,
    		 * ArrayHandler :将ResultSet中第一行的数据转化成对象数组
    		 * ArrayListHandler将ResultSet中所有的数据转化成List,List中存放的是Object[]
    		 * BeanHandler :将ResultSet中第一行的数据转化成类对象
    		 * BeanListHandler :将ResultSet中所有的数据转化成List,List中存放的是类对象
    		 * ColumnListHandler :将ResultSet中某一列的数据存成List,List中存放的是Object对象
    		 * KeyedHandler :将ResultSet中存成映射,key为某一列对应为Map。Map中存放的是数据
    		 * MapHandler :将ResultSet中第一行的数据存成Map映射
    		 * MapListHandler :将ResultSet中所有的数据存成List。List中存放的是Map
    		 * ScalarHandler :将ResultSet中一条记录的其中某一列的数据存成Object 
    		 */
    	}
    }
    

     2.BeanUtils 

     官方文档:https://commons.apache.org/proper/commons-beanutils/

    1)依赖

       <dependency>
          <groupId>commons-beanutils</groupId>
          <artifactId>commons-beanutils</artifactId>
          <version>1.9.3</version>
        </dependency>
     

    2)基本使用

    package cn.test;
    
    import org.apache.commons.beanutils.BeanUtils;
    import org.junit.Before;
    import org.junit.Test;
    
    import java.lang.reflect.InvocationTargetException;
    import java.util.HashMap;
    
    public class BeanUtilsTest {
        private Person person=null;
    
        @Before
        public  void  init(){
             this.person = new Person("张三", "23");
        }
      //setProperty
        @Test//给指定对象bean的指定name属性赋值为指定值value,不能存在什么也不发生
        public  void   testSetProperty() throws InvocationTargetException, IllegalAccessException {
            BeanUtils.setProperty(person, "name", "李四");//不支持内部类的反射
            System.out.println(person);
        }
      //getProperty
        @Test//获取指定对象bean指定name属性的值,不存在就报错
        public  void   testGetProperty() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
            String name = BeanUtils.getProperty(person, "name");//不支持内部类的反射
            System.out.println(name);
        }
      //copyProperties
        @Test//将对象orig的属性值赋值给对象dest对象对应的属性
        public  void   testCopyProperties() throws InvocationTargetException, IllegalAccessException {
            Person p = new Person();
            BeanUtils.copyProperties(p , person);
            System.out.println(person);
            System.out.println(p);
        }
      //populate
        @Test//将一个Map集合中的数据封装到指定对象bean中
        public  void   testPopulate() throws InvocationTargetException, IllegalAccessException {
            Person p = new Person();
            HashMap<String, String> map = new HashMap<>();
            map.put("name", "张三");
            map.put("adress", "qwe");
            BeanUtils.populate(p, map);
            System.out.println(p);
        }
    
    public class Person {
        private   String  name;
        private   String   age;
    ..........
    

     3)添加类型转换器

    public static<T> T     populate( T  t, Map  map) throws InvocationTargetException, IllegalAccessException {
            DateConverter dateConverter = new DateConverter();//创建日期格式转换器对象
            dateConverter.setPattern("yyyy-MM-dd");//设置格式
            ConvertUtils.register(dateConverter, Date.class);//注册
            BeanUtils.populate(t,map);
            return  t;
       }
    

     具有的转换器还有如下

  • 相关阅读:
    金融系列7《动态数据认证》
    PHP异常处理详解
    C语言中的宏定义
    PHP SOCKET编程
    yii实现级联下拉菜单
    AR的一些常见的操作
    IP地址的三种表示格式及在Socket编程中的应用
    时间管理
    socket阻塞与非阻塞,同步与异步、I/O模型
    程序人生 PHP工程师
  • 原文地址:https://www.cnblogs.com/gg128/p/9788423.html
Copyright © 2011-2022 走看看