Skip to content

update #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ public void run(String... args) throws Exception {
jdbcTemplate
.queryForObject("SELECT COUNT(*) FROM FOO WHERE BAR='BBB'", Long.class));
}

try {
fooService.invokeInsertThenRollbackByAopContext();
} catch (Exception e) {
log.info("BBB {}",
jdbcTemplate
.queryForObject("SELECT COUNT(*) FROM FOO WHERE BAR='BBB'", Long.class));
}

try {
fooService.invokeInsertThenRollbackBySelfService();
} catch (Exception e) {
log.info("BBB {}",
jdbcTemplate
.queryForObject("SELECT COUNT(*) FROM FOO WHERE BAR='BBB'", Long.class));
}

try {
fooService.invokeInsertThenRollbackAddTransactional();
} catch (Exception e) {
log.info("BBB {}",
jdbcTemplate
.queryForObject("SELECT COUNT(*) FROM FOO WHERE BAR='BBB'", Long.class));
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ public interface FooService {
void insertRecord();
void insertThenRollback() throws RollbackException;
void invokeInsertThenRollback() throws RollbackException;
void invokeInsertThenRollbackByAopContext() throws RollbackException;
void invokeInsertThenRollbackBySelfService() throws RollbackException;
void invokeInsertThenRollbackAddTransactional() throws RollbackException;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package geektime.spring.data.declarativetransactiondemo;

import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -9,6 +11,8 @@
public class FooServiceImpl implements FooService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
private FooServiceImpl self;

@Override
@Transactional
Expand All @@ -23,6 +27,23 @@ public void insertThenRollback() throws RollbackException {
throw new RollbackException();
}

@Override
public void invokeInsertThenRollbackBySelfService() throws RollbackException {
self.insertThenRollback();
}

@Override
public void invokeInsertThenRollbackByAopContext() throws RollbackException {
((FooService) (AopContext.currentProxy())).insertThenRollback();
}

//再加一层事务
@Override
@Transactional(rollbackFor = RollbackException.class)
public void invokeInsertThenRollbackAddTransactional() throws RollbackException {
insertThenRollback();
}

@Override
public void invokeInsertThenRollback() throws RollbackException {
insertThenRollback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@Repository
Expand All @@ -34,6 +35,10 @@ public int getBatchSize() {
});

List<Foo> list = new ArrayList<>();
// list.add(null);
// List<Foo> lista = new LinkedList<>();
// lista.add(null);

list.add(Foo.builder().id(100L).bar("b-100").build());
list.add(Foo.builder().id(101L).bar("b-101").build());
namedParameterJdbcTemplate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Coffee implements Serializable {
private Long id;
private String name;
@Column
@Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmount",
@Type(type = "org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyMinorAmount",
parameters = {@org.hibernate.annotations.Parameter(name = "currencyCode", value = "CNY")})
private Money price;
@Column(updatable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
Expand All @@ -26,7 +20,7 @@
@Builder
public class CoffeeOrder implements Serializable {
@Id
@GeneratedValue
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String customer;
@ManyToMany
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ public static void main(String[] args) {
public void run(ApplicationArguments args) throws Exception {
Coffee c = Coffee.builder().name("espresso")
.price(Money.of(CurrencyUnit.of("CNY"), 20.0)).build();
Long id = coffeeMapper.save(c);
log.info("Coffee {} => {}", id, c);
coffeeMapper.save(c);

c = coffeeMapper.findById(id);
log.info("Coffee {}", c);
log.info("Coffee {} => {}", c.getId(), c);

c = coffeeMapper.findById(c.getId());
log.info("get Coffee 1 {}", c);

Coffee test = Coffee.builder().name("test")
.price(Money.of(CurrencyUnit.of("CNY"), 80.0)).build();
coffeeMapper.save(test);

log.info("Coffee {} => {}", test.getId(), test);

test = coffeeMapper.findById(test.getId());
log.info("get Coffee 2 {}", test);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void main(String[] args) {

@Override
public void run(ApplicationArguments args) throws Exception {
// generateArtifacts();
//generateArtifacts();
playWithArtifacts();
}

Expand Down Expand Up @@ -68,6 +68,7 @@ private void playWithArtifacts() {

CoffeeExample example = new CoffeeExample();
example.createCriteria().andNameEqualTo("latte");
example.setOrderByClause("id desc");
List<Coffee> list = coffeeMapper.selectByExample(example);
list.forEach(e -> log.info("selectByExample: {}", e));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ public interface CoffeeMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
long countByExample(CoffeeExample example);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
int deleteByExample(CoffeeExample example);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
@Delete({
"delete from T_COFFEE",
Expand All @@ -45,7 +45,7 @@ public interface CoffeeMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
@Insert({
"insert into T_COFFEE (NAME, PRICE, ",
Expand All @@ -60,31 +60,31 @@ public interface CoffeeMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
int insertSelective(Coffee record);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
List<Coffee> selectByExampleWithRowbounds(CoffeeExample example, RowBounds rowBounds);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
List<Coffee> selectByExample(CoffeeExample example);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
@Select({
"select",
Expand All @@ -99,31 +99,31 @@ public interface CoffeeMapper {
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
int updateByExampleSelective(@Param("record") Coffee record, @Param("example") CoffeeExample example);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
int updateByExample(@Param("record") Coffee record, @Param("example") CoffeeExample example);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
int updateByPrimaryKeySelective(Coffee record);

/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table T_COFFEE
*
* @mbg.generated Fri Feb 08 00:02:18 CST 2019
* @mbg.generated Fri Apr 26 01:35:42 PST 2019
*/
@Update({
"update T_COFFEE",
Expand Down
Loading