Skip to content

Commit

Permalink
Transaction工程提交
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwujing committed Jul 12, 2019
1 parent efe6489 commit 9fa2bbb
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 404 deletions.

This file was deleted.

74 changes: 35 additions & 39 deletions springboot-transactional/src/main/java/com/pancm/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
package com.pancm.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.pancm.pojo.User;

/**
*
* @Title: UserDao
* @Description:
* 用户数据接口
* @Version:1.0.0
* @author pancm
* @date 2018年1月9日
*/
@Mapper
public interface UserDao{


@Insert("insert into t_user(id,name,age) values (#{id},#{name},#{age})")
void insert(User user);


@Update("update t_user set name=#{name}, age=#{age} where id=#{id}")
void update(User user);


@Delete("delete from t_user where id=#{id}")
void delete(long id);

@Select("SELECT * FROM t_user where id=#{id}")
User findById(long id);


}
package com.pancm.dao;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.pancm.pojo.User;

import java.sql.SQLException;

/**
*
* @Title: UserDao
* @Description:
* 用户数据接口
* @Version:1.0.0
* @author pancm
* @date 2018年1月9日
*/
@Mapper
public interface UserDao{


@Insert("insert into t_user(id,name,age) values (#{id},#{name},#{age})")
void insert(User user) throws SQLException;

@Update("update t_user set name=#{name}, age=#{age} where id=#{id}")
void update(User user) throws SQLException;

@Select("SELECT * FROM t_user where id=#{id}")
User findById(long id);

}
65 changes: 0 additions & 65 deletions springboot-transactional/src/main/java/com/pancm/pojo/Student.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
package com.pancm.service;

import com.pancm.pojo.User;

/**
*
* @Title: UserService
* @Description:
* 用户接口
* @Version:1.0.0
* @author pancm
* @date 2018年3月19日
*/
public interface UserService {

boolean test1(User user) throws Exception ;

boolean test2(User user);

boolean test3(User user);

boolean test4(User user);

boolean test5(User user);



}
package com.pancm.service;

import com.pancm.pojo.User;

/**
*
* @Title: UserService
* @Description:
* 用户接口
* @Version:1.0.0
* @author pancm
* @date 2018年3月19日
*/
public interface UserService {

boolean test1(User user) throws Exception ;

boolean test2(User user);

boolean test3(User user);

boolean test4(User user);



}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public class UserServiceImpl implements UserService {

@Autowired
private UserDao udao;



@Autowired
private DataSourceTransactionManager dataSourceTransactionManager;
@Autowired
Expand Down Expand Up @@ -83,22 +84,22 @@ public boolean test3(User user) {
deal1(user);
deal2(user);
} catch (Exception e) {
e.printStackTrace();
// 手动回滚事物
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
e.printStackTrace();
}
return false;

}

@Transactional(rollbackFor = Exception.class)
private void deal1(User user) throws Exception {
public void deal1(User user) throws Exception {
udao.insert(user);
System.out.println("查询的数据2:" + udao.findById(user.getId()));
}

@Transactional(rollbackFor = Exception.class)
private void deal2(User user) throws Exception {
public void deal2(User user) throws Exception {
user.setAge(19);
user.setName("xuwujing2");
udao.update(user);
Expand Down Expand Up @@ -126,8 +127,6 @@ public boolean test4(User user) {
}else {
throw new Exception("模拟一个异常!");
}
udao.update(user);
System.out.println("查询的数据4:" + udao.findById(user.getId()));
//手动提交
dataSourceTransactionManager.commit(transactionStatus);
isCommit= true;
Expand Down
Loading

0 comments on commit 9fa2bbb

Please sign in to comment.