欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 前端技术 > javascript >内容正文

javascript

spring中使用jdbc -凯发k8官方网

发布时间:2024/10/12 javascript 22 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 spring中使用jdbc 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

首先要获得datasource连接池:
要对数据库执行任何的jdbc操作,需要有一个connection.在spring中,connection对象是通过datasource获得的。

有几种方法可以得到datasource, 其中一种方法是使用spring提供的轻量级org.springframework.jdbc.datasource.drivermanagerdatasource,第二种方法是使用org.apache.commons.dbcp.basicdatasource类。

一:使用drivermangerdatasource,这种方法是轻量级的,方便测试

1 public class datasoureprovider { 2 public static drivermanagerdatasource datasource = new drivermanagerdatasource(); 3 4 public static drivermanagerdatasource getinstance() { 5 datasource.setdriverclassname("com.mysql.jdbc.driver"); 6 datasource.set; 7 datasource.setusername("y****"); 8 datasource.setpassword("h*******"); 9 return datasource; 10 } 11 12 @test 13 public void test() { 14 datasoureprovider.getinstance(); 15 try { 16 datasource.getconnection(); 17 } 18 catch (sqlexception e) { 19 e.printstacktrace(); 20 } 21 } 22 }

第5~8行是配置连接数据库所需的信息。

二:使用basicdatasouce创建一个连接池。应为basicdatasource所有属性都是通过setter方法暴露在外面的,我们可以像配置其他srping bean那样配置它
我将数据库连接信息配置在properties文件中,利用spring的org.springframeword.beans.factory.config.propertyplaceholderconfigurer类进行读取装载,可以查看spring_装配bean一文。

书写配置文件applicationcontext.xml:

1 xml version="1.0" encoding="utf-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 5 xmlns:p="http://www.springframework.org/schema/p" 6 xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 7 8 <bean id="dbproperty" class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> 9 <property name="location"> 10 <value>connect.propertiesvalue> 11 property> 12 bean> 13 14 <bean id="mydatasource" class="org.apache.commons.dbcp.basicdatasource"> 15 <property name="driverclassname"> 16 <value>${db.driver}value> 17 property> 18 <property name="url"> 19 <value>${db.url}value> 20 property> 21 <property name="username"> 22 <value>${db.username}value> 23 property> 24 <property name="password"> 25 <value>${db.password}value> 26 property> 27 bean> 28 beans>

第14~27行配置basicdatasource参数,其中中的参数是在connect.propertices配置文件中拿到的。

进行测试:

1 public class datasourceprovider2 { 2 @test 3 public void connecttest() { 4 applicationcontext context = new classpathxmlapplicationcontext( 5 "applicationcontext.xml"); 6 basicdatasource datasource = (basicdatasource) context 7 .getbean("mydatasource"); 8 try { 9 datasource.getconnection(); 10 system.out.println("connect successful"); 11 } 12 catch (sqlexception e) { 13 e.printstacktrace(); 14 } 15 } 16 17 }

使用org.apache.commons.dbcp.basicdatasource需要引入额外的jar包,分别是commons-collections-2.1.1.jar,commons-dbcp-1.4.jar,commons-pool-1.2.jar.为了方便大家,这里有这三个jar包的下载地址:http://pan.baidu.com/share/link?shareid=68214&uk=2198762756#dir/path=/lib/datasource

spring把jdbc中重复的操作建立成了一个模板类org.springframework.jdbc.core.jdbctemplate。

 

使用jdbctemplate:
要使用jdbctemplate,需要为每一个dao配置一个jdbctemplate实例

1 public class studentdaoimp implements studentdao { 2 private jdbctemplate jdbctemplate; 3 4 @override 5 public void setjdbctemplate(jdbctemplate jdbctemplate) { 6 this.jdbctemplate = jdbctemplate; 7 } 8 }

如上,studentdaoimp内配置了一个jdbctemplate对象和它对应的setter方法。这样就可以在spring配置文件中对其进行赋值。

