gradle配置
dependencies {
compile(project(":spring-beans"))
compile(project(":spring-core"))
compile(project(":spring-context"))
compile(project(":spring-webmvc"))
compile(project(":spring-jdbc"))
compile(project(":spring-orm"))
compile(project(":spring-tx"))
compile(project(":spring-web"))
compile(project(":spring-context-indexer"))
compile(project(":spring-context-support"))
compile(project(":spring-expression"))
compile(project(":spring-instrument"))
compile(project(":spring-jcl"))
compile(project(":spring-jms"))
compile(project(":spring-messaging"))
compile(project(":spring-oxm"))
compile(project(":spring-test"))
compile(project(":spring-webflux"))
compile(project(":spring-websocket"))
compile(project(":spring-aspects"))
compile("org.aspectj:aspectjweaver:1.9.6")
compile(project(":spring-aop"))
// -----------导入mysql驱动包,数据库连接池-------------
compile 'mysql:mysql-connector-java:5.1.47'
compile 'com.alibaba:druid:1.1.14'
testCompile group: 'junit', name: 'junit', version: '4.12'
}
刷新
编写类
这里使用的注解,如果使用xml注入可以选择set或构造都可以
@Service
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
public void get(int id){
String sql = "select c_phoneno from t_user where n_id=?";
String phoneno = jdbcTemplate.queryForObject(sql, String.class, id);
System.out.println("获取用户。。。。" + phoneno);
}
public void add(int i){
System.out.println("增加用户。。。。" + i);
}
@Transactional
public void update(int id){
String sql = "update t_user set c_phoneno='15966666666' where n_id=?";
jdbcTemplate.update(sql,id);
// int i = 1/0;
System.out.println("更新用户手机号。。。。");
}
public void delete(int i){
System.out.println("删除书籍。。。。");
}
}
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:tx="http://www.springframework.org/schema/tx"
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
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
">
<context:component-scan base-package="com.msgqu.debug.tx"/>
<context:property-placeholder location="application.properties"/>
<!-- 配置bean -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driverName}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--事务的配置-->
<!--声明一个事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<aop:config>
<aop:pointcut id="txPointcut" expression="execution(* com.msgqu.debug.tx.*.*(..))"/>
<!--事务建议-->
<aop:advisor advice-ref="myAdvice" pointcut-ref="txPointcut"></aop:advisor>
</aop:config>
<tx:advice id="myAdvice" transaction-manager="transactionManager">
<!--配置事务的属性-->
<tx:attributes>
<!--配置在哪些方法上添加事务-->
<!-- <tx:method name="*" propagation="REQUIRED" read-only="true" isolation="DEFAULT"/>-->
<tx:method name="add*" propagation="REQUIRED"></tx:method>
<tx:method name="update*" propagation="REQUIRED"></tx:method>
<tx:method name="delete*" propagation="REQUIRED"></tx:method>
</tx:attributes>
</tx:advice>
</beans>
验证
public class TxTest {
public static void main(String[] args) {
MyClassPathXmlApplicationContext ac = new MyClassPathXmlApplicationContext("tx.xml");
UserService userService = ac.getBean(UserService.class);
userService.get(1);
userService.update(1);
ac.close();
}
}
事务的环境配置到这里就完成了~~~