Spring如何基于xml实现声明式事务控制

网友投稿 247 2022-11-18


Spring如何基于xml实现声明式事务控制

一、pom.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.example

A02spring

1.0-SNAPSHOT

org.springframework

spring-context

5.2.8.RELEASE

org.springframework

spring-jdbc

5.2.8.RELEASE

org.springframework

spring-tx

5.2.8.RELEASE

org.aspectj

aspectjweaver

1.9.6

mysql

mysql-connector-java

8.0.11

org.projectlombok

lombok

1.18.12

provided

org.springframework

spring-test

5.2.8.RELEASE

test

junit

junit

4.13

test

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.example

A02spring

1.0-SNAPSHOT

org.springframework

spring-context

5.2.8.RELEASE

org.springframework

spring-jdbc

5.2.8.RELEASE

org.springframework

spring-tx

5.2.8.RELEASE

org.aspectj

aspectjweaver

1.9.6

mysql

mysql-connector-java

8.0.11

org.projectlombok

lombok

1.18.12

provided

org.springframework

spring-test

5.2.8.RELEASE

test

junit

junit

4.13

test

org.apache.maven.plugins

maven-compiler-plugin

1.8

1.8

二、spring的xml配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans

https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

https://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

https://springframework.org/schema/aop/spring-aop.xsd">

value="jdbc:mysql://192.168.2.105:3306/ssm?characterEncoding=utf8&useSSL=false">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xsi:schemaLocation="

http://springframework.org/schema/beans

https://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/tx

https://springframework.org/schema/tx/spring-tx.xsd

http://springframework.org/schema/aop

https://springframework.org/schema/aop/spring-aop.xsd">

value="jdbc:mysql://192.168.2.105:3306/ssm?characterEncoding=utf8&useSSL=false">

value="jdbc:mysql://192.168.2.105:3306/ssm?characterEncoding=utf8&useSSL=false">

三、实体类

package com.wuxi.beans;

import lombok.Data;

import java.io.Serializable;

@Data

public class Account implements Serializable {

private Integer id;

private String name;

private Float money;

}

四、dao

1、接口

package com.wuxi.daos;

import com.wuxi.beans.Account;

public interface AccountDao {

Account findAccountById(Integer accountId);

Account findAccountByName(String accountName);

void updateAccount(Account account);

}

2、实现类

package com.wuxi.daos.impl;

import com.wuxi.beans.Account;

import com.wuxi.daos.AccountDao;

import org.springframework.jdbc.core.BeanPropertyRowMapper;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.util.List;

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {

@Override

public Account findAccountById(Integer accountId) {

List accounts = getJdbcTemplate().query("select * from account where id = ?", new BeanPropertyRowMapper(Account.class), accountId);

return accounts.isEmpty() ? null : accounts.get(0);

}

@Override

public Account findAccountByName(String accountName) {

List accounts = getJdbcTemplate().query("select * from account where name = ?", new BeanPropertyRowMapper(Account.class), accountName);

if (accounts.isEmpty()) {

return null;

}

if (accounts.size() > 1) {

throw new RuntimeException("结果集不唯一");

}

return accounts.get(0);

}

@Override

public void updateAccount(Account account) {

getJdbcTemplate().update("update account set name=?,money=? where id=?", account.getName(), account.getMoney(), account.getId());

}

}

五、service

1、接口

package com.wuxi.services;

import com.wuxi.beans.Account;

public interface AccountService {

Account findAccounById(Integer accountId);

void transfer(String sourceName, String targetName, Float money);

}

2、实现类

package com.wuxi.services.impl;

import com.wuxi.beans.Account;

import com.wuxi.daos.AccountDao;

import com.wuxi.services.AccountService;

public class AccountServiceImpl implements AccountService {

private AccountDao accountDao;

public void setAccountDao(AccountDao accountDao) {

this.accountDao = accountDao;

}

@Override

public Account findAccounById(Integer accountId) {

return accountDao.findAccountById(accountId);

}

@Override

public void transfer(String sourceName, String targetName, Float money) {

Account source = accountDao.findAccountByName(sourceName);

Account target = accountDao.findAccountByName(targetName);

source.setMoney(source.getMoney() - money);

target.setMoney(target.getMoney() + money);

accountDao.updateAccount(source);

int i = 1 / 0;

accountDao.updateAccount(target);

}

}

六、测试

package com.wuxi.tests;

import com.wuxi.services.AccountService;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnhttp://it4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations = "classpath:application.xml")

public class MySpringTest {

@Autowired

private AccountService as;

@Test

public void testTransfer() {

as.transfer("aaa", "bbb", 100f);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:解决websocket 报 Could not decode a text frame as UTF
下一篇:Springmvc异常处理器及拦截器实现代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~