spring整合jdbc配置文件

 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);
		
	}
	
}









  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值