1 xml version="1.0" encoding="utf-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p" 4 xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> 5 6 <bean id="dbproperty" 7 class="org.springframework.beans.factory.config.propertyplaceholderconfigurer"> 8 <property name="location"> 9 <value>connect.propertiesvalue> 10 property> 11 bean> 12 13 <bean id="mydatasource" class="org.apache.commons.dbcp.basicdatasource"> 14 <property name="driverclassname"> 15 <value>${db.driver}value> 16 property> 17 <property name="url"> 18 <value>${db.url}value> 19 property> 20 <property name="username"> 21 <value>${db.username}value> 22 property> 23 <property name="password"> 24 <value>${db.password}value> 25 property> 26 bean> 27 28 <bean id="jdbctemplate" class="org.springframework.jdbc.core.jdbctemplate"> 29 <property name="datasource"> 30 <ref bean="mydatasource"/> 31 property> 32 bean> 33 34 <bean id="studentdao" class="com.sunflower.dao.studentdaoimp"> 35 <property name="jdbctemplate"> 36 <ref bean="jdbctemplate"/> 37 property> 38 bean> 39 beans>

第28~32行是装配jdbctemplate这个bean,其中需要为其设置datasource这个参数,就是我们上面的到的datasource.

使用jdbctemplate插入数据:

