Spring 和JdbcTemplate 和 JdbcDaoSupport方式对比

数据库省略。

jar包引入:

spring的包

spring-jdbc-3.2.0.RELEASE.jar


applicationContext.xml配置:

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="  
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  
    <bean id="test" class="jdbc.Test">  
    <property name="dataSource" ref="dataSource"></property>  
    </bean>  
  
    <bean id="dataSource"  
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />  
        <property name="url" value="jdbc:mysql://localhost:3306/testspring" />  
        <property name="username" value="root" />  
        <property name="password" value="" />  
    </bean>  
  
</beans>  
User.java:

package jdbc;  
  
public class User {  
    private int id;  
    private String name;  
    private String password;  
      
    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public String getPassword() {  
        return password;  
    }  
    public void setPassword(String password) {  
        this.password = password;  
    }  
      
}  


下面来对比一下三种写法:(下面是重点)

1、spring

package jdbc;  
  
import java.sql.Connection;  
import java.sql.PreparedStatement;  
import java.sql.SQLException;  
  
import javax.sql.DataSource;  
  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
  
public class Test {  
    private DataSource dataSource;  
  
    public void setDataSource(DataSource dataSource) {  
        this.dataSource = dataSource;  
    }  
  
    public void insert(User u) {  
        String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句  
        Connection conn = null;  
  
        try {  
            conn = dataSource.getConnection();  
            PreparedStatement ps = conn.prepareStatement(sql);  
            ps.setString(1, u.getName());  
            ps.setString(2, u.getPassword());  
            ps.executeUpdate();  
            ps.close();  
        } catch (SQLException e) {  
            throw new RuntimeException(e);  
        } finally {  
            if (conn != null) {  
                try {  
                    conn.close();  
                } catch (SQLException e) {  
                }  
            }  
        }  
    }  
  
    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
                "applicationContext.xml");  
        Test t = (Test) ctx.getBean("test");  
          
        User u = new User();  
        u.setName("dd");  
        u.setPassword("dd");  
        t.insert(u);  
    }  
}  

2、JdbcTemplate

package jdbc;  
  
import java.sql.Connection;  
import java.sql.PreparedStatement;  
import java.sql.SQLException;  
  
import javax.sql.DataSource;  
  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
import org.springframework.jdbc.core.JdbcTemplate;  
  
public class Test {  
    private DataSource dataSource;  
  
    public void setDataSource(DataSource dataSource) {  
        this.dataSource = dataSource;  
    }  
  
    public void insert(User u) {  
        String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句  
        JdbcTemplate template = new JdbcTemplate(dataSource);  
        template.update(sql, new Object[]{u.getName(), u.getPassword()});  
    }  
  
    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
                "applicationContext.xml");  
        Test t = (Test) ctx.getBean("test");  
          
        User u = new User();  
        u.setName("dd");  
        u.setPassword("dd");  
        t.insert(u);  
    }  
}  

3、JdbcDaoSupport

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

package jdbc;  
  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;  
import org.springframework.jdbc.core.support.JdbcDaoSupport;  
  
public class Test extends JdbcDaoSupport {  
    //JdbcDaoSupport类已经有了public final void setDataSource(DataSource dataSource)了  
    //不用重写也不能重写  
      
    public void insert(User u) {  
        String sql = "insert into _user " + "values(null, ?, ?)";//普通的sql语句  
        this.getJdbcTemplate().update(sql, new Object[]{u.getName(), u.getPassword()});  
    }  
  
    public static void main(String[] args) {  
        ApplicationContext ctx = new ClassPathXmlApplicationContext(  
                "applicationContext.xml");  
        Test t = (Test) ctx.getBean("test");  
          
        User u = new User();  
        u.setName("dd");  
        u.setPassword("dd");  
        t.insert(u);  
    }  
}  

结论:三种方法哪一种更简单一目了然。第三种
本文转自http://blog.csdn.net/qiantujava/article/details/17244529


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值