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

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

还是两个数据源:

SqlMapConfigW.xml 写

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <!DOCTYPE sqlMapConfig        
  4.     PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"        
  5.     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">  
  6.   
  7. <sqlMapConfig>  
  8.   
  9.   <!-- Configure a built-in transaction manager.  If you're using an   
  10.        app server, you probably want to use its transaction manager   
  11.        and a managed datasource -->  
  12.   <transactionManager type="JDBC" commitRequired="false">  
  13.     <dataSource type="SIMPLE">  
  14.       <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>  
  15.       <property name="JDBC.ConnectionURL" value="jdbc:mysql:loadbalance://10.11.2.126:3306/DB_TEST7?roundRobinLoadBalance=true&characterEncoding=UTF-8"/>  
  16.       <property name="JDBC.Username" value="TESTUSER"/>  
  17.       <property name="JDBC.Password" value="TESTPWD"/>  
  18.     </dataSource>  
  19.   </transactionManager>  
  20.   
  21.   <!-- List the SQL Map XML files. They can be loaded from the   
  22.        classpath, as they are here (com.domain.data...) -->  
  23.   <sqlMap resource="com/mydomain/data/City.xml"/>  
  24.   
  25. </sqlMapConfig>  


SqlMapConfigR.xml 读:

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <!DOCTYPE sqlMapConfig        
  4.     PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"        
  5.     "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">  
  6.   
  7. <sqlMapConfig>  
  8.   
  9.   <!-- Configure a built-in transaction manager.  If you're using an   
  10.        app server, you probably want to use its transaction manager   
  11.        and a managed datasource -->  
  12.   <transactionManager type="JDBC" commitRequired="false">  
  13.     <dataSource type="SIMPLE">  
  14.       <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/>  
  15.       <property name="JDBC.ConnectionURL" value="jdbc:mysql:loadbalance://10.11.0.75,172.16.0.202:3306/DB_TEST7?roundRobinLoadBalance=true&characterEncoding=UTF-8"/>  
  16.       <property name="JDBC.Username" value="TESTUSER"/>  
  17.       <property name="JDBC.Password" value="TESTPWD"/>  
  18.     </dataSource>  
  19.   </transactionManager>  
  20.   
  21.   <!-- List the SQL Map XML files. They can be loaded from the   
  22.        classpath, as they are here (com.domain.data...) -->  
  23.   <sqlMap resource="com/mydomain/data/City.xml"/>  
  24.   
  25. </sqlMapConfig>  
[java] view plaincopyprint?
  1. City 实体类:  
[java] view plaincopyprint?
  1. package com.mydomain.domain;  
  2.   
  3. public class City {  
  4.     private int id;  
  5.     private String sname;  
  6.     public int getId() {  
  7.         return id;  
  8.     }  
  9.     public void setId(int id) {  
  10.         this.id = id;  
  11.     }  
  12.     public String getSname() {  
  13.         return sname;  
  14.     }  
  15.     public void setSname(String sname) {  
  16.         this.sname = sname;  
  17.     }  
  18.       
  19.       
  20. }  

 

[java] view plaincopyprint?
  1. City.xml文件:  
[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <!DOCTYPE sqlMap        
  4.     PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"        
  5.     "http://ibatis.apache.org/dtd/sql-map-2.dtd">  
  6.   
  7. <sqlMap namespace="City">  
  8.   
  9.   <!-- Use type aliases to avoid typing the full classname every time. -->  
  10.   <typeAlias alias="City" type="com.mydomain.domain.City"/>  
  11.   
  12.   <!-- Result maps describe the mapping between the columns returned  
  13.        from a query, and the class properties.  A result map isn't  
  14.        necessary if the columns (or aliases) match to the properties   
  15.        exactly. -->  
  16.   <resultMap id="CityResult" class="City">  
  17.     <result property="id" column="ID"/>  
  18.     <result property="sname" column="SNAME"/>  
  19.   </resultMap>  
  20.   
  21.   <!-- Select with no parameters using the result map for Account class. -->  
  22.   <select id="selectAllCitys" resultMap="CityResult" parameterClass="City">  
  23.     select * from City where sName = #sname#  
  24.   </select>  
  25.   
  26.      
  27.   <!-- Insert example, using the Account parameter class -->  
  28.   <insert id="insertCity" parameterClass="City">  
  29.     insert into City (  
  30.       sName  
  31.      )  
  32.     values (  
  33.       #sname#  
  34.     )  
  35.   </insert>  
  36.   
  37.   
  38.   <!-- Delete example, using an integer as the parameter class -->  
  39.   <delete id="deleteCity" parameterClass="int">  
  40.     delete from city where id = #id#  
  41.   </delete>  
  42.   
  43. </sqlMap>  


测试类

[java] view plaincopyprint?
  1. package com.mydomain.data;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.Reader;  
  5. import java.sql.SQLException;  
  6. import java.util.List;  
  7.   
  8. import com.ibatis.common.resources.Resources;  
  9. import com.ibatis.sqlmap.client.SqlMapClient;  
  10. import com.ibatis.sqlmap.client.SqlMapClientBuilder;  
  11. import com.mydomain.domain.City;  
  12.   
  13. public class Test {  
  14.   
  15.     private static SqlMapClient sqlMapperR; //读  
  16.     private static SqlMapClient sqlMapperW; //写  
  17.     public Test(String wr){  
  18.         if(wr.equals("W")){  
  19.             try {  
  20.                 Reader reader = Resources.getResourceAsReader("com/mydomain/data/SqlMapConfigW.xml");  
  21.                 sqlMapperW = SqlMapClientBuilder.buildSqlMapClient(reader);  
  22.                 reader.close();   
  23.             } catch (IOException e) {  
  24.                 throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);  
  25.             }  
  26.         }else{  
  27.             try {  
  28.                 Reader reader = Resources.getResourceAsReader("com/mydomain/data/SqlMapConfigR.xml");  
  29.                 sqlMapperR = SqlMapClientBuilder.buildSqlMapClient(reader);  
  30.                 reader.close();   
  31.             } catch (IOException e) {  
  32.                 throw new RuntimeException("Something bad happened while building the SqlMapClient instance." + e, e);  
  33.             }  
  34.         }  
  35.     }  
  36.       
  37.     public static List selectAllCitys (City city) throws SQLException {  
  38.         return sqlMapperR.queryForList("selectAllCitys",city);  
  39.     }  
  40.     public static void insertCity (City city) throws SQLException {  
  41.         sqlMapperW.insert("insertCity", city);  
  42.     }  
  43.   
  44.     public static void deleteCity (int id) throws SQLException {  
  45.         sqlMapperW.delete("deleteCity", id);  
  46.     }    
  47.         
  48.     public static void main(String[] args) {  
  49.         Test t1 = new Test("R");  
  50.         Test t2 = new Test("W");  
  51.         City c = new City();  
  52.         c.setSname("你的剑就是我的剑");  
  53.         try {    
  54.             t2.insertCity(c);  
  55.             List<City> list = t1.selectAllCitys(c);    
  56.             for (int i = 0; i < list.size(); i++) {  
  57.                 City ci = list.get(i);  
  58.                 System.out.println("sName:"+ci.getSname());  
  59.             }  
  60.         } catch (SQLException e) {  
  61.             e.printStackTrace();  
  62.         }  
  63.     }  
  64.   
  65. }  


测试结果:

[plain] view plaincopyprint?
  1. sName:你的剑就是我的剑