提交 8906dd9a 编写于 作者: 武汉红喜's avatar 武汉红喜

分库分表demo

上级 6e013bd3
package com.whatsmars.dbrouter.demo;
import com.whatsmars.dbrouter.DbContext;
/**
* Created by shenhongxi on 16/7/16.
*/
......@@ -28,10 +30,12 @@ public class BaseDO {
}
public String getTableIndex() {
return tableIndex;
if (tableIndex != null && !"".equals(tableIndex)) return tableIndex;
return DbContext.getTableIndex();
}
public void setTableIndex(String tableIndex) {
this.tableIndex = tableIndex;
DbContext.setTableIndex(tableIndex);
}
}
package com.whatsmars.dbrouter.demo;
import com.whatsmars.dbrouter.DbContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
......@@ -18,6 +17,5 @@ public class Main {
addOrderReq.setUserId("jd123456789");
addOrderReq.setAmount(new BigDecimal(1000));
orderService.addOrder(addOrderReq);
System.out.println(DbContext.getDbKey() + ":" + DbContext.getTableIndex());
}
}
package com.whatsmars.dbrouter.demo;
import com.whatsmars.dbrouter.DbContext;
import com.whatsmars.dbrouter.DbRoute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -19,6 +20,8 @@ public class OrderService {
order.setUserId(req.getUserId());
order.setOrderId(order.getUserId() + System.currentTimeMillis());
order.setAmount(req.getAmount());
System.out.println(DbContext.getDbKey() + ":" + DbContext.getTableIndex());
order.setTableIndex(DbContext.getTableIndex());
orderDao.insert(order);
}
}
......@@ -7,7 +7,7 @@
</sql>
<select id="insert" parameterType="Order">
insert into order_#tableIndex#(userId,orderId,amount) values(#{userId}, #{orderId}, #{amount})
insert into order_#{tableIndex} (userId,orderId,amount) values (#{userId}, #{orderId}, #{amount})
</select>
</mapper>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册