corbarclient 的简单案例
来源:互联网 发布:时间胶囊淘宝 编辑:程序博客网 时间:2024/05/16 05:10
版权所有,转载请标明出处
---------------------
1、创建一个maven项目, 将cobar-client的项目引到该项目中,dao层基于ibaits 与spring集成的(与单个数据的完全一样),
2、beans.xml 文件 :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName">
<context:component-scan base-package="com.uz.dao,com.uz.service"></context:component-scan>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="partition1_main"></property>
<property name="configLocation"
value="classpath:sqlMapConfig.xml">
</property>
</bean>
<bean id="sqlMapClientTemplate" class="com.alibaba.cobar.client.CobarSqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient" />
<property name="cobarDataSourceService" ref="dataSources" />
<!-- 路由规则 -->
<property name="router" ref="internalRouter"></property>
<!-- <property name="sqlAuditor">
<bean class="com.alibaba.cobar.client.audit.SimpleSqlAuditor" />
</property> -->
<property name="profileLongTimeRunningSql" value="true" />
<property name="longTimeRunningSqlIntervalThreshold" value="3600000" />
</bean>
<bean id="internalRouter"
class="com.alibaba.cobar.client.router.config.CobarInteralRouterXmlFactoryBean">
<!-- <property name="functionsMap">
<map>
<entry key="mod">
<bean class="com.alibaba.cobar.client.router.rules.support.ModFunction">
<constructor-arg><value>128</value></constructor-arg>
</bean>
</entry>
</map>
</property> -->
<property name="configLocations">
<list>
<value>classpath:rules.xml</value>
</list>
</property>
</bean>
<bean id="dataSources" class="com.alibaba.cobar.client.datasources.DefaultCobarDataSourceService">
<property name="dataSourceDescriptors">
<set>
<bean class="com.alibaba.cobar.client.datasources.CobarDataSourceDescriptor">
<property name="identity" value="partition1"/> //数据源描述的id,是rule唯一表示
<property name="targetDataSource" ref="partition1_main"/>
<property name="targetDetectorDataSource" ref="partition1_main"/>
<property name="standbyDataSource" ref="partition1_standby"/>
<property name="standbyDetectorDataSource" ref="partition1_standby"/>
<property name="poolSize" value="10"/>
</bean>
<bean class="com.alibaba.cobar.client.datasources.CobarDataSourceDescriptor">
<property name="identity" value="partition2"/>
<property name="targetDataSource" ref="partition2_main"/>
<property name="targetDetectorDataSource" ref="partition2_main"/>
<property name="standbyDataSource" ref="partition2_standby"/>
<property name="standbyDetectorDataSource" ref="partition2_standby"/>
</bean>
</set>
</property>
<property name="haDataSourceCreator">
<bean class="com.alibaba.cobar.client.datasources.ha.FailoverHotSwapDataSourceCreator">
<property name="detectingSql" value="update cobarha set timeflag=CURRENT_TIMESTAMP()"/>
</bean>
</property>
</bean>
<bean id="transactionManager"
class="com.alibaba.cobar.client.transaction.MultipleDataSourcesTransactionManager">
<property name="cobarDataSourceService" ref="dataSources" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allMethod"
expression="execution(* *..*service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod" />
</aop:config>
//引入数据源配置文件
<import resource="dateSource.xml"/>
</beans>
3、数据源配置文件:(两个数据库cobar1和cobar2)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" default-autowire="byName">
<bean id="partition1_main" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cobar1"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="2" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>
<bean id="partition1_standby" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cobar1"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="2" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>
<bean id="partition2_main" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cobar2"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="2" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>
<bean id="partition2_standby" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/cobar2"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="2" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>
</beans>4、选择数据源的路由规则配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<rules>
<rule>
<sqlmap>save</sqlmap>
<shardingExpression>stu_age % 2 == 0 </shardingExpression>
<shards>partition2</shards>
</rule>
<rule>
<sqlmap>save</sqlmap>
<shardingExpression>stu_age % 2 == 1 </shardingExpression>
<shards>partition1</shards>
</rule>
<rule>
<sqlmap>queryAll</sqlmap>
<!-- <shardingExpression>stu_age % 2 == 1 </shardingExpression> -->
<shards>partition2</shards>
</rule>
</rules>
5、ibatis的配置文件:
<?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>
<sqlMap resource="com/uz/domain/Student.xml" />
</sqlMapConfig>6、测试类:
package test.service;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.uz.domain.Student;
import com.uz.service.StudentService;
public class StudentServiceTest {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
/*StudentService bean = (StudentService) ac.getBean("studentService");
Student s= new Student();
s.setStu_age(24);
s.setStu_birth(new Date());
s.setStu_name("xiaoming");
s.setStu_score(23f);
bean.save(s);*/
StudentService bean = (StudentService) ac.getBean("studentService");
List<Student> list = bean.queryAll();
System.out.println(list.size());
for(Student s : list){
System.out.println(s.toString());
}
System.out.println("--------ok");
}
}
7、student类 和对应的sql语句:
public class Student {
private Integer stu_id;
private String stu_name;
private Integer stu_age;
private float stu_score;
private Date stu_birth;}
<?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="student">
<typeAlias alias="Student" type="com.uz.domain.Student" />
<insert id="save" parameterClass="Student">
insert into student
values(#stu_id#,#stu_name#,#stu_age#,#stu_score#,#stu_birth#)
</insert>
<delete id="delStuByID" parameterClass="int">
delete from student where stu_id = #stu_id#
</delete>
<update id="update" parameterClass="Student">
update student set
stu_name=#stu_name#,stu_age=#stu_age#,stu_score=#stu_score#,stu_birth=#stu_birth#
where stu_id=#stu_id#
</update>
<select id="queryAll" resultClass="Student">
select * from student
</select>
<select id="queryById" resultClass="Student" parameterClass="int">
select * from student where stu_id = #stu_id#
</select>
8、dao 层接口和具体子类:
public interface StudentDao {
public void save(Student stu);
public void delete(int stu_id);
public void update(Student stu);
public Student queryByPK(int stu_id);
public List<Student> queryAll();
} @Repository("stuDao")
public class StudentDaoImple implements StudentDao {
@Autowired
private SqlMapClientTemplate sqlMapClientTemplate;
public void delete(int stu_id) {
sqlMapClientTemplate.delete("delStuByID", stu_id);
}
public List<Student> queryAll() {
return sqlMapClientTemplate.queryForList("queryAll");
}
public Student queryByPK(int stu_id) {
return (Student) sqlMapClientTemplate.queryForObject("queryById",
stu_id);
}
public void save(Student stu) {
sqlMapClientTemplate.insert("save", stu);
}
public void update(Student stu) {
sqlMapClientTemplate.update("update", stu);
}
} 9、service层的接口和具体子类:
public interface StudentService {
public void save(Student stu);
public void delete(int stu_id);
public void update(Student stu);
public Student queryByPK(int stu_id);
public List<Student> queryAll();
} @Service("studentService")
public class StudentServiceImple implements StudentService {
@Autowired
private StudentDao stuDao;
public void setStudao(StudentDao studao) {
this.stuDao = studao;
}
public void delete(int stu_id) {
this.stuDao.delete(stu_id);
}
public List<Student> queryAll() {
return stuDao.queryAll();
}
public Student queryByPK(int stu_id) {
return stuDao.queryByPK(stu_id);
}
public void save(Student stu) {
stuDao.save(stu);
}
public void update(Student stu) {
stuDao.update(stu);
}
}
- corbarclient 的简单案例
- corbarclient 多数据源的路由配置
- jdbc的简单案例
- activeMQ的简单案例
- 简单的Intent案例
- cglib的简单案例
- 简单的struts2案例
- echarts的简单案例
- freemarker的简单案例
- corbarclient支持spring3.X
- 批处理的简单的案例
- Applet的简单使用案例
- 一个简单的FreeMarker案例
- BaseAdapter的一个简单案例
- C#简单的委托案例
- iBaties的简单入门案例
- 简单的CardLayout布局案例
- hashmap使用的简单案例
- 期末,Android本学期学习总结
- VMware 8安装苹果操作系统Mac OS X 10.7 Lion正式版
- TINY210 UBOOT(一)启动过程
- JavaScript 32位整型无符号操作
- java可中断的线程
- corbarclient 的简单案例
- VIM的使用技巧(常用)
- JAVA抓取网页的简单实现
- 【Unity3D】【NGUI】UIProgressBar
- 使CBIntrospect支持在设备上使用
- JSP页面分页技术实现
- 孙氏太极拳体悟——孙瑜
- 软件公司的信息化建设(引子)
- 智力测试