您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

8、SpringJDBC

bubuko 2022/1/25 19:11:51 java 字数 6211 阅读 776 来源 http://www.bubuko.com/infolist-5-1.html

8.1、Spring在jdbc中的作用 8.2、SpringJDBC实践 1、添加jar包支持类 spring-jdbc-4.3.14.RELEASE.jar、spring-tx-4.3.14.RELEASE.jar package cn.org.kingdom.dao.impl; import o ...

8.1、Spring在jdbc中的作用

mark

8.2、SpringJDBC实践

1、添加jar包支持类

spring-jdbc-4.3.14.RELEASE.jar、spring-tx-4.3.14.RELEASE.jar

package cn.org.kingdom.dao.impl;

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

import cn.org.kingdom.dao.DeptDao;
import cn.org.kingdom.vo.Dept;
@Repository
public class DeptDaoImpl implements DeptDao {
	@Autowired
	private JdbcTemplate jt  ; 
	public JdbcTemplate getJt() {
		return jt;
	}
	public void setJt(JdbcTemplate jt) {
		this.jt = jt;
	}
	@Override
	public int addUser(Dept vo) throws Exception {
		return jt.update("insert into dept(deptno,dname,loc) values(?,?,?)", vo.getDeptno(),vo.getDname(),vo.getLoc());
	}
}

2、xml配置

<?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"
    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
        ">
   <context:property-placeholder location="classpath:db.properties"/>
   <!-- 扫包 -->
   <context:component-scan base-package="cn.org.kingdom"/>
   <bean id="ds" class = "org.apache.commons.dbcp.BasicDataSource">
   		<property name="driverClassName" value="${db.driver}"/>
		<property name="url" value="${db.url}"/>
		<property name="username" value="${db.username}"/>
		<property name="password" value="${db.password}"/>
   </bean>  
   <bean id = "jt" class="org.springframework.jdbc.core.JdbcTemplate">
   		<property name="dataSource" ref="ds"/>
   </bean>
</beans>

8.3、事务处理

8.3.1、spring对事务的支持

Spring对事务有很好的支持.提供了事务管理器. PlatformTransactionManager:平台事务管理器(顶层)

配置事务管理交给Spring来做

<!—①配置事务管理器(AOP:做什么) --> 
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
		p:dataSource-ref="dataSource"/>
	<!-- 2.配置增强(AOP:什么时机:本质是一个环绕增强) --> 
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="trans"/>
		</tx:attributes>	 
	</tx:advice>
	<!-- 3.配置增强的地点(AOP:在哪里增强) --> 
	<aop:config>
		<aop:pointcut expression="execution(* cn.org.kingdom.service.*Service.*(..))" id="txPoint"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
	</aop:config>

8.3.2、spring事务的属性说明

1、name:匹配到的方法模式(支持统配符);

2、read-only:查询的时候,设置为true,其他设置为false (也可以默认不设置)

3、isolation:代表数据库事务隔离级别(默认不用配置此项)DEFAULT:让spring使用数据库默认的事务隔离级别;其他:spring模拟;

4、no-rollback-for: 如果遇到的异常是匹配的异常类型,就不回滚事务;(记录日志等)

5、rollback-for:如果遇到的异常是指定匹配的异常类型,才回滚事务; 注意:Spring中默认只能回滚RuntimeException及其子类异常类型.(Exception.Throwabale就不能回滚) 、要求我们:在Service层中所有的异常,使用RuntimeException类型、若原始异常不是RuntimeException,重新包装成RuntimeException.

6、propagation:事务的传播方式(当一个方法已经在一个开启的事务当中了,应该怎么处理自身的事务)

? ①REQUIRED(默认的传播属性):如果当前方法运行在一个没有事务环境的情况下,则开启一个新的事务,如果当前方法运行在一个已经开启了的事务里面,把自己加入到开启的那个事务中 (一般默认选择此项)

? ②REQUIRES_NEW:不管当前方法是否运行在一个事务空间之内,都要开启自己的事务保存数据建议使用这个

.(了解)

通用的事务管理器

<tx:advice id="crudAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRES_NEW" />
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="del*" />
			<tx:method name="get*" read-only="true"/>
			<tx:method name="list*" read-only="true"/>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="query*" read-only="true"/>
			//此配置必须必须放在最后
           <tx:method name="*"/>
		</tx:attributes>
</tx:advice>

8.3.3、事务的注解配置

@Service("accountService")
@Transactional:该类中所有方法都使用默认的属性增强 
public class AccountServiceImpl implements IAccountService {
	@Autowired
	@Qualifier("accountDAO")
	private IAccountDAO dao;
	@Transactional(propagation=Propagation.REQUIRES_NEW) 
	public void save(){}
	@Transactional(readOnly=true) 
	public void get(){}
	public void trans(Long outId, Long inId, Integer money) {
		System.out.println("11");
		dao.transIn(inId, money);
		System.out.println(1/0);//模拟停电
		dao.transOut(outId, money);
	}
}
@Repository("accountDAO")
public class AccountDAOImpl implements IAccountDAO {
	private JdbcTemplate jdbcTemplate;
	@Resource(name="dataSource")
	public void setDataSource(DataSource ds){
		jdbcTemplate = new JdbcTemplate(ds);
	}
}

8、SpringJDBC

原文:https://www.cnblogs.com/prefordan/p/14637063.html


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