(可以跑通的,mybatis+springmvc+druid+Atomikos)基于druid ,多数据 spring
程序员文章站
2022-07-11 18:38:48
...
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&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&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&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); } }
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
上一篇: 能帮助化食的水果有哪些
下一篇: 女人吃什么对乳房好