当前位置:首页 > 开发 > 系统架构 > 架构 > 正文

Spring之在开发中使用SpringJDBC

发表于: 2015-07-13   作者:ihuning   来源:转载   浏览:
摘要:   在实际开发中使用SpringJDBC有两种方式:   1. 在Dao中添加属性JdbcTemplate并用Spring注入;     JdbcTemplate类被设计成为线程安全的,所以可以在IOC 容器中声明它的单个实例,并将这个实例注入到所有的 DAO 实例中。JdbcTemplate也利用了Java 1.5 的特定(自动装箱,泛型,可变长度

 

在实际开发中使用SpringJDBC有两种方式

 

1. 在Dao中添加属性JdbcTemplate并用Spring注入;

    JdbcTemplate类被设计成为线程安全的,所以可以在IOC 容器中声明它的单个实例,并将这个实例注入到所有的 DAO 实例中。JdbcTemplate也利用了Java 1.5 的特定(自动装箱,泛型,可变长度等)来简化开发。

2. 使Dao继承JdbcDaoSupport;

    Spring JDBC 框架还提供了一个 JdbcDaoSupport 类来简化 DAO 实现,该类声明了 jdbcTemplate 属性,它可以从 IOC 容器中注入,或者自动从数据源中创建。

 

Dao设计示例

 

1. 添加jdbcTemplate属性,用注解配置

 

1.1 添加Dao

 

package xyz.huning.dao.impl.jdbctemplate.annotation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import xyz.huning.dao.impl.Employee;
import xyz.huning.dao.impl.IEmployeeDao;

@Repository
public class EmployeeDao implements IEmployeeDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Employee getEmployeeById(int id) {
		String sql = "select id,name,level from t_employee where id = ?";
		RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<Employee>(Employee.class);
		return jdbcTemplate.queryForObject(sql, rowMapper, id);
	}

}

  

 

1.2 添加配置

 

<?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: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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	
	<context:component-scan base-package="xyz.huning.dao.impl.jdbctemplate.annotation"></context:component-scan>
	
	<!-- 导入资源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置 C3P0 数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>

		<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
		<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
		<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
		<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
		<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"></property>
	</bean>
	
	<!--配置Spring的JdbcTemplate-->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
</beans>

  

 

1.3 添加测试类 

 

package xyz.huning.dao.impl.jdbctemplate.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import xyz.huning.dao.impl.Employee;

public class Main {

	public static void main(String[] args) {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("jdbctemplate-annotation.xml");

		EmployeeDao employeeDao = ctx.getBean("employeeDao", EmployeeDao.class);
		Employee employee = employeeDao.getEmployeeById(1);
		System.out.println(employee);
		
		((ClassPathXmlApplicationContext)ctx).close();
	}
}

 

 2. 添加jdbcTemplate属性,用XML配置

 

2.1 添加Dao

 

package xyz.huning.dao.impl.jdbctemplate.xml;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import xyz.huning.dao.impl.Employee;
import xyz.huning.dao.impl.IEmployeeDao;

public class EmployeeDao implements IEmployeeDao{

	private JdbcTemplate jdbcTemplate;
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public Employee getEmployeeById(int id) {
		String sql = "select id,name,level from t_employee where id = ?";
		RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<Employee>(Employee.class);
		return jdbcTemplate.queryForObject(sql, rowMapper, id);
	}

}

  

 

2.2 添加配置

 

<?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: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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<!-- 导入资源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置 C3P0 数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>

		<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
		<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
		<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
		<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
		<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"></property>
	</bean>
	
	<!--配置Spring的JdbcTemplate-->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
    <bean id="employeeDao" class="xyz.huning.dao.impl.jdbctemplate.xml.EmployeeDao">
    	<property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
	
</beans>

  

 

2.3 添加测试类 

 

package xyz.huning.dao.impl.jdbctemplate.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import xyz.huning.dao.impl.Employee;

public class Main {

	public static void main(String[] args) {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("jdbctemplate-xml.xml");

		EmployeeDao employeeDao = ctx.getBean("employeeDao", EmployeeDao.class);
		Employee employee = employeeDao.getEmployeeById(1);
		System.out.println(employee);
		
		((ClassPathXmlApplicationContext)ctx).close();
	}
}

 

 

3. 继承JdbcDaoSupport ,用注解配置

 

    备注: 可以选择向Dao中注入JdbcTemplate或者向Dao中注入dataSource

 

3.1 添加Dao

 

package xyz.huning.dao.impl.jdbcdaosuppport.annotation;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import xyz.huning.dao.impl.Employee;
import xyz.huning.dao.impl.IEmployeeDao;

@Repository
public class EmployeeDao extends JdbcDaoSupport implements IEmployeeDao{
	
