您好,欢迎访问一九零五行业门户网

用JdbcTemplateTool配合JdbcTemplate实现更便捷的数据库操作

jdbctemplatetool spring 出品的 jdbctemplate 对于不想使用hibernate或者ibatis那样需要大量学习成本而且还想获得对象化的人来说是很好用的。但是 jdbctemplate还是有很多不足之处或者说是缺点。比如你没法像hibernate那样直接传一个对象给它让他拆分成sql
jdbctemplatetool
spring 出品的 jdbctemplate 对于不想使用hibernate或者ibatis那样需要大量学习成本而且还想获得对象化的人来说是很好用的。但是 jdbctemplate还是有很多不足之处或者说是缺点。比如你没法像hibernate那样直接传一个对象给它让他拆分成sql并保存起来,当然这也是可以理解的,毕竟它并没有要求你去写 hbm.xml 文件所以无法知道你哪些字段要映射,哪些不要等等。又比如jdbctemplate 可以帮忙把一个查询结果传化为一个对象列表,但是你需要查阅一些资料才知道要用 beanpropertyrowmapper 。如果下次要用的时候又忘记了这个类,又要查一次或者翻以前的代码来看,其实完全可以提供一个方法直接传一个po类进去自动创建 beanpropertyrowmapper 。基于以上的一些不足之处,我建立了 jdbctemplatetool 它有以下特性:
把查询结果转换为po列表,不需要调用 beanpropertyrowmapper传一条统计sql比如 a select count(1) from table 可以直接返回一个数字作为结果,不需要自己实现中间步骤。可以直接把一个po类存到数据库通过po类和一个id可以获取到该对象通过po类可以直接update数据库记录不需要实现 batchpreparedstatementsetter, 就可以批量update通过一个对po对象删除对应的数据库记录依然可以使用原始的 jdbctemplate目前只在mysql上测试.
maven 依赖
org.crazycakejdbctemplatetool1.0.4-release
快速开始step 1. 创建一个maven项目创建一个maven项目叫 testjtt. 添加 jdbctemplatetool 依赖到 pom.xml. 再添加以下依赖到 pom.xml.
junitjunit4.11testorg.springframeworkspring-context3.2.2.releasetestcom.mchangec3p00.9.2.1testmysqlmysql-connector-java5.1.19testorg.springframeworkspring-test3.2.2.releasetest
最好使用 1.6+ jdk. 我并没有在 1.5 下测试
step 2. 创建测试数据库创建一个测试的数据库叫 jtt_test 创建一个用户 travis 不要分配密码. 赋予jtt_test的权限给 travis .
create user 'travis'@'%' identified by '';grant all on jtt_test.* to 'travis'@'%';flush privileges;
创建一张表 employee 插入一些测试数据.
drop table if exists `employee`;create table `employee` ( `id` int(11) not null, `name` varchar(300) not null, `join_date` datetime not null, `age` int(11) not null, primary key (`id`)) engine=innodb default charset=utf8;/*data for the table `employee` */insert into `employee`(`id`,`name`,`join_date`,`age`) values (1,'jack','2014-09-22 00:00:00',23),(2,'ted','2014-08-30 00:00:00',25),(3,'jim','2014-06-22 00:00:00',33);
step 3. 配置一下spring在test文件夹下创建 resources 文件夹. 添加 resources 到 source folder 修改输出为 target/test-classes 创建 spring.xml 在 test/resources 里面
jdbc:mysql://localhost:3306/jtt_test?characterencoding=utf8com.mysql.jdbc.drivertravis
step 4. 创建po类创建 employee.java
import java.sql.timestamp;import javax.persistence.id;public class employee { private integer id; private string name; private timestamp joindate; private integer age; @id public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public timestamp getjoindate() { return joindate; } public void setjoindate(timestamp joindate) { this.joindate = joindate; } public integer getage() { return age; } public void setage(integer age) { this.age = age; }}
step 5. 创建测试用例创建 hellojtttest.java
import static org.hamcrest.corematchers.is;import static org.junit.assert.assertthat;import java.util.list;import org.crazycake.jdbctemplatetool.jdbctemplatetool;import org.junit.test;import org.springframework.test.context.contextconfiguration;import org.springframework.test.context.junit4.abstractjunit4springcontexttests;@contextconfiguration(locations={classpath:spring.xml})public class hellojtttest extends abstractjunit4springcontexttests{ @test public void testsave(){ jdbctemplatetool jtt = super.applicationcontext.getbean(jdbctemplatetool,jdbctemplatetool.class); employee e = new employee(); e.setid(4); e.setname(billy); date now = new date(); e.setjoindate(new timestamp(now.gettime())); e.setage(33); try { jtt.save(e); } catch (exception e1) { e1.printstacktrace(); } }}
step 6. 启动!运行测试用例,等待绿色条。然后去数据库会看到多了一条记录 :
idnamejoin_dateage
4 billy 2014-09-24 22:51:20 33
高级教程以下是各个方法的详细介绍
list把查询结果转换为po列表,不需要调用 beanpropertyrowmapper 。 自动根据数据库的列将下划线转为驼峰命名规则映射类的属性.
@testpublic void testlist(){ jdbctemplatetool jtt = super.applicationcontext.getbean(jdbctemplatetool,jdbctemplatetool.class); list es = jtt.list(select * from employee where age
count传一条统计sql比如 a select count(1) from table 可以直接返回一个数字作为结果,不需要自己实现中间步骤。
@testpublic void testcount() throws ioexception, sqlexception { jdbctemplatetool jtt = super.applicationcontext.getbean(jdbctemplatetool,jdbctemplatetool.class); int total = jtt.count(select count(1) from employee, null); assertthat(total,is(4)); }
save可以直接把一个po类存到数据库。如果你不想把某个列映射为数据库字段可以使用 @trasient 注解在getter上
public class student { private integer id; private string name; private string nothing; public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } @transient public string getnothing() { return nothing; } public void setnothing(string nothing) { this.nothing = nothing; }}
这个字段会被跳过
@testpublic void testsave() throws exception { jdbctemplatetool jtt = super.applicationcontext.getbean(jdbctemplatetool,jdbctemplatetool.class); student s = new student(); s.setname(michael); s.setnothing(nothing); jtt.save(s);}
get通过po类和一个id可以获取到该对象。但是前提是需要在主键的getter上标上 @id 注解
@idpublic integer getid() { return id;}
例子
@testpublic void testget() throws noidannotationfoundexception, nocolumnannotationfoundexception, ioexception, sqlexception { jdbctemplatetool jtt = super.applicationcontext.getbean(jdbctemplatetool,jdbctemplatetool.class); employee e = jtt.get(employee.class, 3); assertthat(e.getname(),is(jim));}
update自动根据po类更新数据库. 记得增加 @id .
@testpublic void testupdate() throws exception { jdbctemplatetool jtt = super.applicationcontext.getbean(jdbctemplatetool,jdbctemplatetool.class); employee e = jtt.get(employee.class, 1); e.setage(23); jtt.update(e);}
batchupdate批量更新
@testpublic void testbatchupdate() throws sqlexception, ioexception { build(); jdbctemplatetool jtt = super.applicationcontext.getbean(jdbctemplatetool,jdbctemplatetool.class); list params = new arraylist(); object[] p1 = new object[]{23,jack}; params.add(p1); object[] p2 = new object[]{29,tim}; params.add(p2); jtt.batchupdate(update employee set age = ? where name = ?, params);}
delete删除数据库对象
@testpublic void testdelete() throws exception { jdbctemplatetool jtt = super.applicationcontext.getbean(jdbctemplatetool,jdbctemplatetool.class); employee e = new employee(); e.setid(1); jtt.delete(e);}
getjdbctemplate你依然可以使用原始的 jdbctemplate. 调用 jdbctemplatetool.getjdbctemplate() to getjdbctemplate 就可以了。
其它类似信息

推荐信息