(可以跑通的,mybatis+springmvc+druid+Atomikos)基于druid ,多数据 spring

简介:     package test.multidatasource; import org.junit.Test; import org.junit.runner.RunWith; import org.



 

 

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);
	 }
	
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。



 
 
 谢谢您的赞助,我会做的更好!

 

 

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
|
Java 数据库连接 数据库
mybatis查询数据,返回的对象少了一个字段
mybatis查询数据,返回的对象少了一个字段
1701 9
|
SQL Java 数据库连接
对Spring、SpringMVC、MyBatis框架的介绍与解释
Spring 框架提供了全面的基础设施支持,Spring MVC 专注于 Web 层的开发,而 MyBatis 则是一个高效的持久层框架。这三个框架结合使用,可以显著提升 Java 企业级应用的开发效率和质量。通过理解它们的核心特性和使用方法,开发者可以更好地构建和维护复杂的应用程序。
1162 29
|
SQL Java 数据库连接
【潜意识Java】深入理解MyBatis的Mapper层,以及让数据访问更高效的详细分析
深入理解MyBatis的Mapper层,以及让数据访问更高效的详细分析
3534 1
|
SQL JSON Java
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
这篇文章介绍了如何在Spring Boot项目中整合MyBatis和PageHelper进行分页操作,并且集成Swagger2来生成API文档,同时定义了统一的数据返回格式和请求模块。
1128 1
mybatis使用三:springboot整合mybatis,使用PageHelper 进行分页操作,并整合swagger2。使用正规的开发模式:定义统一的数据返回格式和请求模块
|
缓存 前端开发 Java
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
Soring Boot的起步依赖、启动流程、自动装配、常用的注解、Spring MVC的执行流程、对MVC的理解、RestFull风格、为什么service层要写接口、MyBatis的缓存机制、$和#有什么区别、resultType和resultMap区别、cookie和session的区别是什么?session的工作原理
【Java面试题汇总】Spring,SpringBoot,SpringMVC,Mybatis,JavaWeb篇(2023版)
|
Java 数据库连接 测试技术
SpringBoot 3.3.2 + ShardingSphere 5.5 + Mybatis-plus:轻松搞定数据加解密,支持字段级!
【8月更文挑战第30天】在数据驱动的时代,数据的安全性显得尤为重要。特别是在涉及用户隐私或敏感信息的应用中,如何确保数据在存储和传输过程中的安全性成为了开发者必须面对的问题。今天,我们将围绕SpringBoot 3.3.2、ShardingSphere 5.5以及Mybatis-plus的组合,探讨如何轻松实现数据的字段级加解密,为数据安全保驾护航。
1735 1
|
SQL 关系型数据库 MySQL
解决:Mybatis-plus向数据库插入数据的时候 报You have an error in your SQL syntax
该博客文章讨论了在使用Mybatis-Plus向数据库插入数据时遇到的一个常见问题:SQL语法错误。作者发现错误是由于数据库字段中使用了MySQL的关键字,导致SQL语句执行失败。解决方法是将这些关键字替换为其他字段名称,以避免语法错误。文章通过截图展示了具体的操作步骤。
|
Java 数据库连接 Maven
文本,使用SpringBoot工程创建一个Mybatis-plus项目,Mybatis-plus在编写数据层接口,用extends BaseMapper<User>继承实体类
文本,使用SpringBoot工程创建一个Mybatis-plus项目,Mybatis-plus在编写数据层接口,用extends BaseMapper<User>继承实体类
|
Java 关系型数据库 MySQL
Mybatis入门之在基于Springboot的框架下拿到MySQL中数据
Mybatis入门之在基于Springboot的框架下拿到MySQL中数据
463 4
|
安全 前端开发 Java
挑战5分钟内基于Springboot+SpringMVC+Mybatis-plus快速构建web后端三层架构
挑战5分钟内基于Springboot+SpringMVC+Mybatis-plus快速构建web后端三层架构
485 1