[Spring Boot learning four] Spring Boot transaction management

Environment
  the Eclipse 4.7
  the JDK 1.8
  the Spring the Boot 1.5.2

A, springboot integration affairs
affairs Category: Programming affairs, declarative transaction (XML, annotations), the recommended way to use annotations, springboot default integrated thing, only in the main method with the @Transactional to
1, controller

package com.wjy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wjy.test1.service.UserServiceTest1;


@RestController
public class UserController {

    @Autowired
    public UserServiceTest1 userServiceTest1;
    
    @RequestMapping("/insertTest1ByService")
    public String insertTest1ByService(String name,Integer age) {
        userServiceTest1.insertuser1(name, age);
        return "success";
    }
    
    
}

2、service

/ ** 
 * 
 * / 
Package com.wjy.test1.service; 

Import org.springframework.beans.factory.annotation.Autowired;
 Import org.springframework.stereotype.Service;
 Import org.springframework.transaction.annotation.Transactional; 

Import COM .wjy.test1.dao.UserMapperTest1; 

/ ** 
 * @Desc 
 * @author wangjy15
  * / 
@Service 
public  class UserServiceTest1 { 
    
    @Autowired 
    Private userMapperTest1 userMapperTest1; 
    
    / ** 
     * @Description: If no transaction control then inserted into the database after error where the data will not be rolled back annotate to roll back @Transactional
     */
    @Transactional
    public String insertuser1(String name,Integer age) {
        userMapperTest1.insert(name, age);
        int i =1/0;
        return "success";
    }

}

 

3、mapper

/**
 * 
 */
package com.wjy.test1.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.wjy.entity.User;

/**
 * @Desc
 * @author wangjy15
 */
public interface UserMapperTest1 {
    
    @Select("SELECT * FROM users WHERE NAME = #{name}")
    User findByName(@Param("name") String name);

    @Insert("insert into users (name,age) values(#{name},#{age})")
    int insert(@Param("name") String name,@Param("age") Integer age);
}

 

4、APP

package com.wjy;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class APP {

    public static void main(String[] args) {
        SpringApplication.run(APP.class, args);
    }

}

5, testing and certification

http://localhost:8080/insertTest1ByService?name=wangsan0010&age=1000

 

Two, SpringBoot distributed transaction management
of traditional projects: jta + automatic

 

Guess you like

Origin www.cnblogs.com/cac2020/p/11230967.html