19

您所在的位置:网站首页 spring5日志 19

19

#19| 来源: 网络整理| 查看: 265

1.基于完全注解和基于xml配置文件的比较

下面是一个xml配置文件bean1.xml

其实xml配置文件是可以用java配置类完全代替的 这是上文xml配置文件对应的java配置类写法 配置类SpringConfig

package com.limi.config;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 //配置类, 代替xml配置文件 @ComponentScan(basePackages = "com.limi") //组件扫描 @EnableTransactionManagement //开启事务 public class SpringConfig {//创建数据库连接池//@Bean注解写在方法上面, 用于将方法的返回值加入到IOC容器中@Beanpublic DruidDataSource getDruidDataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/db_springtest");dataSource.setUsername("root");dataSource.setPassword("123456");return dataSource;}//创建 JdbcTemplate 对象, 参数dataSource会自动从IOC容器中寻找并注入@Beanpublic JdbcTemplate getJdbcTemplate(DataSource dataSource) {//到 ioc 容器中根据类型找到 dataSourceJdbcTemplate jdbcTemplate = new JdbcTemplate();//注入 dataSourcejdbcTemplate.setDataSource(dataSource);return jdbcTemplate;}//创建事务管理器@Beanpublic DataSourceTransactionManagergetDataSourceTransactionManager(DataSource dataSource) {DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();transactionManager.setDataSource(dataSource);return transactionManager;} } 2.项目代码

在这里插入图片描述 配置类SpringConfig

package com.limi.config;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 //配置类, 代替xml配置文件 @ComponentScan(basePackages = "com.limi") //组件扫描 @EnableTransactionManagement //开启事务 public class SpringConfig {//创建数据库连接池//@Bean注解写在方法上面, 用于将方法的返回值加入到IOC容器中@Beanpublic DruidDataSource getDruidDataSource() {DruidDataSource dataSource = new DruidDataSource();dataSource.setDriverClassName("com.mysql.jdbc.Driver");dataSource.setUrl("jdbc:mysql://localhost:3306/db_springtest");dataSource.setUsername("root");dataSource.setPassword("123456");return dataSource;}//创建 JdbcTemplate 对象, 参数dataSource会自动从IOC容器中寻找并注入@Beanpublic JdbcTemplate getJdbcTemplate(DataSource dataSource) {//到 ioc 容器中根据类型找到 dataSourceJdbcTemplate jdbcTemplate = new JdbcTemplate();//注入 dataSourcejdbcTemplate.setDataSource(dataSource);return jdbcTemplate;}//创建事务管理器@Beanpublic DataSourceTransactionManagergetDataSourceTransactionManager(DataSource dataSource) {DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();transactionManager.setDataSource(dataSource);return transactionManager;}}

Account

package com.limi.entity;public class Account {private Integer id;private String userName;private Double price;public Account(){}public Account(Integer id, String userName, Double price) {this.id = id;this.userName = userName;this.price = price;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public Double getPrice() {return price;}public void setPrice(Double price) {this.price = price;} }

AccountDao

package com.limi.dao;public interface AccountDao {//修改账户余额int updateMoneyById(Integer id, Double money); }

AccountDaoImpl

package com.limi.dao;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository;@Repository public class AccountDaoImpl implements AccountDao{@Autowiredprivate JdbcTemplate jdbcTemplate;@Overridepublic int updateMoneyById(Integer id, Double money) {String sql;if(money>0)sql = "update t_account set money = money+? where id = ?";//加法else{money = -money;sql = "update t_account set money = money-? where id = ?";//减法}int res = jdbcTemplate.update(sql, money, id);return res;} }

AccountService

package com.limi.service;import com.limi.dao.AccountDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;@Transactional @Service public class AccountService {@Autowiredprivate AccountDao accountDao;public void change(){//模拟1号账户给2号账户转账200accountDao.updateMoneyById(1, -200.00);//模拟出故障int a = 10/0;accountDao.updateMoneyById(2, 200.00);} }

测试类MyTest

package com.limi.test; import com.limi.config.SpringConfig; import com.limi.dao.AccountDao; import com.limi.service.AccountService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;public class MyTest {@Testpublic void test1(){//1.加载配置类ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);//2.获取配置的对象, 参数1:bean的id值, 参数2: 类名.classAccountService accountService = context.getBean("accountService", AccountService.class);//3.使用对象accountService.change();} }

执行转账前 在这里插入图片描述 在这里插入图片描述 执行结果 在这里插入图片描述 在这里插入图片描述 可以看到事务开启成功, 当故障出现执行了回滚, 保证了数据的正确性.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3