多平台统一管理软件接口,如何实现多平台统一管理软件接口
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">
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">
二、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
return accounts.isEmpty() ? null : accounts.get(0);
}
@Override
public Account findAccountByName(String accountName) {
List
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小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~