标签:word cto 连接 测试 publickey contex mysql ESS throw
package com.sunxiaping.bean; import java.io.Serializable; public class Account implements Serializable { private Integer id; private String name; private Double 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 Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } }
package com.sunxiaping.dao; import com.sunxiaping.bean.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository public class AccountDao { @Autowired private JdbcTemplate jdbcTemplate; public void save(Account account) { jdbcTemplate.update(" insert into account(`name`,`money`) value (?,?) ", account.getName(), account.getMoney()); } }
package com.sunxiaping.service; import com.sunxiaping.bean.Account; import com.sunxiaping.dao.AccountDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service @Transactional public class AccountService { @Autowired private AccountDao accountDao; public void saveAccount(Account account) { accountDao.save(account); int i = 1 / 0; } }
package com.sunxiaping.config; import com.mchange.v2.c3p0.ComboPooledDataSource; 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.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import javax.sql.DataSource; import java.beans.PropertyVetoException; @ComponentScan(value = "com.sunxiaping") @EnableTransactionManagement @Configuration //告诉Spring这是一个配置类 public class SpringConfig { /** * 注册数据源 * * @return 数据源 * @throws PropertyVetoException 异常 */ @Bean public DataSource dataSource() throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true"); dataSource.setDriverClass("com.mysql.cj.jdbc.Driver"); dataSource.setUser("root"); dataSource.setPassword("123456"); return dataSource; } /** * 注册JdbcTemplate * * @param dataSource 数据源 * @return JdbcTemplate */ @Bean public JdbcTemplate jdbcTemplate(DataSource dataSource) { return new JdbcTemplate(dataSource); } /** * 注册事务管理器 * * @param dataSource 数据源 * @return 事务管理器 */ @Bean public PlatformTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } }
package com.sunxiaping.test; import com.sunxiaping.bean.Account; import com.sunxiaping.config.SpringConfig; import com.sunxiaping.service.AccountService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class SpringTest { @Test public void test() { ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class); AccountService accountService = context.getBean(AccountService.class); Account account = new Account(); account.setName("张三"); account.setMoney(20.1); accountService.saveAccount(account); } }
标签:word cto 连接 测试 publickey contex mysql ESS throw
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/11979947.html