💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
## 代码 ~~~ jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.10.10:3306/hibernate jdbc.username=homestead jdbc.password=secret ~~~ ~~~ package com.like.springconfig; import com.mchange.v2.c3p0.ComboPooledDataSource; import org.springframework.beans.factory.annotation.Qualifier; 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(basePackages = "com.like") @EnableTransactionManagement // <tx:annotation-driven transaction-manager="transactionManager"/> public class SpringConfig { //创建C3P0 @Bean(name = "c3p0") public DataSource createDataSourceC3p0() throws Exception { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://192.168.10.10:3306/hibernate"); dataSource.setUser("homestead"); dataSource.setPassword("secret"); return dataSource; } @Bean(name = "jdbcTemplate") public JdbcTemplate createJdbcTemplate(@Qualifier("c3p0") DataSource dataSource) { JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.setDataSource(dataSource); return jdbcTemplate; } @Bean(name = "transactionManager") public DataSourceTransactionManager createTransactionManager(@Qualifier("c3p0") DataSource dataSource) { DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager(); dataSourceTransactionManager.setDataSource(dataSource); return dataSourceTransactionManager; } } ~~~ ~~~ package com.like.dao; public interface TransferDao { void toMoney(String toUser, Double money); void inMoney(String inUser, Double money); } ~~~ ~~~ package com.like.daoImpl; import com.like.dao.TransferDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository("transferDao") public class TransferDaoImpl implements TransferDao { @Autowired private JdbcTemplate jdbcTemplate; @Override public void toMoney(String toUser, Double money) { String sql = "update account set money = money + ? where username = ?"; jdbcTemplate.update(sql, money, toUser); } @Override public void inMoney(String inUser, Double money) { String sql = "update account set money = money - ? where username = ?"; jdbcTemplate.update(sql, money, inUser); } } ~~~ ~~~ package com.like.service; public interface TransferService { void transfer(String toUser, String inUser, Double money); } ~~~ ~~~ package com.like.serviceImpl; import com.like.dao.TransferDao; import com.like.service.TransferService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service("transferService") public class TransferServiceImpl implements TransferService { @Autowired private TransferDao transferDao; @Override @Transactional public void transfer(String toUser, String inUser, Double money) { //加钱 transferDao.toMoney(toUser, money); //减钱 transferDao.inMoney(inUser, money); } } ~~~ ~~~ import com.like.service.TransferService; import com.like.springconfig.SpringConfig; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @ContextConfiguration(classes = SpringConfig.class) @RunWith(value = SpringJUnit4ClassRunner.class) public class Test { @Autowired private TransferService transferService; @org.junit.Test public void test() { transferService.transfer("jack", "milan", 500.0); } } ~~~ ## JDBCTemplate方式 ~~~ package com.like.config; 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.jdbc.datasource.DriverManagerDataSource; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; @Configuration @ComponentScan("com.like") @EnableTransactionManagement public class SpringConfig { @Bean public DataSource createDataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://192.168.10.10:3306/jdbc"); dataSource.setUsername("homestead"); dataSource.setPassword("secret"); return dataSource; } @Bean("jdbcTemplate") public JdbcTemplate createJt(DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean("transactionManager") public PlatformTransactionManager createTransactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } } ~~~ ~~~ package com.like.domain; import java.io.Serializable; public class Account implements Serializable { private Integer id; private String name; private Float money; @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Float getMoney() { return money; } public void setMoney(Float money) { this.money = money; } } ~~~ ~~~ package com.like.dao; import com.like.domain.Account; public interface IAccountDao { Account findAccountByName(String name); void updateAccount(Account account); } ~~~ ~~~ package com.like.dao.impl; import com.like.dao.IAccountDao; import com.like.domain.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import java.util.List; @Repository public class AccountDaoImpl implements IAccountDao { @Autowired private JdbcTemplate jt; public Account findAccountByName(String name) { List<Account> accounts = jt.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), name); if (accounts.isEmpty()) { return null; } if (accounts.size() > 1) { throw new RuntimeException("结果集不唯一"); } return accounts.get(0); } public void updateAccount(Account account) { jt.update("update account set money = ? where name = ?", account.getMoney(), account.getName()); } } ~~~ ~~~ package com.like.service; public interface IAccountService { void transfer(String sourceName,String targetName,Float money); } ~~~ ~~~ package com.like.service.impl; import com.like.dao.IAccountDao; import com.like.domain.Account; import com.like.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class AccountServiceImpl implements IAccountService { @Autowired private IAccountDao accountDao; @Transactional 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 a = 1/0; accountDao.updateAccount(target); } } ~~~ 测试: ~~~ ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); IAccountService accountService = (IAccountService) context.getBean("accountServiceImpl"); accountService.transfer("jack", "milan", 500f); ~~~