Spring + JdbcTemplate + JdbcDaoSupport

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


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


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);
	}
}
可以看得出简单了很多,不用Connection了。

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);
	}
}
三种方法哪一种更简单一目了然。

我参考的文章:Spring + JdbcTemplate + JdbcDaoSupport Examples

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值