ibatis mysql 主从_ibatis对Mysql主从集群进行读写分离测试

ibatis对Mysql主从集群进行读写分离测试。

还是两个数据源:

SqlMapConfigW.xml 写

[html]view plaincopyprint?

/p>

PUBLIC “-//ibatis.apache.org//DTD SQL Map Config 2.0//EN”

“http://ibatis.apache.org/dtd/sql-map-config-2.dtd”>

app server, you probably want to use its transaction manager

and a managed datasource –>

classpath, as they are here (com.domain.data…) –>

SqlMapConfigR.xml 读:

[html]view plaincopyprint?

/p>

PUBLIC “-//ibatis.apache.org//DTD SQL Map Config 2.0//EN”

“http://ibatis.apache.org/dtd/sql-map-config-2.dtd”>

app server, you probably want to use its transaction manager

and a managed datasource –>

classpath, as they are here (com.domain.data…) –>

[java]view plaincopyprint?

City 实体类:

[java]view plaincopyprint?

package com.mydomain.domain;

public class City {

private int id;

private String sname;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getSname() {

return sname;

}

public void setSname(String sname) {

this.sname = sname;

}

}

[java]view plaincopyprint?

City.xml文件:

[html]view plaincopyprint?

/p>

PUBLIC “-//ibatis.apache.org//DTD SQL Map 2.0//EN”

“http://ibatis.apache.org/dtd/sql-map-2.dtd”>

from a query, and the class properties. A result map isn’t

necessary if the columns (or aliases) match to the properties

exactly. –>

select * from City where sName = #sname#

insert into City (

sName

)

values (

#sname#

)

delete from city where id = #id#

测试类

[java]view plaincopyprint?

package com.mydomain.data;

import java.io.IOException;

import java.io.Reader;

import java.sql.SQLException;

import java.util.List;

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import com.mydomain.domain.City;

public class Test {

private static SqlMapClient sqlMapperR; //读

private static SqlMapClient sqlMapperW; //写

public Test(String wr){

if(wr.equals(“W”)){

try {

Reader reader = Resources.getResourceAsReader(“com/mydomain/data/SqlMapConfigW.xml”);

sqlMapperW = SqlMapClientBuilder.buildSqlMapClient(reader);

reader.close();

} catch (IOException e) {

throw new RuntimeException(“Something bad happened while building the SqlMapClient instance.” + e, e);

}

}else{

try {

Reader reader = Resources.getResourceAsReader(“com/mydomain/data/SqlMapConfigR.xml”);

sqlMapperR = SqlMapClientBuilder.buildSqlMapClient(reader);

reader.close();

} catch (IOException e) {

throw new RuntimeException(“Something bad happened while building the SqlMapClient instance.” + e, e);

}

}

}

public static List selectAllCitys (City city) throws SQLException {

return sqlMapperR.queryForList(“selectAllCitys”,city);

}

public static void insertCity (City city) throws SQLException {

sqlMapperW.insert(“insertCity”, city);

}

public static void deleteCity (int id) throws SQLException {

sqlMapperW.delete(“deleteCity”, id);

}

public static void main(String[] args) {

Test t1 = new Test(“R”);

Test t2 = new Test(“W”);

City c = new City();

c.setSname(“你的剑就是我的剑”);

try {

t2.insertCity(c);

List list = t1.selectAllCitys(c);

for (int i = 0; i < list.size(); i++) {

City ci = list.get(i);

System.out.println(“sName:”+ci.getSname());

}

} catch (SQLException e) {

e.printStackTrace();

}

}

}

测试结果:

[plain]view plaincopyprint?

sName:你的剑就是我的剑

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值