`
wangyanlong0107
  • 浏览: 480359 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

【转】Spring + JdbcTemplate + JdbcDaoSupport

阅读更多

首先,数据库是这样的,很简单。


当然,要引入spring的包,这里我全部导入了,省事。


applicationContext.xml是这样的:

 

[html] view plaincopy在CODE上查看代码片派生到我的代码片
 
  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"  
  4.     xsi:schemaLocation="  
  5.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  6.   
  7.     <bean id="test" class="jdbc.Test">  
  8.     <property name="dataSource" ref="dataSource"></property>  
  9.     </bean>  
  10.   
  11.     <bean id="dataSource"  
  12.         class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  13.         <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
  14.         <property name="url" value="jdbc:mysql://localhost:3306/testspring" />  
  15.         <property name="username" value="root" />  
  16.         <property name="password" value="" />  
  17.     </bean>  
  18.   
  19. </beans>  

 

User.java是这样的:

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package jdbc;  
  2.   
  3. public class User {  
  4.     private int id;  
  5.     private String name;  
  6.     private String password;  
  7.       
  8.     public int getId() {  
  9.         return id;  
  10.     }  
  11.     public void setId(int id) {  
  12.         this.id = id;  
  13.     }  
  14.     public String getName() {  
  15.         return name;  
  16.     }  
  17.     public void setName(String name) {  
  18.         this.name = name;  
  19.     }  
  20.     public String getPassword() {  
  21.         return password;  
  22.     }  
  23.     public void setPassword(String password) {  
  24.         this.password = password;  
  25.     }  
  26.       
  27. }  

 

下面来对比一下三种写法:

1、spring

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package jdbc;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.SQLException;  
  6.   
  7. import javax.sql.DataSource;  
  8.   
  9. import org.springframework.context.ApplicationContext;  
  10. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  11.   
  12. public class Test {  
  13.     private DataSource dataSource;  
  14.   
  15.     public void setDataSource(DataSource dataSource) {  
  16.         this.dataSource = dataSource;  
  17.     }  
  18.   
  19.     public void insert(User u) {  
  20.         String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句  
  21.         Connection conn = null;  
  22.   
  23.         try {  
  24.             conn = dataSource.getConnection();  
  25.             PreparedStatement ps = conn.prepareStatement(sql);  
  26.             ps.setString(1, u.getName());  
  27.             ps.setString(2, u.getPassword());  
  28.             ps.executeUpdate();  
  29.             ps.close();  
  30.         } catch (SQLException e) {  
  31.             throw new RuntimeException(e);  
  32.         } finally {  
  33.             if (conn != null) {  
  34.                 try {  
  35.                     conn.close();  
  36.                 } catch (SQLException e) {  
  37.                 }  
  38.             }  
  39.         }  
  40.     }  
  41.   
  42.     public static void main(String[] args) {  
  43.         ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  44.                 "applicationContext.xml");  
  45.         Test t = (Test) ctx.getBean("test");  
  46.           
  47.         User u = new User();  
  48.         u.setName("dd");  
  49.         u.setPassword("dd");  
  50.         t.insert(u);  
  51.     }  
  52. }  

运行结果是这样的:

 


2、JdbcTemplate 

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package jdbc;  
  2.   
  3. import java.sql.Connection;  
  4. import java.sql.PreparedStatement;  
  5. import java.sql.SQLException;  
  6.   
  7. import javax.sql.DataSource;  
  8.   
  9. import org.springframework.context.ApplicationContext;  
  10. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  11. import org.springframework.jdbc.core.JdbcTemplate;  
  12.   
  13. public class Test {  
  14.     private DataSource dataSource;  
  15.   
  16.     public void setDataSource(DataSource dataSource) {  
  17.         this.dataSource = dataSource;  
  18.     }  
  19.   
  20.     public void insert(User u) {  
  21.         String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句  
  22.         JdbcTemplate template = new JdbcTemplate(dataSource);  
  23.         template.update(sql, new Object[]{u.getName(), u.getPassword()});  
  24.     }  
  25.   
  26.     public static void main(String[] args) {  
  27.         ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  28.                 "applicationContext.xml");  
  29.         Test t = (Test) ctx.getBean("test");  
  30.           
  31.         User u = new User();  
  32.         u.setName("dd");  
  33.         u.setPassword("dd");  
  34.         t.insert(u);  
  35.     }  
  36. }  

可以看得出简单了很多,不用Connection了。

 

3、JdbcDaoSupport

这个更简单,不用new JdbcTemplate了。

 

[java] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. package jdbc;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import org.springframework.jdbc.core.support.JdbcDaoSupport;  
  6.   
  7. public class Test extends JdbcDaoSupport {  
  8.     //JdbcDaoSupport类已经有了public final void setDataSource(DataSource dataSource)了  
  9.     //不用重写也不能重写  
  10.       
  11.     public void insert(User u) {  
  12.         String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句  
  13.         this.getJdbcTemplate().update(sql, new Object[]{u.getName(), u.getPassword()});  
  14.     }  
  15.   
  16.     public static void main(String[] args) {  
  17.         ApplicationContext ctx = new ClassPathXmlApplicationContext(  
  18.                 "applicationContext.xml");  
  19.         Test t = (Test) ctx.getBean("test");  
  20.           
  21.         User u = new User();  
  22.         u.setName("dd");  
  23.         u.setPassword("dd");  
  24.         t.insert(u);  
  25.     }  
  26. }  

三种方法哪一种更简单一目了然。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics