设为首页 收藏本站
查看: 1900|回复: 0

[经验分享] (可以跑通的,mybatis+springmvc+druid+Atomikos)基于druid ,多数据 spring

[复制链接]

尚未签到

发表于 2016-11-28 10:22:28 | 显示全部楼层 |阅读模式
  
DSC0000.png
 

package test.multidatasource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.mifi.service.MifiDeviceService;
import com.mifidb.service.SysMifiUserAssetsService;
@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations = {"classpath:application*.xml"})
public class MultidatasourceTest {
@Autowired
MifiDeviceService service1;
@Autowired
SysMifiUserAssetsService service2;

@Test
public void Test(){
try{
System.out.println(service1.findAllMifiDevices().get(0).getId());
System.out.println(service2.findAllSysMifiUserAssetses().get(0).getId());
}catch(Exception e){
e.printStackTrace();
}
}
}


#druid
druid.initialSize=5
druid.minIdle=5
druid.maxActive=15
druid.maxWait=6000
druid.timeBetweenEvictionRunsMillis=3000
druid.minEvictableIdleTimeMillis=300000
#jpa
jpa.database=org.hibernate.dialect.MySQL5Dialect
#mysql
jdbc.mysql.database=org.hibernate.dialect.MySQL5Dialect
jdbc.mysql.driverClassName=com.mysql.jdbc.Driver
#mifi database
jdbc.mifi.url=jdbc:mysql://localhost:3306/mifi?useUnicode=true&characterEncoding=utf-8
jdbc.mifi.username=root
jdbc.mifi.password=root
#mififlow database
jdbc.mififlow.url=jdbc:mysql://localhost:3306/mififlow?useUnicode=true&characterEncoding=utf-8
jdbc.mififlow.username=root
jdbc.mififlow.password=root
#mifidb database
jdbc.mififlow.url=jdbc:mysql://localhost:3306/mifidb?useUnicode=true&characterEncoding=utf-8
jdbc.mififlow.username=root
jdbc.mififlow.password=root


<?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:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd   
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd   
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd   
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd   
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd   
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd   
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd   
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd   
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd   
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd   
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<context:component-scan base-package="com.mifi" />
<context:component-scan base-package="com.mifidb" />
<context:component-scan base-package="com.mififlow" />
<context:property-placeholder location="classpath:db-config.properties" ignore-unresolvable="true"/>
<!--dataSource mifi -->
<bean id="dataSourceMifi" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="xaDataSourceClassName" value="com.alibaba.druid.pool.xa.DruidXADataSource" />
<property name="uniqueResourceName" value="mifi" />
<property name="xaProperties">
<props>
<prop key="url">jdbc:mysql://localhost:3306/mifi?useUnicode=true&amp;characterEncoding=utf-8</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>

<!--dataSource mififlow -->
<bean id="dataSourceMifiFlow" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="xaDataSourceClassName" value="com.alibaba.druid.pool.xa.DruidXADataSource" />
<property name="uniqueResourceName" value="mififlow" />
<property name="xaProperties">
<props>
<prop key="url">jdbc:mysql://localhost:3306/mifi_flow?useUnicode=true&amp;characterEncoding=utf-8</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
<!--dataSource mifidb -->
<bean id="dataSourceMifiDb" class="com.atomikos.jdbc.AtomikosDataSourceBean"
init-method="init" destroy-method="close">
<property name="xaDataSourceClassName" value="com.alibaba.druid.pool.xa.DruidXADataSource" />
<property name="uniqueResourceName" value="mifidb" />
<property name="xaProperties">
<props>
<prop key="url">jdbc:mysql://localhost:3306/mifi_db?useUnicode=true&amp;characterEncoding=utf-8</prop>
<prop key="username">root</prop>
<prop key="password">root</prop>
</props>
</property>
</bean>
</beans>   

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"
xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd   
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd   
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd   
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd   
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd   
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd   
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd   
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd   
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd   
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd   
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<!-- mifi -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mifi.dao" />
<property name="sqlSessionFactory" ref="sqlSessionFactoryMifi"/>
</bean>
<bean id="sqlSessionFactoryMifi" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceMifi"/>
<property name="mapperLocations" value="classpath*:com/mifi/mapper/*.xml" />
</bean>
<!-- mififlow -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mififlow.dao" />
<property name="sqlSessionFactory" ref="sqlSessionFactoryMifiFlow"/>
</bean>
<bean id="sqlSessionFactoryMifiFlow" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceMifiFlow"/>
<property name="mapperLocations" value="classpath*:com/mififlow/mapper/*.xml" />
</bean>
<!-- mifidb -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.mifidb.dao" />
<property name="sqlSessionFactory" ref="sqlSessionFactoryMifiDb"/>
</bean>
<bean id="sqlSessionFactoryMifiDb" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceMifiDb"/>
<property name="mapperLocations" value="classpath*:com/mifidb/mapper/*.xml" />
</bean>
</beans>   

