ibatis对Mysql主从集群进行读写分离测试
来源:互联网 发布:单片机软件调试 编辑:程序博客网 时间:2024/04/30 23:15
还是两个数据源:
SqlMapConfigW.xml 写
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"><sqlMapConfig> <!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource --> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/> <property name="JDBC.ConnectionURL" value="jdbc:mysql:loadbalance://10.11.2.126:3306/DB_TEST7?roundRobinLoadBalance=true&characterEncoding=UTF-8"/> <property name="JDBC.Username" value="TESTUSER"/> <property name="JDBC.Password" value="TESTPWD"/> </dataSource> </transactionManager> <!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) --> <sqlMap resource="com/mydomain/data/City.xml"/></sqlMapConfig>
SqlMapConfigR.xml 读:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"><sqlMapConfig> <!-- Configure a built-in transaction manager. If you're using an app server, you probably want to use its transaction manager and a managed datasource --> <transactionManager type="JDBC" commitRequired="false"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="com.mysql.jdbc.Driver"/> <property name="JDBC.ConnectionURL" value="jdbc:mysql:loadbalance://10.11.0.75,172.16.0.202:3306/DB_TEST7?roundRobinLoadBalance=true&characterEncoding=UTF-8"/> <property name="JDBC.Username" value="TESTUSER"/> <property name="JDBC.Password" value="TESTPWD"/> </dataSource> </transactionManager> <!-- List the SQL Map XML files. They can be loaded from the classpath, as they are here (com.domain.data...) --> <sqlMap resource="com/mydomain/data/City.xml"/></sqlMapConfig>
City 实体类:
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;}}
City.xml文件:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"><sqlMap namespace="City"> <!-- Use type aliases to avoid typing the full classname every time. --> <typeAlias alias="City" type="com.mydomain.domain.City"/> <!-- Result maps describe the mapping between the columns returned from a query, and the class properties. A result map isn't necessary if the columns (or aliases) match to the properties exactly. --> <resultMap id="CityResult" class="City"> <result property="id" column="ID"/> <result property="sname" column="SNAME"/> </resultMap> <!-- Select with no parameters using the result map for Account class. --> <select id="selectAllCitys" resultMap="CityResult" parameterClass="City"> select * from City where sName = #sname# </select> <!-- Insert example, using the Account parameter class --> <insert id="insertCity" parameterClass="City"> insert into City ( sName ) values ( #sname# ) </insert> <!-- Delete example, using an integer as the parameter class --> <delete id="deleteCity" parameterClass="int"> delete from city where id = #id# </delete></sqlMap>
测试类
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<City> 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();}}}
测试结果:
sName:你的剑就是我的剑
- ibatis对Mysql主从集群进行读写分离测试
- mysql集群技术:主从复制,读写分离
- mysql主从读写分离
- MySQL主从同步读写分离的集群配置
- Mysql 分布式集群 主从同步 读写分离 amoeba 中间件配置
- JDBC访问Mysql进行读写分离测试
- JDBC进行Mysql读写分离测试
- Spring+Hibernate进行双数据源测试Mysql集群读写分离
- shopNC中mysql配置主从进行读写分离的坑
- mysql集群--读写分离
- Redhat7.0系统利用amoeba对mysql数据进行读写分离的操作,MySQL数据库的主从配置
- MySQL主从复制架构搭建及读写分离测试
- MySQL主从复制架构搭建及读写分离测试
- MySQL主从复制 读写分离
- Mysql 主从复制,读写分离
- mysql主从+读写分离实现
- 关于mysql主从读写分离
- mysql 主从amoeba读写分离
- js的小常识语法
- NET使用NPOI组件将数据导出Excel
- Linux 监测IO状态
- 向flash传参
- (续)一种简陋的CC1100/CC1101主从通信协议
- ibatis对Mysql主从集群进行读写分离测试
- delphi - bpl与dll之关系
- ubuntu 12.04 安装 Nginx+PHP5 (PHP-FPM) 配置CI项目
- int main(int argc,char*argv[])的输入参数
- JNI和Qt通信 (Part 3)
- Windows Server 2003 注册码
- Delphi BPL包合并图文教程 IDEWiz tangram-plugin-framework
- Linux 常用命令
- GPIO宏定义一