	@Autowired
	public void injectDataSource(DataSource dataSource)
	{
		setDataSource(dataSource);
	}
	
//	@Autowired
//	public void injectJdbcTemplate(JdbcTemplate jdbcTemplate)
//	{
//		setJdbcTemplate(jdbcTemplate);
//	}
	
	@Override
	public Employee getEmployeeById(int id) {
		String sql = "select id,name,level from t_employee where id = ?";
		RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<Employee>(Employee.class);
		return getJdbcTemplate().queryForObject(sql, rowMapper, id);
	}

}

 

3.2 添加配置

 

<?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: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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	
	<context:component-scan base-package="xyz.huning.dao.impl.jdbcdaosuppport.annotation"></context:component-scan>
	
	<!-- 导入资源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置 C3P0 数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>

		<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
		<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
		<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
		<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
		<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"></property>
	</bean>
	
	<!--配置Spring的JdbcTemplate-->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
</beans>

 

3.3 添加测试类

 

package xyz.huning.dao.impl.jdbcdaosuppport.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import xyz.huning.dao.impl.Employee;

public class Main {

	public static void main(String[] args) {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("jdbcdaosuppport-annotation.xml");

		EmployeeDao employeeDao = ctx.getBean("employeeDao", EmployeeDao.class);
		Employee employee = employeeDao.getEmployeeById(1);
		System.out.println(employee);
		
		((ClassPathXmlApplicationContext)ctx).close();
	}
}

 

 

4. 继承JdbcDaoSupport ,用XML配置

 

    备注: 可以选择向Dao中注入JdbcTemplate或者向Dao中注入dataSource

 

 

4.1 添加Dao

 

package xyz.huning.dao.impl.jdbcdaosupport.xml;

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

import xyz.huning.dao.impl.Employee;
import xyz.huning.dao.impl.IEmployeeDao;

public class EmployeeDao extends JdbcDaoSupport implements IEmployeeDao{
	
	@Override
	public Employee getEmployeeById(int id) {
		String sql = "select id,name,level from t_employee where id = ?";
		RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<Employee>(Employee.class);
		return getJdbcTemplate().queryForObject(sql, rowMapper, id);
	}

}

 

 

4.2 添加配置

 

<?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: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/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	
	<!-- 导入资源文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 配置 C3P0 数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>

		<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
		<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
		<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
		<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
		<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"></property>
	</bean>
	
	<!--向Dao中注入JdbcTemplate-->
	<!--
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<bean id="employeeDao" class="xyz.huning.dao.impl.jdbcdaosupport.xml.EmployeeDao">
		<property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    -->
    
    <!--向Dao中注入dataSource-->
    <bean id="employeeDao" class="xyz.huning.dao.impl.jdbcdaosupport.xml.EmployeeDao">
		<property name="dataSource" ref="dataSource"></property>
    </bean>
	
</beans>

 

4.3 添加测试类

 

package xyz.huning.dao.impl.jdbcdaosupport.xml;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import xyz.huning.dao.impl.Employee;

public class Main {

	public static void main(String[] args) {
		
		ApplicationContext ctx = new ClassPathXmlApplicationContext("jdbcdaosuppport-xml.xml");

		EmployeeDao employeeDao = ctx.getBean("employeeDao", EmployeeDao.class);
		Employee employee = employeeDao.getEmployeeById(1);
		System.out.println(employee);
		
		((ClassPathXmlApplicationContext)ctx).close();
	}
}

 

 

Spring之在开发中使用SpringJDBC

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
在经典的 JDBC 用法中,SQL 参数是用占位符 ? 表示,并且受到位置的限制。定位参数的问题在于, 一
在Web应用中如何使用Spring框架? 在回答这个问题之前,让我们先回顾一下在非Web中使用Spring框架的
Web Service,即“Web 服务”,简写为 WS。那么服务有服务需求方和提供服务方,顾名思义,提供服务
在这个案例中为了更加简单明了的让读者看懂,没有做分层: 这个是目录结构:下面结构中bean这个没用到,
大家好,我是秦元培,欢迎大家关注我的博客,我的博客地址是blog.csdn.net/qinyuanpei。 今天我们来
众说周知,在iOS系统提供的字体是有限的,我们可以利用 UIFont类取出查看iOS系统支持的所有字体类型
<jQuery EasyUI最新试用版免费下载> 如果默认排序行为不能满足您的需求,您可以自定义数据网
<jQuery EasyUI最新试用版免费下载> 本教程为您演示如何放置一个复选框到数据网格中。通过复
转自: 转载自雨松MOMO程序研究院本文链接地址:IOS研究院之在IOS5中使用NavigationBar导航栏(八)
关于如何移植SDL2.0到安卓上面来参考我的上一篇文章:[原]零基础学习SDL开发之移植SDL2.0到Android
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号