@Transactional comment rollback

1. Create an entity class with the properties owned by the database fields correspondence

import lombok.Data;
@Data
public class InterfaceTest {
private Integer interfaceId;
private String interfaceName;
private String interfaceType;
private String interfaceMethod;
private String interfaceAliasName;

}

2 database tables have fields Here Insert Picture Description
create an interface: write CRUD interface method, write new method

import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface MyInterfaceTestMapper {
void addInterfaceTest(InterfaceTest interfaceTest);
}

Create a xml file: Write CRUD sql file, write new sql

<mapper namespace="com.longteng.lesson2.my.mybatis.MyInterfaceTestMapper">
    <!--id就是接口中的方法名-->
    <insert id="addInterfaceTest"
            useGeneratedKeys="true"
            keyColumn="interface_test_id"
            keyProperty="interfaceId"
            parameterType="com.longteng.lesson2.my.mybatis.InterfaceTest">
            <!--给哪个表插入数据,就写哪个表-->
            insert into interface_test
            <!--给哪些字段插入数据,就写哪些字段名-->
            (interface_name,interface_method,interface_type,interface_alias_name)
            <!--要插入的数据是对应的哪些字段,就是实体类里的字段,要与字段名的先后顺序一一对应,用#{}接收-->
            values (#{interfaceName},#{interfaceMethod},#{interfaceType},#{interfaceAliasName})
    </insert>

Things: things were rolled back by @Transactional

Put together to perform multiple operations called things
such as a lower order of business
was first performed is inserted into the database, but the database is inserted after the successful follow-up business did not succeed, the next order business has not completely finished, when in fact this is already an order fails, then insert the data of the piece of data is dirty data, data thing to do is to do a rollback operation is not successful
things into program management things, things management statement, the statement is an example of something implemented in
programming things: control their own rollback process, used for complex systems
declare things: @Transactional roll back annotation flow control, and more for a simple process
Import com.longteng.lesson2.my.mybatis.InterfaceTest;
Import com.longteng.lesson2.my.mybatis.MyInterfaceTestMapper ;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Service;
Import org.springframework.transaction.annotation.Transactional;
@Service
public class InterfaceService {
// injection interfaces
@Autowired
MyInterfaceTestMapper myInterfaceTest Mapper;
// abnormal rollback
@Transactional (rollbackFor = Exception.class)
// return type Object method with parameters, an exception is thrown
public Object Test (Integer I) throws Exception {
// create an instance of the entity class, adding data to a database by way of example
InterfaceTest interfaceTest InterfaceTest new new = ();
interfaceTest.setInterfaceName ( "qwer");
; ( "the GET") interfaceTest.setInterfaceMethod
interfaceTest.setInterfaceType ( "the HTTP");
interfaceTest.setInterfaceAliasName ( "qWER");
// call interface interface class instance new methods to add data to the database
myInterfaceTestMapper.addInterfaceTest (interfaceTest);
// if i is not equal to the reference transfer methods throw an exception, if the return to normal is equal to 1, adding success
IF (! i = 1) {
the throw new new exception ();
} the else {
return "added successfully";
}
}
}

controller in

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyInterfaceController {
//注入回滚的业务类
@Autowired
InterfaceService interfaceService;
@RequestMapping("/interfaceService")
public @ResponseBody Object test(Integer i)throws Exception{
    return interfaceService.test(i);
}
}

Browser requests http://127.0.0.1:8080/interfaceService?i=1 I add data to the database. 1 = Success

Here Insert Picture Description
Browser requests http://127.0.0.1:8080/interfaceService?i=2 I = 2 failed to add data to the database

If you do not @Transactional, when even request i is not equal to 1 into the database can be successful
example, the call addInterfaceTest method is actually inserted into the database operations, and debug words, sql indeed printed, but this time was not inserted into the database, because with the rollback notes, the whole thing was a submission, where not read the code where to perform, and so is the entire test method is finished before deciding whether or not to insert the sql database

Guess you like

Origin blog.csdn.net/qq_41767337/article/details/89397191