spring JDBC模板的使用

目录

说明

使用spring JDBC模板来操作数据库,有两种方式。

方式一:使用IoC注入的方式

配置文件

代码片.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 引入资源文件 jdbc.properties -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>jdbc/jdbc.properties</value>
		</property>
	</bean>
	<!-- 配置连接池 spring自带的类 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	
	<!-- spring jdbc模板 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- DAO,注入jdbc模板 -->
	<bean id="accountDAO" class="jdbc.AccountDAO1_JDBC">
		<property name="jdbcTemplate" ref="jdbcTemplate" />
	</bean>
	
</beans>

说明:
将配置文件内声明的JDBC模板bean注入到DAO类中

操作类AccountDAO2_JDBC.java

代码片.

package jdbc;
import org.springframework.jdbc.core.JdbcTemplate;

//DAO层
public class AccountDAO1_JDBC {

	JdbcTemplate jdbcTemplate;

	//IoC注入JdbcTemplate对象
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void inMoney(String name, double money) {
		String sql = "update account set money = money + ? where name= ?";
		jdbcTemplate.update(sql, money, name);
		System.out.println("给" + name + "转入" + money + "元钱");
	}

	public void outMoney(String name, double money) {	
		String sql = "update account set money = money - ? where name= ?";
		jdbcTemplate.update(sql, money, name);
		System.out.println("从" + name + "转出" + money + "元钱");
	}

}

说明:
在类中IoC注入JdbcTemplate对象

方式二:继承JdbcDaoSupport类的方式

配置文件

代码片.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.xsd">


	<!-- 引入资源文件 jdbc.properties -->
	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<value>jdbc/jdbc.properties</value>
		</property>
	</bean>
	<!-- 配置连接池 spring自带的类 -->
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<!-- DAO,继承模板类 -->
	<bean id="accountDAO" class="jdbc.AccountDAO2_JDBC">
		<property name="dataSource" ref="dataSource" />
	</bean>

	
</beans>



说明:
将配置文件内声明的JDBC模板bean注入到DAO类中

操作类AccountDAO2_JDBC.java

代码片.

package jdbc;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

//DAO层
public class AccountDAO2_JDBC extends JdbcDaoSupport {

	JdbcTemplate jdbcTemplate;

	public AccountDAO1_JDBC() {
		// 使用JdbcDaoSupport模板类来获得jdbcTemplate对象
		jdbcTemplate = this.getJdbcTemplate();
	}

	public void inMoney(String name, double money) {
		String sql = "update account set money = money + ? where name= ?";
		jdbcTemplate.update(sql, money, name);
		System.out.println("给" + name + "转入" + money + "元钱");
	}

	public void outMoney(String name, double money) {
		String sql = "update account set money = money - ? where name= ?";
		jdbcTemplate.update(sql, money, name);
		System.out.println("从" + name + "转出" + money + "元钱");
	}
}


说明:
继承JdbcDaoSupport ,
在类中使用JdbcDaoSupport模板类来获得jdbcTemplate对象

JDBC模板的使用—增删改查

更新–增删改

代码片.

	// 更新--更、删、改
	{
		String sql = "update ccount set money = money + ? where name= ?";
		double money = 0;
		String name = null;
		jdbcTemplate.update(sql, money, name);
	}
 
	// 批处理更新
	{
		String sql = null;
		jdbcTemplate.batchUpdate(sql1, sql2, sql3);
	}

查找

代码片.

	// 查询--API配合使用,返回一个实体类
	{
		User user = jdbcTemplate.queryForObject("select * from User where id=?",
				new BeanPropertyRowMapper<User>(User.class), 3);
	}

	// 查询--查询一个值
	{
		String Name = jdbcTemplate.queryForObject("select name from User where id=?", String.class, 3);
	}

	// 查询--查询一条记录
	{
		// key是属性名,value是记录值
		Map<String, Object> map = jdbcTemplate.queryForMap("select * from User where id=?", 3);
	}

	// 查询--查询多条记录
	{
		// list的每一个entry是一个记录,entry的key是属性名、value是记录值
		List<Map<String, Object>> maps = jdbcTemplate.queryForList("select * from User");
	}

	// 查询--查询多条记录使用RowMapper映射为实体类(可以取表中的一部分属性值)
	{
		List<User> query = jdbcTemplate.query("select id, name, pass from User where id between ? and ?",
				new RowMapper<User>() {
					@Override
					public User mapRow(ResultSet resultSet, int i) throws SQLException {
						int id = resultSet.getInt("id");
						String name = resultSet.getString("name");
						String pass = resultSet.getString("pass");
						User user = new User(id, name, pass);
						return user;
					}
				}, 10, 20);
	}

	// 查询--查询多条记录使用RowMapper映射为实体类(实体类属性和表的属性必须一对一同名)
	{
		List<User> query = jdbcTemplate.query("select * from User where id in(?,?,?)",
				new BeanPropertyRowMapper<User>(User.class), 3, 4, 5);
	}

总结

	1. 使用JDBC模板来操作数据库可以大大简化传统的JDBC使用
**引用包是最大的困难(一步一步来)**
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值