十九、Spring之JdbcDaoSupport的使用

一、环境搭建

导入相关jar包,这些jar包前面都有用到过,这里就不一一介绍了。
在这里插入图片描述
引入相关配置文件
主要是log4j.properties的配置文件

### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=c\:mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.rootLogger=info, stdout

和Spring的配置文件,这里我将所有schema都配置上了,有些是用不到的,顺便将DataSource也配置好

<?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">
	
	 
	 <!-- 注册c3p0连接池  -->
	 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	 	<!-- c3p0的连接属性的名称和默认连接池的有所不同,这里需要注意下 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql:///spring_jdbc" />
		<property name="user" value="root" />
		<property name="password" value="1234" />

	</bean>
	
</beans>

JdbcDaoSupport类

JdbcDaoSupport是Spring内置的一个Dao层的基类,来自spring-jdbc-4.2.4.RELEASE.jar这个包,其内部定义了JdbcTemplate的set方法,这样我们自己的dao类只需要继承JdbcDaoSupport类,就可以省略JdbcTemplate的set方法书写了,通过查看源码你会发现,该方法是final修饰的

/**
	 * Set the JdbcTemplate for this DAO explicitly,
	 * as an alternative to specifying a DataSource.
	 */
	public final void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
		initTemplateConfig();
	}

也就是说,子类是无法覆盖的。

另外,它还提供了注入数据库连接池的set方法。

/**
	 * Set the JDBC DataSource to be used by this DAO.
	 */
	public final void setDataSource(DataSource dataSource) {
		if (this.jdbcTemplate == null || dataSource != this.jdbcTemplate.getDataSource()) {
			this.jdbcTemplate = createJdbcTemplate(dataSource);
			initTemplateConfig();
		}
	}

从该方法可以发现,如果我们在Spring配置文件中没有注入jdbcTemplate ,那么可以通过注入DataSource 的时候自动创建jdbcTemplate,这样一来又可以省略JdbcTemplate类的注册工作了。

说白了,JdbcDaoSupport就是为了简化我们dao类有关JdbcTemplate的注入的相关工作量。

如果需要用到JdbcTemplate,只需要调用JdbcDaoSupport的getJdbcTemplate方法即可获取。

测试

javabean

package blog.csdn.net.mchenys.domain;

public class Account {
	private Integer id;
	private String name;
	private Double money;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getMoney() {
		return money;
	}
	public void setMoney(Double money) {
		this.money = money;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", money=" + money + "]";
	}
	
	
}

dao层

package blog.csdn.net.mchenys.dao;

import blog.csdn.net.mchenys.domain.Account;

public interface AccountDao {

	void save(Account account);
}

package blog.csdn.net.mchenys.dao;

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

import blog.csdn.net.mchenys.domain.Account;

//dao层,继承JdbcDaoSupport 
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

	@Override
	public void save(Account account) {
		// 从父类获取jdbcTemplate
		getJdbcTemplate().update("insert into t_account values(null,?,?)", 
				account.getName(), account.getMoney());
	}

}

Spring配置文件

<?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">
	
	 
	 <!-- 注册c3p0连接池  -->
	 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	 	<!-- c3p0的连接属性的名称有所不同,这里需要注意下 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql:///spring_jdbc" />
		<property name="user" value="root" />
		<property name="password" value="1234" />

	</bean>
	
	<!-- 注册AccountDaoImpl -->
	<bean id="accountDao" class="blog.csdn.net.mchenys.dao.AccountDaoImpl">
		
		<!-- 注入属性 :dataSource属性来自JdbcDaoSupport类,只要继承就可以拥有-->
		<property name="dataSource" ref="dataSource"/>
		
		<!--dataSource属性对应的setDataSource方法内部还会自动创建jdbcTemplate
			因此,这里无需再注入 jdbcTemplate属性了  
		<property name="jdbcTemplate" ref="jdbcTemplate"/>
		-->
	</bean>
	
	<!-- 有了JdbcDaoSupport,就不需要再注册JdbcTemplate类了
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	 -->
</beans>

test类

package blog.csdn.net.mchenys.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import blog.csdn.net.mchenys.dao.AccountDao;
import blog.csdn.net.mchenys.domain.Account;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo1 {

	@Resource(name = "accountDao")
	private AccountDao dao; // 通过注解注入配置文件中注册的AccountDaoImpl

	@Test
	public void test1() {
		Account account = new Account();
		account.setName("hello");
		account.setMoney(1000d);
		dao.save(account);
	}
}

运行后,查看数据库多了这条记录则测试通过
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值