JAVA 入门学习之路-apache-dbutils-BeanUtils使用详解。
来源:互联网 发布:json date 编辑:程序博客网 时间:2024/05/07 11:33
使用非常简单,首先导入需要的jar包,但是在使用中需要 DataSource或者Connection 这需要自己写Utils类,或者用C3P0或者DBCP获取,如有不解详见上篇博客:
JAVA 入门学习之路-数据源/连接池-DBCP和C3P0
用例如下:
以下构造参数如果没传数据源,那么必须在使用查询、更新等操作时选择传递Connection的方法。
//这里我使用了C3P0数据源。QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());qr.query("SELECT * FROM users WHERE NAME=? AND pwd=?;", new BeanHandler<User>(User.class), name, pwd);
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());qr.query("SELECT * FROM books;", new BeanListHandler<Books>(Books.class));
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());qr.update("INSERT INTO books VALUES(?,?,?,?,?,?)", book.getId(), book.getName(), book.getPrice(), book.getNum(), book.getLb(), book.getContent());
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());qr.query("SELECT * FROM books where id=?;", new BeanHandler<Books>(Books.class), id);
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());qr.update("update books set name=?,price=?,num=?,lb=?,content=? where id=?", book.getName(), book.getPrice(), book.getNum(), book.getLb(), book.getContent(), book.getId());
看到这里,是不是觉得用apache的dbutils进行CRUD很简单很方便呢。
下面再介绍一种很实用的工具吧,同属apache公司的BeanUtils。
同上先导入jar包,这里需要使用两个:
commons-beanutils-1.8.3.jarcommons-logging-1.1.1.jar
BeanUtils.setProperty(bean, name, value);其中bean是指你将要设置的对象,name指的是将要设置的属性(写成”属性名”),value(从配置文件中读取到到的字符串值)BeanUtils。copyProperties(bean, name, value),和上面的方法是完全一样的。使用哪个都可以ConvertUtils.register(Converter converter , ..),当需要将String数据转换成引用数据类型(自定义数据类型时),需要使用此方法实现转换。BeanUtils.populate(bean,Map),其中Map中的key必须与目标对象中的属性名相同,否则不能实现拷贝。BeanUtils.copyProperties(newObject,oldObject),实现对象的拷贝
//需要给一个映射的数据源Map。Map<String, String[]> map;//将map里面的数据通过反射,将数据映射到实体类。BeanUtils.populate(book, map);//需要注意,实体类中声明的变量需要与map对应。
ConvertUtils.register(Converter converter , ..)方法用例:
//使用日起转换器工具类ConvertUtils.register(new DateLocaleConverter(), Date.class);
public void test() throws Exception { Map map = new HashMap(); map.put("name", "xiazdong"); map.put("age", "20"); map.put("birth", "2010-10-10"); ConvertUtils.register(new DateLocaleConverter(), Date.class); Person p = new Person(); BeanUtils.populate(p, map); System.out.println(p.getAge()); System.out.println(p.getBirth().toLocaleString()); }
ConvertUtils.register(new Converter() { //自定义日期类型转换器 @Override public Object convert(Class type, Object value) { //type:目前需要转换的数据类型 value:目前参数的值 //目标:将字符串转换为日期 if(type != Date.class) return null; if (value == null || "".equals(value.toString().trim())) { return null; } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日"); Date date = null; try { date = dateFormat.parse((String)value); } catch (ParseException e) { throw new RuntimeException(e); } return date; } }, Date.class); //Date.class表示要转换的成引用类型,Date类型不是基本数据类型,所以需要一个转换器进行相应的转换,同样该功能属于BeanUtils
下面传几个使用案例,实现了登陆、查看列表、修改单个商品、增加单个商品、删除等功能,数据均来自数据库真实数据,非模拟。
0 0
- JAVA 入门学习之路-apache-dbutils-BeanUtils使用详解。
- JDBC学习之路之apache dbutils
- Apache的DBUtils使用详解
- Apache的DBUtils使用详解
- Apache之DBUtils的使用
- Apache的BeanUtils的使用入门
- Apache DBUtils入门(一)
- Apache DBUtils入门(二)
- Apache Commons DbUtils 入门
- Java工具类之Apache的BeanUtils
- Apache DbUtils详解
- BeanUtils组件和DbUtils组件的使用
- apache commons DbUtils学习
- Apache-DbUtils学习
- Apache DBUtils使用总结
- Apache dbutils使用示例
- Apache DBUtils使用总结
- Apache DBUtils使用总结
- Zookeeper集群版安装
- Android开发——ViewPage制作滑动导航界面
- linux下设置tomcat脚本启动
- MD 色板参考
- scrapy使用
- JAVA 入门学习之路-apache-dbutils-BeanUtils使用详解。
- 水面无人艇局部危险避障算法研究 结论
- [问题记录]log4j使用记录
- 怎么安装MySQL,安装MySQL遇到的一些问题!!!!!!
- 去掉dialog的黑边
- 普林斯顿微积分读本——第二章 三角学回顾(读书笔记)
- 显控触摸屏SA-4.3A下载程序提示:选择的HMI型号不匹配
- BGD、SGD与人工神经网络
- python2以及pip windows 安装