spring纯注解的account案例
dao层:
package cn.mepu.dao.imp; import cn.mepu.dao.IAccountDao; import cn.mepu.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; /** * @author shkstart * @create 2019-11-10 16:09 */ @Repository("accountDao") public class AccountDaoImp implements IAccountDao { @Autowired private JdbcTemplate jdbcTemplate; public Account findAccountById(Integer accountId) { List<Account> accounts = jdbcTemplate.query("select * from account where id = ?",new BeanPropertyRowMapper<Account>(Account.class),accountId); return accounts.isEmpty()?null:accounts.get(0); } public Account findAccountByName(String accountName) { List<Account> accounts = jdbcTemplate.query("select * from account where name = ?",new BeanPropertyRowMapper<Account>(Account.class),accountName); if(accounts.isEmpty()){ return null; } if(accounts.size()>1){ throw new RuntimeException("结果集不唯一"); } return accounts.get(0); } public void updateAccount(Account account) { jdbcTemplate.update("update account set name=?,money=? where id=?",account.getName(),account.getMoney(),account.getId()); } }
service层:
package cn.mepu.service.impl; import cn.mepu.dao.IAccountDao; import cn.mepu.domain.Account; import cn.mepu.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; /** * @author shkstart * @create 2019-11-11 8:43 */ @Service("accountService") @Transactional(propagation = Propagation.SUPPORTS,readOnly=true)//只读配置 public class AccountServiceImpl implements IAccountService { @Autowired private IAccountDao accountDao; @Override public Account findAccountById(Integer accountId) { return accountDao.findAccountById(accountId); } @Transactional(propagation = Propagation.REQUIRED,readOnly=false)//只读配置 public void transfer(String sourceName, String targetName, float money) { //2.1根据名称查询转出账户 Account source = accountDao.findAccountByName(sourceName); //2.2根据名称查询转入账户 Account target = accountDao.findAccountByName(targetName); //2.3转出账户减钱 source.setMoney(source.getMoney()-money); //2.4转入账户加钱 target.setMoney(target.getMoney()+money); //2.5更新转出账户 accountDao.updateAccount(source); // int i=1/0; //2.6更新转入账户 accountDao.updateAccount(target); } }
配置文件:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/javaee jdbc.username=root jdbc.password=root
spring配置类:
package cn.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.PropertySource; import org.springframework.transaction.annotation.EnableTransactionManagement; /** * @author shkstart * @create 2019-11-11 10:52 * spring的配置类 */ @Configuration @ComponentScan("cn.mepu") @Import({JdbcConfig.class,TransactionConfig.class}) @PropertySource("jdbcConfig.properties") @EnableTransactionManagement public class SpringConfiguration { }
事务相关配置类;
package cn.config; import org.springframework.context.annotation.Bean; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import javax.sql.DataSource; /** * @author shkstart * @create 2019-11-11 11:12 * 和事务相关的配置类 */ public class TransactionConfig { /** * 创建事务管理器对象 * @param dataSource * @return */ @Bean("transactionManager") public PlatformTransactionManager createTransactionManager(DataSource dataSource){ return new DataSourceTransactionManager(dataSource); } }
连接数据库相关的配置类:
package cn.config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DriverManagerDataSource; import javax.sql.DataSource; /** * @author shkstart * @create 2019-11-11 10:55 * 连接数据库相关的配置类 */ public class JdbcConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; /** * 创建jdbcTemplat容器 * @param dataSource * @return */ @Bean(name = "jdbcTemplate") public JdbcTemplate createJdbcTemplate(DataSource dataSource){ return new JdbcTemplate(dataSource); } /** * 创建数据源对象 * @return */ @Bean(name={"dataSource"}) public DataSource createDataSource(){ DriverManagerDataSource ds = new DriverManagerDataSource(); ds.setDriverClassName(driver); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); return ds; } }
测试类:
package cn.mepu; import cn.config.SpringConfiguration; import cn.mepu.service.IAccountService; 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.SpringJUnit4ClassRunner; /** * @author shkstart * @create 2019-11-11 8:51 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = SpringConfiguration.class) public class TestAccount { @Autowired private IAccountService as; @Test public void testTransfer(){ as.transfer("aaa","bbb",100f); } }
spring纯注解的account案例的更多相关文章
- spring 纯注解方式 与AOP
spring注解方式 以前我也使用过纯注解方式.现在在这里做个记录 我们先认识几个我们都耳熟能详的注解 @configuration :从spring3.0这个注解就可以用于定义配置类,可以替换xml ...
- Spring纯注解配置
待改造的问题 我们发现,之所以我们现在离不开 xml 配置文件,是因为我们有一句很关键的配置: <!-- 告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器 ...
- Spring进阶案例之注解和IoC案例
Spring进阶案例之注解和IoC案例 一.常见的注解分类及其作用 从此前的基于xml的IoC开发案例和依赖注入案例中,我们可以将xml配置归纳为: <bean id="" ...
- spring in action 学习笔记十四:用纯注解的方式实现spring mvc
在讲用纯注解的方式实现springmvc之前先介绍一个类:AbstractAnnotationDispatcherServletInitializer.这个类的作用是:任何一个类继承AbstractA ...
- spring基于注解的IoC以及IoC的案例
1.Spring中IoC的常用注解 1.1明确: (1)基于注解的配置和xml的配置要实现的功能都是一样的,都是要降低程序之间的耦合,只是配置的形式不一样 2.案例:使用xml方式和注解方式实现单表的 ...
- spring提供的事务配置--纯注解
spring提供的事务--纯注解 模拟转账业务 ,出错需要事务回滚,没错正常执行 事务和数据库技术都是spring的内置提供的 --------dao包--------------- IAccoun ...
- spring boot纯注解开发模板
简介 spring boot纯注解开发模板 创建项目 pom.xml导入所需依赖 点击查看源码 <dependencies> <dependency> <groupId& ...
- Spring MVC4 纯注解配置教程
阅读本文需要又一定的sping基础,最起码要成功的运行过一个SpringMvc项目. 在传统的Spring项目中,我们要写一堆的XML文件.而这些XML文件格式要求又很严格,很不便于开发.而网上所谓的 ...
- Spring 使用纯注解方式完成IoC
目录 创建一个简单的Person类 使用xml方式配置Spring容器并获取bean的过程 创建xml配置文件 进行测试 使用纯注解方式配置Spring容器并获取bean的过程 创建spring配置类 ...
随机推荐
- Using-JSONNET-for-dynamic-JSON-parsing
原文 https://weblog.west-wind.com/posts/2012/Aug/30/Using-JSONNET-for-dynamic-JSON-parsing With the re ...
- 前端工程师的新选择WebApp
作为新一代移动端应用分发入口,小程序的趋势明朗化,竞争也在急剧激烈化.战线从手机 QQ.QQ 浏览器.支付宝.手机淘宝,华为,小米等九家手机厂商推出“快应用”,再拉到了谷歌的 Instant App ...
- c# 编程--数组例题
1.输入十个学生的成绩,找出最高分 #region 输入十个学生的成绩,找出最高分 //输入十个学生的成绩,找出最高分 ]; ; i < ; i++) { ; Console.Write(&qu ...
- 2018-8-29-win2d-渐变颜色
title author date CreateTime categories win2d 渐变颜色 lindexi 2018-08-29 08:58:14 +0800 2018-7-7 20:5:3 ...
- Python 获取当前文件所在路径
记录几个os获取路径的函数 1. os.path.realpath(__file__):获取文件的绝对路径,包括文件自己的名字 2.os.path.dirname(path):获取path路径的上级路 ...
- python 循环中使用多个subplot画子图像(python matplotlib use more than one subplot in loop)
在循环语句中画出多个subplot图像代码如下 http://jonathansoma.com/lede/data-studio/classes/small-multiples/long-explan ...
- 【leedcode】950. Reveal Cards In Increasing Order
题目如下: In a deck of cards, every card has a unique integer. You can order the deck in any order you ...
- Spring整合SpringDataJpa配置文件头
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- Steup Factory 操作注册表
//判断注册表是否存在,不存在就创建 result = Registry.DoesKeyExist(HKEY_CURRENT_USER, "SOFTWARE\\MyTestApp" ...
- spring+websocket的整合实例--可使用
spring+websocket的整合实例----借鉴如下链接--此贴用于笔记 https://blog.csdn.net/qq_35515521/article/details/78610847