现在的位置: 首页 > 综合 > 正文

spring整合jdbc配置文件

2018年01月26日 ⁄ 综合 ⁄ 共 2924字 ⁄ 字号 评论关闭

 spring通过jdbcTemplate整合jdbc,在spring框架中管理数据源配置,并进行事务管理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:context="http://www.springframework.org/schema/context"
		    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


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-2.5.xsd

			    ">
		
	<!-- 配置数据源 -->
	<bean 	class="org.apache.commons.dbcp.BasicDataSource" 
		destroy-method="close"
		name="MydataSource">
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
			<value>jdbc:oracle:thin:@localhost:1521:xe</value>
		</property>
		<property name="username">
			<value>abcuser</value>
		</property>
		<property name="password">
			<value>123456</value>
		</property>
	</bean>
		
		
	<!-- 在jdbcTemplate模板中注入数据源 -->	
	<bean name="jdbcTemelate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="MydataSource"></property>	 
	</bean>	
	

	
	<!-- 配置事务管理器 -->
	<bean name="tranManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	   <property name="dataSource" ref="MydataSource"></property>
	</bean>
	
	<!-- 配置事务拦截器 -->
	<bean name="tranInterceptor" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
	    <property name="transactionManager" ref="tranManager"></property>
	    <property name="transactionAttributes">
	        <props>
	        <!-- 事务传播属性,事务隔离级别, 方法属性值,控制提交回滚操作 (+Exception强制提交,-Exception回滚)-->
	        <!-- <prop key="*">PROPAGATION_REQUIRED,,,</prop> -->
	           <prop key="*">PROPAGATION_REQUIRED</prop>
	        </props>
	    </property>
	    <property name="target" ref="service"></property>
	</bean>
	

         <!-- 配置dao层 -->
	<bean name="jdbcDao" class="com.sjdbc.dao.impl.JdbcTemplateDaoImpl">
	     <property name="jdbcTemplate" ref="jdbcTemelate"></property>
	</bean>
	
	
	
        <!-- 配置service层 -->
          <bean name="service" class="com.sjdbc.service.impl.UserServiceImpl">
               <property name="dao" ref="jdbcDao"></property>
                   </bean>
</beans>

最终在dao层进行数据库交互时,使用jdbcTemplate

package com.sjdbc.dao.impl;

import org.springframework.jdbc.core.JdbcTemplate;

import com.sjdbc.bean.User;
import com.sjdbc.dao.UserDao;

public class JdbcTemplateDaoImpl  implements UserDao{
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

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

	public void saveOrUpdate(User user) throws Exception {
		// TODO Auto-generated method stub
//		String sql="insert into user values("+user.getId()+",'"+user.getName()+"')";
//		jdbcTemplate.execute(sql);
		
		String sql="insert into users values(?,?)";
		jdbcTemplate.update(sql,new Object[]{user.getId(),user.getName()});
	}

	public User findByName(String name) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public void deleteUser(User user) throws Exception {
		// TODO Auto-generated method stub
		String sql="delete from users where id="+user.getId();
		throw new RuntimeException("删除失败");
//		jdbcTemplate.execute(sql);
		
	}
	
}

抱歉!评论已关闭.