package com.mifi.bean;
import java.io.Serializable;
import java.sql.Timestamp;

public class MifiDevice implements Serializable{
private static final long serialVersionUID = -2696646267915239503L;
//id,device_mode,device_type,device_mac,device_no,device_code,device_ip,surplus_flow,create_time,modify_time,status
private long id;
private String deviceMode;
private String deviceType;
private String deviceMac;
private String deviceNo;
private String deviceCode;
private String deviceIp;
private long surplusFlow;
private Timestamp createTime;
private Timestamp modifyTime;
private int status;

public long getId() {
return id;
}
public String getDeviceMode() {
return deviceMode;
}
public String getDeviceType() {
return deviceType;
}
public String getDeviceMac() {
return deviceMac;
}
public String getDeviceNo() {
return deviceNo;
}
public String getDeviceCode() {
return deviceCode;
}
public String getDeviceIp() {
return deviceIp;
}
public long getSurplusFlow() {
return surplusFlow;
}
public Timestamp getCreateTime() {
return  createTime;
}
public Timestamp getModifyTime() {
return modifyTime;
}
public int getStatus() {
return status;
}
public void setId(long id) {
this.id = id;
}
public void setDeviceMode(String deviceMode) {
this.deviceMode = deviceMode;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public void setDeviceMac(String deviceMac) {
this.deviceMac = deviceMac;
}
public void setDeviceNo(String deviceNo) {
this.deviceNo = deviceNo;
}
public void setDeviceCode(String deviceCode) {
this.deviceCode = deviceCode;
}
public void setDeviceIp(String deviceIp) {
this.deviceIp = deviceIp;
}
public void setSurplusFlow(long surplusFlow) {
this.surplusFlow = surplusFlow;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
public void setModifyTime(Timestamp modifyTime) {
this.modifyTime = modifyTime;
}
public void setStatus(int status) {
this.status = status;
}
}


package com.mifi.dao;
import java.util.List;
import com.mifi.bean.MifiDevice;

public interface MifiDeviceDao {
public  MifiDevice  getMifiDeviceById(long id);
public List<MifiDevice> findAllMifiDevices();
public void addMifiDevice(MifiDevice mifiDevice);
public void delMifiDeviceById(long id);
public void updateMifiDevice(MifiDevice mifiDevice);
}


<?xml version="1.0" encoding="UTF-8"?>   
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.mifi.dao.MifiDeviceDao">
<!-- id,device_mode,device_type,device_mac,device_no,device_code,device_ip,surplus_flow,create_time,modify_time,status -->
<!-- Result Map-->  
<resultMap type="com.mifi.bean.MifiDevice" id="mifiDevice">   
<result property="id" column="id"   javaType="long" jdbcType="INTEGER" />  
<result property="deviceMode" column="device_mode" javaType="string" jdbcType="VARCHAR"/>  
<result property="deviceType" column="device_type" javaType="string" jdbcType="VARCHAR"/>  
<result property="deviceMac" column="device_mac" javaType="string" jdbcType="VARCHAR"/>  
<result property="deviceNo" column="device_no" javaType="string" jdbcType="VARCHAR" />  
<result property="deviceCode" column="device_code" javaType="string" jdbcType="VARCHAR"  />  
<result property="deviceIp" column="device_ip" javaType="string" jdbcType="VARCHAR" />  
<result property="surplusFlow" column="surplus_flow"   javaType="long" jdbcType="INTEGER" />  
<result property="createTime" column="create_time"  jdbcType="TIMESTAMP"   javaType="java.sql.Timestamp" />  
<result property="modifyTime" column="modify_time"   jdbcType="TIMESTAMP"   javaType="java.sql.Timestamp" />  
<result property="status" column="status"    javaType="int" jdbcType="INTEGER" />   
</resultMap>  

<select id="getMifiDeviceById" resultMap="mifiDevice" parameterType="java.lang.Long">
select * from mifi_device where id = #{id,jdbcType=INTEGER}
</select>
<select id="findAllMifiDevices" resultMap="mifiDevice">
select * from mifi_device
</select>
<insert id="addMifiDevice" parameterType="com.mifi.bean.MifiDevice">
INSERT INTO mifi_device
(id,device_mode,device_type,device_mac,device_no,device_code,device_ip,surplus_flow,create_time,modify_time,status)
VALUE
(null,#{deviceMode},#{deviceType},#{deviceMac},#{deviceNo},#{deviceCode},#{deviceIp},#{surplusFlow},#{createTime},#{modifyTime},#{status})
</insert>
<delete id="delMifiDeviceById" parameterType="java.lang.Long">
delete from mifi_device where id = #{id}
</delete>
<update id="updateMifiDevice" parameterType="com.mifi.bean.MifiDevice">
update mifi_device
<set>
<if test="deviceMode != null ">device_mode = #{deviceMode},</if>
<if test="deviceType != null ">device_type = #{deviceType},</if>
<if test="deviceMac != null ">device_mac = #{deviceMac},</if>
<if test="deviceNo != null ">device_no = #{deviceNo},</if>
<if test="deviceCode != null ">device_code = #{deviceCode},</if>
<if test="deviceIp != null ">device_ip = #{deviceIp},</if>
<if test="surplusFlow != null ">surplus_flow = #{surplusFlow,jdbcType=INTEGER},</if>
<if test="createTime != null ">create_time = #{createTime,jdbcType=TIMESTAMP},</if>
<if test="modifyTime != null ">modify_time = #{modifyTime,jdbcType=TIMESTAMP},</if>
<if test="status != null ">status = #{status,jdbcType=INTEGER}</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>   


package com.mifi.service;
import java.util.List;
import com.mifi.bean.MifiDevice;

public interface MifiDeviceService {
public  boolean  hasMifiDeviceById(long id);
public  MifiDevice  getMifiDeviceById(long id);
public List<MifiDevice> findAllMifiDevices();
public void addMifiDevice(MifiDevice mifiDevice);
public void delMifiDeviceById(long id);
public void updateMifiDevice(MifiDevice mifiDevice);
}


package com.mifi.serviceimpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mifi.bean.MifiDevice;
import com.mifi.dao.MifiDeviceDao;
import com.mifi.service.MifiDeviceService;
import com.netty.service.NettyServerService;
@NettyServerService(name="mifiDeviceService")
@Service
public class MifiDeviceServiceImpl implements MifiDeviceService {
@Autowired
MifiDeviceDao dao;
public  boolean  hasMifiDeviceById(long id){
return dao.getMifiDeviceById(id) == null ? false : true ;
}
public  MifiDevice  getMifiDeviceById(long id){
return dao.getMifiDeviceById(id);
}
public List<MifiDevice> findAllMifiDevices(){
return dao.findAllMifiDevices();
}
public void addMifiDevice(MifiDevice mifiDevice){
dao.addMifiDevice(mifiDevice);
}
public void delMifiDeviceById(long id){
dao.delMifiDeviceById(id);
}
public void updateMifiDevice(MifiDevice mifiDevice){
dao.updateMifiDevice(mifiDevice);
}
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-306544-1-1.html 上篇帖子: SpringMVC+MyBatis 下篇帖子: Spring 3.1.1 + mybatis 3.1.0 + struts2.3.1.2
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表