1. schema.xml 配置
2. serverl.xml 配置
3.事务测试
mysql 客户端连接 mycat 命令 :
mysql -u mycat_god -p -P 8066 -h 10.20.101.105
退出:
quit
mycat中创建表:
-- 商品表 :
CREATE TABLE `tb_item` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '商品id,同时也是商品编号',
`title` varchar(100) NOT NULL COMMENT '商品标题',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='商品表';
-- 商品描述表 :
CREATE TABLE `tb_item_desc` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`item_id` bigint(20) NOT NULL COMMENT '商品ID',
`item_desc` text COMMENT '商品描述',
`created` datetime DEFAULT NULL COMMENT '创建时间',
`updated` datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='商品描述表';
-- 连接 mycat 生成 mybatis 的 dao
java 连接mycat 配置 :
db.driverClass=com.mysql.jdbc.Driver
db.url=jdbc:mysql://10.20.101.105:8066/shenyi?useUnicode=true&characterEncoding=UTF-8
db.username=mycat_god
db.password=123
db.maxPoolSize=30
db.minPoolSize=10
db.checkoutTimeout=100000000
db.acquireRetryAttempts=2
db.initialSize=5
mybatis.config=classpath:META-INF/mybatis-config.xml
事务测试:
@Transactional(rollbackFor = Exception.class)
@Override public void insertItem(TbItem item, String Desc) throws Exception {
int count= tbItemMapper.insert(item);
Long id=null;
if(count>0){
id=item.getId();
TbItemDesc tbItemDesc=new TbItemDesc();
tbItemDesc.setCreated(new Date());
tbItemDesc.setUpdated(new Date());
tbItemDesc.setItemId(id);
tbItemDesc.setItemDesc(Desc);
int c=tbItemDescMapper.insert(tbItemDesc);
if(c<=0){
throw new Exception("商品保存失败");
}
}
}
@RunWith(value = SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = {"classpath*:spring/spring-push-conf.xml","classpath*:spring/spring-order-conf.xml",
"classpath*:spring/spring-service-conf.xml","classpath*:spring/spring-dao-conf.xml"})
public class TbItemServiceTest {
@Autowired
private TbItemService tbItemService;
@Test
public void addProduct() throws Exception{
TbItem tbItem=new TbItem();
tbItem.setTitle("java mysql读写分离第二版");
tbItemService.insertItem(tbItem,"果然强悍");
}
事务测试成功!