1 public class studentdaoimp implements studentdao { 2 private jdbctemplate jdbctemplate; 3 4 @override 5 public void setjdbctemplate(jdbctemplate jdbctemplate) { 6 this.jdbctemplate = jdbctemplate; 7 } 8 9 public void insert(student student) 10 { 11 string sql = "insert into student (cno,name,score) values(?,?,?)"; 12 //设置传递给通配符的参数 13 object[] params = new object[]{student.getcno(), student.getname(), student.getscore()}; 14 jdbctemplate.update(sql, params); 15 } 16 }

第9~15行为插入一条学生记录的方法,第14行中,jdbctemplate为我们提供了update(string sql,object... args)方法,方便我们进行数据的插入. 

进行测试:

1 public class inserttest { 2 @test 3 public void inserttest() { 4 student student = new student(); 5 student.setcno(1); 6 student.setname("张飞"); 7 student.setscore(50); 8 9 applicationcontext context = new classpathxmlapplicationcontext( 10 "applicationcontext.xml"); 11 studentdaoimp studentdao = (studentdaoimp) context.getbean("studentdao"); 12 studentdao.insert(student); 13 } 14 }

数据库中多了一条记录:

mysql> select * from student;
----- ----- ------ -------
| sno | cno | name | score |
----- ----- ------ -------
| 1 | 1 | 地心 | 50 |
| 2 | 2 | 华雄 | 88 |
| 3 | 1 | 孝慈 | 90 |
| 4 | 3 | 必须 | 42 |
| 5 | 1 | 华雄 | 74 |
| 6 | 2 | 地心 | 75 |
| 7 | 2 | 横切 | 85 |
| 11 | 2 | 横切 | 85 |
| 12 | 2 | 横切 | 85 |
| 14 | 1 | 张飞 | 50 |
----- ----- ------ -------
10 rows in set

批量插入数据:
批量插入数据需要用到org.springframework.jdbc.core.batchpreparedstatementsetter接口。

修改studentdaoimp:

1 public class studentdaoimp implements studentdao { 2 private jdbctemplate jdbctemplate; 3 4 @override 5 public void setjdbctemplate(jdbctemplate jdbctemplate) { 6 this.jdbctemplate = jdbctemplate; 7 } 8 9 public int insert(student student) 10 { 11 string sql = "insert into student (cno,name,score) values(?,?,?)"; 12 //设置传递给通配符的参数 13 object[] params = new object[]{student.getcno(), student.getname(), student.getscore()}; 14 return jdbctemplate.update(sql, params); 15 } 16 17 public int[] batchinsert(final list list) 18 { 19 string sql = "insert into student (cno,name,score) values(?,?,?)"; 20 21 batchpreparedstatementsetter setter = new batchpreparedstatementsetter() { 22 23 @override 24 public void setvalues(preparedstatement ps, int index) throws sqlexception { 25 student student = (student) list.get(index); 26 ps.setint(1, student.getcno()); 27 ps.setstring(2, student.getname()); 28 ps.setdouble(3, student.getscore()); 29 } 30 31 //有多少条记录要处理 32 @override 33 public int getbatchsize() { 34 return list.size(); 35 } 36 }; 37 38 return jdbctemplate.batchupdate(sql, setter); 39 } 40 }

第17~39行为批量插入的方法,batchpreparedstatementsetter接口的两个方法,其中getbatchsize()方法是得到需要插入的记录的个数,setvalues(preparedstatement ps, int index)方法是实际进行插入的方法

进行测试:

1 @test 2 public void batchinsert() { 3 student student1 = null, student2 = null, student3 = null; 4 student1 = new student(); 5 student2 = new student(); 6 student3 = new student(); 7 8 student1.setcno(1); 9 student1.setname("刘备"); 10 student1.setscore(70); 11 12 student2.setcno(2); 13 student2.setname("关羽"); 14 student2.setscore(90); 15 16 student3.setcno(2); 17 student3.setname("张飞"); 18 student3.setscore(40); 19 20 list list = new arraylist(); 21 list.add(student1); 22 list.add(student2); 23 list.add(student3); 24 25 applicationcontext context = new classpathxmlapplicationcontext( 26 "applicationcontext.xml"); 27 studentdaoimp studentdao = (studentdaoimp) context 28 .getbean("studentdao"); 29 studentdao.batchinsert(list); 30 }

插入结果:
mysql> select * from student;

----- ----- ------ -------
| sno | cno | name | score |
----- ----- ------ -------
| 15 | 1 | 刘备 | 70 |
| 16 | 2 | 关羽 | 90 |
| 17 | 2 | 张飞 | 40 |
----- ----- ------ -------
3 rows in set


查询一条记录:
执行一条数据的查询,需要使用org.springframework.jdbc.core.rowcallbackhandler接口的实现。

修改studentdaoimp:

1 /** 2 * 查询一条记录 3 */ 4 public student getstudent(final int id) { 5 // 装载查询结果 6 final student student = new student(); 7 8 string sql = "select s.cno,s.name,s.score from student s where sno = ?"; 9 // 设置查询参数 10 final object[] params = new object[] { new integer(id) }; 11 // 进行查询 12 jdbctemplate.query(sql, params, new rowcallbackhandler() { 13 @override 14 public void processrow(resultset rs) throws sqlexception { 15 student.setcno(rs.getint("cno")); 16 student.setname(rs.getstring("name")); 17 student.setscore(rs.getdouble("score")); 18 } 19 }); 20 21 return student; 22 }

进行测试:

1 @test 2 public void selecttest() { 3 applicationcontext context = new classpathxmlapplicationcontext( 4 "applicationcontext.xml"); 5 studentdaoimp studentdao = (studentdaoimp) context 6 .getbean("studentdao"); 7 8 student student = studentdao.getstudent(15); 9 system.out.println("cno:" student.getcno() " name:" student.getname() " score:" student.getscore()); 10 }

 

查询多条记录:
这里需要用到org.springframework.jdbc.core.rowmapper接口的实现。

修改studentdaoimp:

1 /** 2 * 查询多条记录 3 */ 4 public list getallstudent() { 5 string sql = "select s.cno,s.name,s.score from student s"; 6 7 return jdbctemplate.query(sql, new rowmapper() { 8 @override 9 public student maprow(resultset rs, int index) throws sqlexception { 10 student student = new student(); 11 student.setcno(rs.getint("cno")); 12 student.setname(rs.getstring("name")); 13 student.setscore(rs.getdouble("score")); 14 15 return student; 16 } 17 }); 18 }

rowmapper接口负责把result中的一条记录映射成一个对象。

进行测试:

1 @test 2 public void getallstudent() { 3 applicationcontext context = new classpathxmlapplicationcontext( 4 "applicationcontext.xml"); 5 studentdaoimp studentdao = (studentdaoimp) context 6 .getbean("studentdao"); 7 8 list list = new arraylist(); 9 list = studentdao.getallstudent(); 10 11 for (int i = 0; i < list.size(); i ) { 12 system.out.println("name is:" list.get(i).getname()); 13 } 14 }

也可以使用这种方法查询一条记录,只要附加查询参数即可:

1 /** 2 * 查询一条记录 3 */ 4 public student getstudent(final int id) { 5 // 装载查询结果 6 final student student = new student(); 7 8 string sql = "select s.cno,s.name,s.score from student s where sno = ?"; 9 // 设置查询参数 10 final object[] params = new object[] { new integer(id) }; 11 12 list list = jdbctemplate.query(sql, params, 13 new rowmapper() { 14 @override 15 public student maprow(resultset rs, int index) 16 throws sqlexception { 17 student student = new student(); 18 student.setcno(rs.getint("cno")); 19 student.setname(rs.getstring("name")); 20 student.setscore(rs.getdouble("score")); 21 22 return student; 23 } 24 }); 25 26 return list.get(0); 27 }

 

转载于:https://www.cnblogs.com/hanyuan/archive/2012/10/05/2712132.html

总结

以上是凯发k8官方网为你收集整理的spring中使用jdbc的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图