zoukankan      html  css  js  c++  java
  • (十四)SpringBoot之事务处理

    一、简介

    ssh ssm都有事务管理service层通过applicationContext.xml配置,所有service方法都加上事务操作;

    用来保证一致性,即service方法里的多个dao操作,要么同时成功,要么同时失败;

    springboot下的话,在service方法上加上@Transactional即可

    二、案例

       2.1  controller

    package com.shyroke.controller;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.data.domain.PageRequest;
    import org.springframework.data.domain.Pageable;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.shyroke.dao.UserMapper;
    import com.shyroke.entity.UserBean;
    import com.shyroke.service.UserService;
    
    @Controller
    @RequestMapping(value = "/")
    public class IndexController {
    
        @Autowired
        private UserService userService;
        
        @ResponseBody
        @RequestMapping(value="/save")
        public String list() {
        
            UserBean user1=new UserBean();
            user1.setUserName("user1");
            user1.setPassWord("123");
        
            userService.save(user1);
            
            return "index";
            
            
        }
    }
    • service

    package com.shyroke.service;
    
    import com.shyroke.entity.UserBean;
    
    public interface UserService {
    
        void save(UserBean user1);
    
    }
    • service实现类

      在下面的代码中,我们对save方法加上了@Transactional注解,表示使用事务,当有异常抛出时,就会自动回滚。

    package com.shyroke.service.impl;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import com.shyroke.dao.UserMapper;
    import com.shyroke.entity.UserBean;
    import com.shyroke.service.UserService;
    
    @Service
    public class UserServiceImpl implements UserService{
    
        @Autowired
        private UserMapper userMapper;
        
        @Override
        @Transactional
        public void save(UserBean user1) {
        
            userMapper.save(user1);
            
            boolean flag = true;
            if (flag) {
            throw new RuntimeException();
            }
            
        }
    
    }
    • mapper

    package com.shyroke.dao;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    
    import com.shyroke.entity.UserBean;
    
    public interface UserMapper extends JpaRepository<UserBean, Integer>{
    
    }
    •   结果:

    数据库没有数据,说明已经被回滚了。

  • 相关阅读:
    移动MM首届手机软件设计及创意大赛决赛取得圆满成功
    Windows Phone 7 EKB系列文章发布
    EVC3/4项目升级到Visual Studio项目的一些建议
    Windows Phone SDK 7.1 RTM 发布
    Howto: 创建Windows Phone 7自定义控件
    风云再起,7迹由你WP7技术沙龙上海站第二次活动
    Windows Phone Dev Notes如何使用ConnectionSettingsTask 来启动连接设置页面
    【OneNote Mobile】 如何处理便签内容的格式?
    《101 Windows Phone 7 Apps》读书笔记PASSWORDS & SECRETS
    3年MVP路,一颗感恩的心
  • 原文地址:https://www.cnblogs.com/shyroke/p/8025018.html
Copyright © 2011-2022 走看看