当前位置:   article > 正文

Spring事务和JDBCTemplate(全注解)_java注解事务能操作普通jdbc吗

java注解事务能操作普通jdbc吗

类一(user_dao):

  1. public interface user_dao {
  2. public void addmoney();
  3. public void reducemoney();
  4. public void query();
  5. }

类二(user_dao_impl):

  1. @Repository
  2. public class user_dao_impl implements user_dao {
  3. @Autowired
  4. private JdbcTemplate jdbcTemplate;
  5. @Override
  6. public void addmoney() {
  7. int a = jdbcTemplate.update("update users1 set money = money-100 where uname=?", "张三");
  8. System.out.println(a);
  9. }
  10. @Override
  11. public void reducemoney() {
  12. int i = jdbcTemplate.update("update users1 set money = money+100 where uname=?", "李四");
  13. System.out.println(i);
  14. }
  15. @Override
  16. public void query() {
  17. List<user> query = jdbcTemplate.query("select * from users1", new BeanPropertyRowMapper<>(user.class));
  18. System.out.println(query);
  19. }
  20. }

类三(pojo.user)

  1. public class user {
  2. private String uname;
  3. private Double money;
  4. public user(String uname, Double money) {
  5. this.uname = uname;
  6. this.money = money;
  7. }
  8. public user() {
  9. }
  10. public String getUname() {
  11. return uname;
  12. }
  13. public void setUname(String uname) {
  14. this.uname = uname;
  15. }
  16. public Double getMoney() {
  17. return money;
  18. }
  19. public void setMoney(Double money) {
  20. this.money = money;
  21. }
  22. @Override
  23. public String toString() {
  24. return "user{" +
  25. "uname='" + uname + '\'' +
  26. ", money=" + money +
  27. '}';
  28. }
  29. }

类四(UserService)

  1. @org.springframework.stereotype.Service
  2. @Transactional(readOnly = false)
  3. public class UserService {
  4. @Autowired
  5. private user_dao_impl user_dao_impl;
  6. public void testMoney(){
  7. user_dao_impl.addmoney();
  8. user_dao_impl.reducemoney();
  9. user_dao_impl.query();
  10. }
  11. }

配置类(模拟XML文件):

  1. @Configuration
  2. @ComponentScan(basePackages = "com")
  3. @EnableTransactionManagement
  4. public class DoConfig {
  5. @Bean
  6. public DruidDataSource getDataSource(){
  7. DruidDataSource druidDataSource = new DruidDataSource();
  8. druidDataSource.setUsername("root");
  9. druidDataSource.setUrl("jdbc:mysql:///users?useUnicode=true&characterEncoding=utf-8");
  10. druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
  11. druidDataSource.setPassword("123456");
  12. return druidDataSource;
  13. }
  14. @Bean
  15. public JdbcTemplate getJdbcTemplate(DruidDataSource druidDataSource){
  16. JdbcTemplate jdbcTemplate = new JdbcTemplate();
  17. jdbcTemplate.setDataSource(druidDataSource);
  18. return jdbcTemplate;
  19. }
  20. @Bean
  21. public DataSourceTransactionManager getTransactional(DruidDataSource druidDataSource){
  22. DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
  23. dataSourceTransactionManager.setDataSource(druidDataSource);
  24. return dataSourceTransactionManager;
  25. }
  26. }

测试类:

  1. public class Test01 {
  2. @Test
  3. public void test01(){
  4. ApplicationContext applicationContext = new AnnotationConfigApplicationContext(DoConfig.class);
  5. UserService userService = applicationContext.getBean("userService", UserService.class);
  6. userService.testMoney();
  7. }
  8. }

总结Spring中常见的注释:

@Component
@Service     一般用在Service层
@Controller   一般用在Web层
@Repository    一般用在Dao层
@Transactional  开启事务一般用在Service层
@Configuration  通常用于全注解开发时写xml文件的类
@ComponentScan 组件扫描

事务的4大特性:

(1).原子性:两个的操作要么都成功,要么都失败.
(2).一致性:在操作中总数保持不变
(3).隔离性:多个人去操作数据,不会受其影响
(4).持久性:操作后数据会同步更新到数据库中

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/576786
推荐阅读
相关标签
  

闽ICP备14008679号