Spring学习(八)

Spring事务

1
2
3
4
5
6
7
8
9
10
11
12
package com.xiaoguan.dao;

import com.xiaoguan.pojo.Account;
import org.springframework.stereotype.Repository;

@Repository
public interface AccountDao {
Account selectByActno(String actno);
int update(Account account);
int insert(Account account);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.xiaoguan.dao.imp;

import com.xiaoguan.dao.AccountDao;
import com.xiaoguan.pojo.Account;
import jakarta.annotation.Resource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class AccountDaoImpl implements AccountDao {
@Override
public int insert(Account account) {
String sql="insert into t_act values(?,?,?)";
int count = jdbcTemplate.update(sql,account.getId(),account.getActno(), account.getBalance());
return count;
}

@Resource(name ="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Override
public Account selectByActno(String actno) {
String sql="select actno,id,balance from t_act where actno=?";
Account account = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Account.class),actno);
return account;

}

@Override
public int update(Account account) {
String sql="update t_act set balance=? where actno=?";
int count = jdbcTemplate.update(sql,account.getBalance(),account.getActno());
return count;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.xiaoguan.pojo;

import java.util.Objects;

public class Account {
private Long id;
private String actno;
private Double balance;

@Override
public String toString() {
return "Account{" +
"id=" + id +
", actno='" + actno + '\'' +
", balance=" + balance +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Account account = (Account) o;
return Objects.equals(getId(), account.getId()) && Objects.equals(getActno(), account.getActno()) && Objects.equals(getBalance(), account.getBalance());
}

@Override
public int hashCode() {
return Objects.hash(getId(), getActno(), getBalance());
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getActno() {
return actno;
}

public void setActno(String actno) {
this.actno = actno;
}

public Double getBalance() {
return balance;
}

public void setBalance(Double balance) {
this.balance = balance;
}

public Account(Long id, String actno, Double balance) {
this.id = id;
this.actno = actno;
this.balance = balance;
}

public Account() {
}
}

1
2
3
4
5
6
7
8
9
10
11
package com.xiaoguan.service;

import com.xiaoguan.pojo.Account;
import org.springframework.stereotype.Service;

@Service
public interface AccountService {
void transfer(String fromActno,String toActno,double money);
void save(Account act);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.xiaoguan.service.impl;

import com.xiaoguan.dao.AccountDao;
import com.xiaoguan.pojo.Account;
import com.xiaoguan.service.AccountService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service

public class AccountServiceImpl implements AccountService {
@Resource(name = "accountServiceImpl2")
private AccountService accountService2;
@Resource(name = "accountDaoImpl")
private AccountDao accountDao;
@Override
@Transactional
public void transfer(String fromActno, String toActno, double money) {
Account fromAccount = accountDao.selectByActno(fromActno);
if (fromAccount.getBalance()<money) {
throw new RuntimeException("余额不足!");
}
Account toAccount = accountDao.selectByActno(toActno);
fromAccount.setBalance(fromAccount.getBalance()-money);
toAccount.setBalance(toAccount.getBalance()+money);
int count = accountDao.update(fromAccount);
//
count+=accountDao.update(toAccount);
if (count!=2) {
throw new RuntimeException("转账失败");
}

}


@Transactional(propagation=Propagation.REQUIRED)
public void withdraw(){

}
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void save(Account act) {
accountDao.insert(act);
Account account2=new Account(3L,"act003",10000.0);
accountService2.save(account2);

}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.xiaoguan.service.impl;

import com.xiaoguan.dao.AccountDao;
import com.xiaoguan.pojo.Account;
import com.xiaoguan.service.AccountService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountServiceImpl2 implements AccountService {
@Resource(name = "accountDaoImpl")
private AccountDao accountDao;
@Override
public void transfer(String fromActno, String toActno, double money) {

}

@Override
@Transactional(propagation = Propagation.REQUIRED)
public void save(Account act) {
accountDao.insert(act);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.xiaoguan.service.impl;

import com.xiaoguan.dao.AccountDao;
import com.xiaoguan.pojo.Account;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Service("11")
public class IsolationService1 {
@Resource(name = "accountDaoImpl")
private AccountDao accountDao;
@Transactional(isolation = Isolation.READ_COMMITTED)
public void getByActno(String actno){
Account account = accountDao.selectByActno(actno);
System.out.println(account);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.xiaoguan.service.impl;

import com.xiaoguan.dao.AccountDao;
import com.xiaoguan.pojo.Account;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("22")
public class IsolationService2 {
@Resource(name = "accountDaoImpl")
private AccountDao accountDao;
// @Transactional(timeout = 10 ,readOnly = true)//设置事务超时时间为10秒
public void save(Account account){
// try {
// Thread.sleep(1000*20);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
accountDao.insert(account);

}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.xiaoguan;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.xiaoguan")//组件扫描
@EnableTransactionManagement
public class Spring6Config {
@Bean(name = "dataSource")
public DruidDataSource getDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
druidDataSource.setUrl("jdbc:mysql://localhost:3306/spring6");
druidDataSource.setUsername("root");
druidDataSource.setPassword("68963120g");
return druidDataSource;
}
@Bean(name = "jdbcTemplate")
public JdbcTemplate getJDBCTemplate(DataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
return jdbcTemplate;
}
@Bean(name = "dataSourceTransactionManager")
public DataSourceTransactionManager getTX(DataSource dataSource){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>

<configuration>

<loggers>
<!--
level指定日志级别,从低到高的优先级:
ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
-->
<root level="OFF">
<appender-ref ref="spring6log"/>
</root>
</loggers>

<appenders>
<!--输出日志信息到控制台-->
<console name="spring6log" target="SYSTEM_OUT">
<!--控制日志输出的格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-3level %logger{1024} - %msg%n"/>
</console>
</appenders>

</configuration>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?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"
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/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="com.xiaoguan"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring6"/>
<property name="username" value="root"/>
<property name="password" value="68963120g"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务管理器-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 事务注解驱动器开启-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>

</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.xiaoguan.test;

import com.xiaoguan.Spring6Config;
import com.xiaoguan.pojo.Account;
import com.xiaoguan.service.AccountService;
import com.xiaoguan.service.impl.IsolationService1;
import com.xiaoguan.service.impl.IsolationService2;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BankTest {
@Test
public void testNoXML(){
ApplicationContext applicationContext=new AnnotationConfigApplicationContext(Spring6Config.class);
AccountService accountService = applicationContext.getBean("accountServiceImpl", AccountService.class);
try {
accountService.transfer("act001","act002",10000);
System.out.println("转账成功");
}catch (Exception e){
e.printStackTrace();
System.out.println("转账失败");
}

}
@Test
public void testIsolation1(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
IsolationService1 is1 = applicationContext.getBean("11", IsolationService1.class);
IsolationService2 is2 = applicationContext.getBean("22", IsolationService2.class);
is1.getByActno("actno11");
}
@Test
public void testIsolation2(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
IsolationService2 is2 = applicationContext.getBean("22", IsolationService2.class);
Account account=new Account(null,"actno13",77777.0);
is2.save(account);
}
@Test
public void testPropagation(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
AccountService accountService = applicationContext.getBean("accountServiceImpl", AccountService.class);
Account act=new Account(4L,"act004",80000.0);
accountService.save(act);
}
@Test
public void testSpringTx(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
AccountService accountService = applicationContext.getBean("accountServiceImpl", AccountService.class);
try {
accountService.transfer("act001","act002",10000);
System.out.println("转账成功");
}catch (Exception e){
e.printStackTrace();
System.out.println("转账失败");
}
}
}

1
2
3
4
5
6
7
8
9
10
11
package com.xiaoguan.dao;

import com.xiaoguan.pojo.Account;
import org.springframework.stereotype.Repository;

@Repository
public interface AccountDao {
Account selectByActno(String actno);
int update(Account account);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.xiaoguan.dao.impl;

import com.xiaoguan.dao.AccountDao;
import com.xiaoguan.pojo.Account;
import jakarta.annotation.Resource;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class AccountDaoImpl implements AccountDao {
@Resource(name ="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
@Override
public Account selectByActno(String actno) {
String sql="select actno,id,balance from t_act where actno=?";
Account account = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Account.class),actno);
return account;

}

@Override
public int update(Account account) {
String sql="update t_act set balance=? where actno=?";
int count = jdbcTemplate.update(sql,account.getBalance(),account.getActno());
return count;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.xiaoguan.pojo;

import java.util.Objects;

public class Account {
private Long id;
private String actno;
private Double balance;

@Override
public String toString() {
return "Account{" +
"id=" + id +
", actno='" + actno + '\'' +
", balance=" + balance +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Account account = (Account) o;
return Objects.equals(getId(), account.getId()) && Objects.equals(getActno(), account.getActno()) && Objects.equals(getBalance(), account.getBalance());
}

@Override
public int hashCode() {
return Objects.hash(getId(), getActno(), getBalance());
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getActno() {
return actno;
}

public void setActno(String actno) {
this.actno = actno;
}

public Double getBalance() {
return balance;
}

public void setBalance(Double balance) {
this.balance = balance;
}

public Account(Long id, String actno, Double balance) {
this.id = id;
this.actno = actno;
this.balance = balance;
}

public Account() {
}
}

1
2
3
4
5
6
7
8
9
10
package com.xiaoguan.service;

import com.xiaoguan.pojo.Account;
import org.springframework.stereotype.Service;

@Service
public interface AccountService {
void transfer(String fromActno,String toActno,double money);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.xiaoguan.service.impl;

import com.xiaoguan.dao.AccountDao;
import com.xiaoguan.pojo.Account;
import com.xiaoguan.service.AccountService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service

public class AccountServiceImpl implements AccountService {
@Resource(name = "accountDaoImpl")
private AccountDao accountDao;
@Override
public void transfer(String fromActno, String toActno, double money) {
Account fromAccount = accountDao.selectByActno(fromActno);
if (fromAccount.getBalance()<money) {
throw new RuntimeException("余额不足!");
}
Account toAccount = accountDao.selectByActno(toActno);
fromAccount.setBalance(fromAccount.getBalance()-money);
toAccount.setBalance(toAccount.getBalance()+money);
int count = accountDao.update(fromAccount);
// String s=null;
// s.toString();
count+=accountDao.update(toAccount);
if (count!=2) {
throw new RuntimeException("转账失败");
}

}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?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"
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/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="com.xiaoguan"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring6"/>
<property name="username" value="root"/>
<property name="password" value="68963120g"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事务管理器-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="dataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置通知的相关属性,通知当中要配置事务管理器-->
<tx:advice id="tx" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<tx:method name="transfer" propagation="REQUIRED"/>
<!-- 还可以模糊匹配-->
<tx:method name="transfer*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面-->
<aop:config>
<!-- 配置切点-->
<aop:pointcut id="tx2" expression="execution(* com.xiaoguan.service..*(..))"/>
<!-- 配置切面-->
<aop:advisor advice-ref="tx" pointcut-ref="tx2"/>
</aop:config>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.xiaoguan.test;

import com.xiaoguan.service.AccountService;
import com.xiaoguan.service.impl.AccountServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Banktest {
@Test
public void testNoAnnotation(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
AccountService accountService = applicationContext.getBean("accountServiceImpl", AccountService.class);
try {
accountService.transfer("act001","act002",10.333);
}catch (Exception e){
e.printStackTrace();
}
}
}

单元测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.xiaoguan.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Objects;
@Component
public class User {
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(getName(), user.getName());
}

@Override
public int hashCode() {
return Objects.hash(getName());
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public User() {
}

public User(String name) {
this.name = name;
}

@Value("张三")
private String name;
}

1
2
3
4
5
6
7
8
9
10
11
12
<?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"
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/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="com.xiaoguan"/>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.xiaoguan.test;

import com.xiaoguan.bean.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class SpringJunit4Test {
@Autowired
private User user;
@Test
public void testUser(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
User user = applicationContext.getBean("user", User.class);
System.out.println(user);
}
@Test
public void testUser2(){
System.out.println(user);
}


}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.xiaoguan.test;

import com.xiaoguan.bean.User;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ContextConfiguration("classpath:spring.xml")
@ExtendWith(SpringExtension.class)
public class SpringJunit5Test {
@Autowired
private User user;
@Test
public void testUser(){
System.out.println(user.getName());
}
}

集成MyBatis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.xiaoguan.test;

import com.xiaoguan.bean.User;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ContextConfiguration("classpath:spring.xml")
@ExtendWith(SpringExtension.class)
public class SpringJunit5Test {
@Autowired
private User user;
@Test
public void testUser(){
System.out.println(user.getName());
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.xiaoguan.pojo;

import java.util.Objects;

public class Account {
private Long id;
private String actno;
private Double balance;

@Override
public String toString() {
return "Account{" +
"id=" + id +
", actno='" + actno + '\'' +
", balance=" + balance +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Account account = (Account) o;
return Objects.equals(getId(), account.getId()) && Objects.equals(getActno(), account.getActno()) && Objects.equals(getBalance(), account.getBalance());
}

@Override
public int hashCode() {
return Objects.hash(getId(), getActno(), getBalance());
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getActno() {
return actno;
}

public void setActno(String actno) {
this.actno = actno;
}

public Double getBalance() {
return balance;
}

public void setBalance(Double balance) {
this.balance = balance;
}

public Account(Long id, String actno, Double balance) {
this.id = id;
this.actno = actno;
this.balance = balance;
}

public Account() {
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.xiaoguan.service;

import com.xiaoguan.pojo.Account;

import java.util.List;

public interface AccountService {
int save(Account account);
int deleteByActno(String actno);
int modify(Account account);
Account getByActno(String actno);
List<Account> getAll();
void transfer(String fromActno,String toActno,Double money);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.xiaoguan.service.impl;

import com.xiaoguan.mapper.AccountMapper;
import com.xiaoguan.pojo.Account;
import com.xiaoguan.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional
@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountMapper accountMapper;
@Override
public int save(Account account) {
return accountMapper.insert(account);
}

@Override
public int deleteByActno(String actno) {
return accountMapper.deleteByAccountNum(actno);
}

@Override
public int modify(Account account) {
return accountMapper.update(account);
}

@Override
public Account getByActno(String actno) {
return accountMapper.selectByactno(actno);
}

@Override
public List<Account> getAll() {
return accountMapper.selectAll();
}

@Override
public void transfer(String fromActno, String toActno, Double money) {
Account fromAccount = accountMapper.selectByactno(fromActno);
if (fromAccount.getBalance()<money) {
throw new RuntimeException("对不起,钱不够");
}
Account toAccount = accountMapper.selectByactno(toActno);
fromAccount.setBalance(fromAccount.getBalance()-money);
toAccount.setBalance(toAccount.getBalance()+money);
int count = accountMapper.update(fromAccount);
// String s=null;
// s.toString();
count+=accountMapper.update(toAccount);
if (count!=2) {
throw new RuntimeException("转账失败,请联系管理员");
}
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xiaoguan.mapper.AccountMapper">
<insert id="insert">
insert into t_act values (null,#{actno},#{balance})
</insert>
<delete id="deleteByAccountNum">
delete from t_act where actno=#{actno}
</delete>
<update id="update">
update t_act set balance=#{balance} where actno=#{actno}
</update>
<select id="selectByactno" resultType="Account">
select * from t_act where actno=#{actno}
</select>
<select id="selectAll" resultType="Account">
select * from t_act
</select>

</mapper>
1
2
3
4
5
6
7
<?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"
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">
<context:component-scan base-package="com.xiaoguan"/>
</beans>
1
2
3
4
5
6
7
8
9
10
11
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 帮助打印mybatis配置信息,包括sql语句等-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

</configuration>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<import resource="common.xml"/>
<context:property-placeholder location="jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.xiaoguan.pojo"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xiaoguan.mapper"/>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.xiaoguan.test;
import com.xiaoguan.service.AccountService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SMTest {
@Test
public void testSM(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");
AccountService accountService = applicationContext.getBean("accountService", AccountService.class);
accountService.transfer("act001","act002",10000.0);
}
}