目录
配置环境
1.配置pom.xml,主要导入Spring框架
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1_3</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
</dependency>
<dependency>
<groupId>commons-dbutils</groupId>
<artifactId>commons-dbutils</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
2.配置Spring的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: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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="accountDao" class="com.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountService" class="com.service.impl.AccountServiceImpl">
<property name="dao" ref="accountDao"/>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!--连接数据库的必备信息-->
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/travel?useSSL=false"></property>
<property name="username" value="root"></property>
<property name="password" value="root."></property>
</bean>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--配个事务的属性
isolation:用于指定事务的隔离级别。默认位是DEFAULT, 表示使用数据库的默认附离级别。
propagation:用于指定小务的传播行为。默认值是REQUIRED.表示定会有小务,增删改的选择。查询方法可以选揮SUPPORTS。
read-only:用于指定事务是否只读。只有查询方法才能设为true。默认值是false,表示读写。
timeout:用于指定小海的超时时间,默认值是-1.表示永不超时。如果指定了故值,以秒为单位。
rollback-for:用于指定一个异帘,当产生该异常时,事务回浓,产生其他异帘时,事势不回滚。没有默认值。表示任何异常都回滚。
no-rollback-for:用于指定一个异常,当产生该异常时,非务不回滚,产生其他异常时小务回滚。没有默认值。表示任何异常都回滚。
-->
<tx:attributes>
<!--name 表示当遇到这个名字时候来执行事务-->
<tx:method name="transfer" propagation="REQUIRED" read-only="false" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<!--设置切入点表达式,管理路径-->
<aop:pointcut id="pt1" expression="execution(* com.service.impl.*.*(..))"/>
<!--切入点表达式和事务通知建立联系-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
</aop:config>
</beans>
代码实现
3.我的test项目结构图
3.1 持久层实现类继承了JdbcDaoSupport,这个类对JdbcTemplate进行了封装
package com.dao.impl;
import com.dao.IAccountDao;
import com.domain.Account;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import java.util.List;
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao {
public List<Account> findAllAccount() {
List<Account> query=null;
try {
query = getJdbcTemplate().query("select * from account ",
new BeanPropertyRowMapper<Account>(Account.class));
} catch (Exception e) {
e.printStackTrace();
}
return query;
}
public Account findById(Integer id) {
Account query =null;
try {
query = getJdbcTemplate().queryForObject("select * from account where id = ? ", new BeanPropertyRowMapper<Account>(Account.class), id);
} catch (Exception e) {
e.printStackTrace();
}
return query;
}
public void saveAccount(Account account) {
getJdbcTemplate().update("insert into account (name,money) values(?,?) ", account.getName(),
account.getMoney());
}
public void updateAccount(Account account) {
try {
getJdbcTemplate().update("update account set money=?,name=? where id =? ", account.getMoney(), account.getName(), account.getId());
} catch (Exception e) {
e.printStackTrace();
}
}
public void delAccount(Integer id) {
getJdbcTemplate().update("delete from account where id =? ",id);
}
public Account findByName(String name) {
Account query = null;
try {
query = getJdbcTemplate().queryForObject("select * from account where name =?",
new BeanPropertyRowMapper<Account>(Account.class), name);
} catch (Exception e) {
e.printStackTrace();
}
return query;
}
}
因为Spring 对其JdbcTemplate封装的原因只需注入DataSource即可自动注入JdbcTemplate。
3.2 service实现类
package com.service.impl;
import com.dao.IAccountDao;
import com.domain.Account;
import com.service.IAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
public class AccountServiceImpl implements IAccountService {
private IAccountDao dao;
public void setDao(IAccountDao dao) {
this.dao = dao;
}
public List<Account> findAllAccount() {
return dao.findAllAccount();
}
public Account findById(Integer id) {
return dao.findById(id);
}
public void saveAccount(Account account) {
dao.saveAccount(account);
}
public void updateAccount(Account account) {
dao.updateAccount(account);
}
public void delAccount(Integer id) {
dao.delAccount(id);
}
public void transfer(String Name, String ToName, Integer money) {
Account oldAccount = dao.findByName(Name);
Account newAccount = dao.findByName(ToName);
oldAccount.setMoney(oldAccount.getMoney()-money);
newAccount.setMoney(newAccount.getMoney()+money);
updateAccount(oldAccount);
//int a =5/0;
updateAccount(newAccount);
}
}
3.3 测试类完美实现对Jdbc的控制
import com.service.IAccountService;
import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
@Test
void test() {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
IAccountService service = (IAccountService) ac.getBean("accountService");
service.transfer("aaa","bbb",1000);
}
}