Skip to content

Commit

Permalink
add transaction case to mybatis example
Browse files Browse the repository at this point in the history
  • Loading branch information
hanahmily committed Jan 27, 2016
1 parent 433cac8 commit 201f7bc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ public static void main(final String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("META-INF/mybatisContext.xml");
OrderService orderService = applicationContext.getBean(OrderService.class);
orderService.clear();
orderService.add();
orderService.fooService();
orderService.select();

//[order_id: 1, user_id: 10, status: UPDATED, order_id: 1, user_id: 11, status: UPDATED]
orderService.clear();
try{
orderService.addRollback();
orderService.fooServiceWithFailure();
}catch (IllegalArgumentException e){
System.out.println("roll back");
}
//[]
orderService.select();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ public interface OrderRepository {

void insert(Order model);

int update(Order orderId);

int delete(Order orderId);
int update(List<Integer> userIds);

int deleteAll();

Order selectById(Order orderId);

List<Order> selectAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import com.dangdang.ddframe.rdb.sharding.example.jdbc.entity.Order;
import com.dangdang.ddframe.rdb.sharding.example.jdbc.repository.OrderRepository;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -28,20 +29,21 @@ public void clear(){
orderRepository.deleteAll();
}

public void add(){
public void fooService() {
Order criteria = new Order();
criteria.setUserId(10);
criteria.setOrderId(1000);
criteria.setOrderId(1);
criteria.setStatus("INSERT");
orderRepository.insert(criteria);
}

public void addRollback(){
Order criteria = new Order();
criteria.setUserId(12);
criteria.setOrderId(1002);
criteria.setUserId(11);
criteria.setOrderId(1);
criteria.setStatus("INSERT2");
orderRepository.insert(criteria);
orderRepository.update(Lists.newArrayList(10, 11));
}

public void fooServiceWithFailure() {
fooService();
throw new IllegalArgumentException("failed");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,18 @@

<update id="update">
UPDATE `t_order`
SET `status` = #{status,jdbcType=VARCHAR}
WHERE `order_id` = #{orderId} AND `user_id` = #{userId}
SET `status` = 'UPDATED'
WHERE `order_id` = 1 AND `user_id` in
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
</update>

<delete id="delete">
DELETE FROM `t_order`
WHERE `order_id` = #{orderId} AND `user_id` = #{userId}
</delete>

<delete id="deleteAll">
DELETE FROM `t_order`
</delete>

<select id="selectById" resultMap="baseResultMap">
SELECT
<include refid="baseColumnList"/>
FROM `t_order`
WHERE `order_id` = #{orderId} AND `user_id` = #{userId}
</select>

<select id="selectAll" resultMap="baseResultMap">
SELECT
Expand Down

0 comments on commit 201f7bc

Please sign in to comment.