Spring-JDB和声明式事务

56 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

Spring for JDBC

注意:

  • 只需要将接口实现类使用注释将其添加到Ioc中(命名接口就行)

  • 调用的时候只需要调用接口,不用调用接口实现类,(但实际上还时调用的接口实现类)

1,添加依赖

  1. spring-webmvc就可以导入其他的
  2. spring-jdbc(spring提供的jdbc)
  3. druid包(导入数据源的依赖)
  4. mysql驱动
  5. junit测试
  6. lombok插件(项目的支持)和导包(程序的解析)

2,创建基本项目工程,数据库表,实体类,各层接口,实现类

注意,在service层调用dao层接口时,使用依赖注入,不用new

@Autowired

StudentDao studentDao;

再用studentDao进行操作

3,创建bean.xml文件

修改xml头部信息

  • 包扫描(头部context)
  • 指定要扫面的包(<context:component-scan base-package="org.lanqiao"/>

4,在dao层使用Spring自带的JdbcTemplate类

  • 使用之前需要将这个类添加到IoC中 <bean id="" class=""/>

  • 在dao层自动注入JdbcTemplate类的对象 @Autowired

  • 使用JdbcTemplate自带的方法进行sql语句的编写

5,导入数据源的依赖 druid包

  • 将druid中的DruidDataSource类添加到IoC中

  • 给这个类的属性进行赋值

    • druid.driverClassName .....
  • 给JdbcTemplate类添加属性datasource数据源,并添加引用到前面定义的数据源

6,测试

AOP的应用-声明式事务

事务的处理一定是在service层(事务主要时用在DML上面 增删改)

AOP思想处理事务

1,Spring事务的五个维度

1,事务传播机制

2,事务隔离级别

3,事务只读模式

4,事务超时

5,事务回滚

2,在Spring beans.xml配置文件设置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns="http://www.springframework.org/schema/beans"
       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-4.3.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 ">

    <!--扫描包-->
    <context:component-scan base-package="org.lanqiao"/>

    <!--配置阿里druid数据源,需要导入druid的jar包-->
	<bean id="DruidDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
		destroy-method="close">
		<!-- 基本属性 url、user、password -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
		<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
		<property name="username" value="root" />
		<property name="password" value="123" />
		<!-- 配置初始化大小、最小、最大 -->
		<property name="initialSize" value="1" />
		<property name="minIdle" value="1" />
		<property name="maxActive" value="20" />
		<!-- 配置获取连接等待超时的时间 -->
		<property name="maxWait" value="60000" />
		<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
		<property name="timeBetweenEvictionRunsMillis" value="60000" />
		<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="300000" />
		<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
		<property name="poolPreparedStatements" value="true" />
		<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
		<!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
		<property name="filters" value="stat" />
	</bean>
	<!-- 添加spring事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="DruidDataSource"/>
	</bean>
	<!-- 定义spring aop通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED"/>
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="update*" propagation="REQUIRED"/>
			<tx:method name="modify*" propagation="REQUIRED"/>
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="find*" read-only="true"/>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="select*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	<!-- 定义切入点,引用通知 -->
	<aop:config>
		<aop:pointcut expression="execution(* org.lanqiao.service.*.*(..))" id="mypointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>
	</aop:config>     

</beans>

3,使用注解来添加事务

  1. 在xml中添加注解声明器,并引用事务管理器
  2. 在相应的方法上面添加@Transactional注解,表明这个方法添加了事务