项目路径
添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.example</groupId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--多数据源事务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jta-atomikos</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
创建DataSourceConfig
import com.alibaba.druid.pool.xa.DruidXADataSource;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class DataSourceConfig {
@Bean(name = "primaryProperty")
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DruidXADataSource primaryDataSource() {
return new DruidXADataSource();
}
@Primary
@Bean(name = "secondaryProperty")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DruidXADataSource secondaryDataSource() {
return new DruidXADataSource();
}
}
创建PrimaryDBConfig
package com.demp.yc.config;
import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages ={"com.demp.yc.dao.primary"}, sqlSessionTemplateRef = "primarySqlSessionTemplate")
public class PrimaryDBConfig {
@Bean(name = "primaryDataSource")
public DataSource dataSourceCar(@Qualifier("primaryProperty") DruidXADataSource druidXADataSource) {
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(druidXADataSource);
xaDataSource.setUniqueResourceName("primaryDataSource");
return xaDataSource;
}
@Bean(name = "primarySqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/oa/*.xml"));//扫描指定目录的xml
return bean.getObject();
}
@Bean(name = "primarySqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(
@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
System.out.println("123");
return new SqlSessionTemplate(sqlSessionFactory);
}
}
创建SecondaryDBConfig
package com.demp.yc.config;
import com.alibaba.druid.pool.xa.DruidXADataSource;
import com.atomikos.jdbc.AtomikosDataSourceBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import tk.mybatis.spring.annotation.MapperScan;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = {"com.demp.yc.dao.secondary"}, sqlSessionTemplateRef = "secondarySqlSessionTemplate")
public class SecondaryDBConfig {
@Bean(name = "secondaryDataSource")
public DataSource dataSourceCar(@Qualifier("secondaryProperty") DruidXADataSource druidXADataSource) {
AtomikosDataSourceBean xaDataSource = new AtomikosDataSourceBean();
xaDataSource.setXaDataSource(druidXADataSource);
xaDataSource.setUniqueResourceName("secondaryDataSource");
return xaDataSource;
}
@Bean(name = "secondarySqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/xg/*.xml"));//扫描指定目录的xml
return bean.getObject();
}
@Bean(name = "secondarySqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(
@Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
创建Dao接口
package com.demp.yc.dao.primary;
import com.demp.yc.pojo.data;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface OaMapper {
int add(data add);
}
import com.demp.yc.pojo.data;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface XgMapper {
int add(data add);
}
创建yml配置文件
spring:
datasource:
primary:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/oa?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 353632
secondary:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/xg?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai
username: root
password: 353632
创建实体类
省略吧。。。
在servect 包下创建接口和实现类
package com.demp.yc.service;
import com.demp.yc.pojo.data;
public interface ServiceOa {
int add(data add);
}
实现类
package com.demp.yc.service.impl;
import com.demp.yc.dao.primary.OaMapper;
import com.demp.yc.pojo.data;
import com.demp.yc.service.ServiceOa;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class ServiceOaImpl implements ServiceOa {
@Resource
OaMapper oaMapper;
@Override
public int add(data add) {
return oaMapper.add(add);
}
}
在资源目录下创建Mapper映射文件
<?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.demp.yc.dao.primary.OaMapper">
<insert id="add" parameterType="com.demp.yc.pojo.data">
insert into data_xsxx(id,xh,xm,bj,zy,yx,xb,csrq,mz,rxsj)
value (#{id},#{xh},#{xm},#{bj},#{zy},#{yx},#{xb},#{csrq},#{mz},#{rxsj})
</insert>
</mapper>
在测试类开始测试
@Test
public void qq(){ //次数据库插入
try{
data da=new data();
da.setId("2");
da.setZy("q");
da.setYx("w");
da.setXh("e");
da.setXb("r");
da.setXm("t");
da.setBj("55");
da.setMz("123");
da.setRxsj(new Date());
da.setCsrq(new Date());
serviceXg.add(da);
System.out.println("执行成功");
}catch (Exception e){
System.out.println("保存异常B"+e);
}
}