main
xingzi 2 years ago
parent d2099524ac
commit af3a894c14

@ -15,9 +15,6 @@
*/
package me.zhengjie;
import io.swagger.annotations.Api;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.utils.SpringContextHolder;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter;
@ -25,8 +22,13 @@ import org.springframework.context.annotation.Bean;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.utils.SpringContextHolder;
/**
* -> @EnableJpaAuditing
*
@ -60,6 +62,7 @@ public class AppRun {
* @return /
*/
@AnonymousGetMapping("/")
@Transactional
public String index() {
return "Backend service started successfully";
}

@ -1,4 +1,4 @@
package me.zhengjie.util;
package me.zhengjie.config.jpa;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
@ -16,7 +16,7 @@ public class DataSourceConfig {
* @return
*/
@Primary
@Bean(name = "primaryDataSource")
@Bean(name = "primaryJpaDataSource")
@ConfigurationProperties("spring.datasource.druid")
public DataSource dataSource() {
return DruidDataSourceBuilder.create().build();
@ -27,7 +27,7 @@ public class DataSourceConfig {
*
* @return
*/
@Bean(name = "secondDataSource")
@Bean(name = "secondJpaDataSource")
@ConfigurationProperties("spring.datasource.secondary")
public DataSource secondDataSource() {
return DruidDataSourceBuilder.create().build();

@ -1,4 +1,4 @@
package me.zhengjie.util;
package me.zhengjie.config.jpa;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
@ -22,7 +22,9 @@ import java.util.Map;
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = {
"me.zhengjie.repository","me.zhengjie.modules.quartz.repository","me.zhengjie.modules.mnt.repository" })
"me.zhengjie.repository", "me.zhengjie.modules.quartz.repository", "me.zhengjie.modules.mnt.repository",
"me.zhengjie.modules.company.repository", "me.zhengjie.modules.flow.repository",
"me.zhengjie.modules.goods.repository", "me.zhengjie.modules.supplier.repository" })
public class JPAPrimaryConfig {
@Resource
@ -32,7 +34,7 @@ public class JPAPrimaryConfig {
private HibernateProperties hibernateProperties;
@Resource
@Qualifier("primaryDataSource")
@Qualifier("primaryJpaDataSource")
private DataSource primaryDataSource;
@Primary
@ -47,7 +49,7 @@ public class JPAPrimaryConfig {
Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(),
new HibernateSettings());
return builder.dataSource(primaryDataSource).properties(properties)
.packages("me.zhengjie.domain","me.zhengjie.modules.quartz.domain","me.zhengjie.modules.mnt.domain")
.packages("me.zhengjie.domain", "me.zhengjie.modules.quartz.domain", "me.zhengjie.modules.mnt.domain")
.persistenceUnit("primaryPersistenceUnit").build();
}

@ -1,4 +1,4 @@
package me.zhengjie.util;
package me.zhengjie.config.jpa;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
@ -36,7 +36,7 @@ public class JPASecondaryConfig {
private HibernateProperties hibernateProperties;
@Resource
@Qualifier("secondDataSource")
@Qualifier("secondJpaDataSource")
private DataSource secondaryDataSource;
@Bean(name = "entityManagerSecondary")

@ -0,0 +1,50 @@
package me.zhengjie.config.mybatis;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
@Configuration
@MapperScan(basePackages = "me.zhengjie.mapper.db1.**", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class DataSourceConfigPrimary {
/**
* 配置主数据源
*
* @return
*/
@Bean(name = "primaryDataSource")
@Primary
@ConfigurationProperties("spring.datasource.druid")
public DataSource dataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:me.zhengjie.mapper.db1/*.xml"));
return bean.getObject();
}
@Bean("primarySqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(
@Qualifier("primarySqlSessionFactory") SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
}

@ -0,0 +1,44 @@
package me.zhengjie.config.mybatis;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
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;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
@Configuration
@MapperScan(basePackages = "me.zhengjie.mapper.db2.**", sqlSessionFactoryRef = "secondSqlSessionFactory")
public class DataSourceConfigSecond {
@Bean(name = "secondDataSource")
@ConfigurationProperties("spring.datasource.secondary")
public DataSource dataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:me.zhengjie.mapper.db2/*.xml"));
return bean.getObject();
}
@Bean("secondSqlSessionTemplate")
@Primary
public SqlSessionTemplate secondSqlSessionTemplate(
@Qualifier("secondSqlSessionFactory") SqlSessionFactory sessionFactory) {
return new SqlSessionTemplate(sessionFactory);
}
}

@ -0,0 +1,20 @@
package me.zhengjie.mapper.db1;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import me.zhengjie.modules.system.pojo.IStore;
@Mapper
public interface IStoreMapper {
/**
*
*/
List<IStore> getAll();
@Select("SELECT * FROM sys_store WHERE name = #{name}")
IStore findByName(@Param("name") String name);
}

@ -0,0 +1,20 @@
package me.zhengjie.mapper.db2;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import me.zhengjie.modules.system.pojo.IStore;
@Mapper
public interface IStoreMapper2 {
/**
*
*/
List<IStore> getAll();
@Select("SELECT * FROM sys_store WHERE name = #{name}")
IStore findByName(@Param("name") String name);
}

@ -1,15 +0,0 @@
package me.zhengjie.modules.system.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import me.zhengjie.modules.system.pojo.IStore;
@Mapper
public interface IStoreMapper {
/**
*
*/
List<IStore> getAll();
}

@ -10,6 +10,8 @@ import lombok.ToString;
@NoArgsConstructor
@ToString
public class IStore {
private Long id;
private Long store_id;
private String name;
private Integer subCount;
private String code;
}

@ -50,15 +50,16 @@ spring:
multi-statement-allow: true
secondary:
db-type: com.alibaba.druid.pool.DruidDataSource
driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url: jdbc:log4jdbc:mysql://${DB_HOST:140.249.182.90}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
username: ${DB_USER:liuniu}
password: ${DB_PWD:Lnkj2022}
third:
driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url: jdbc:log4jdbc:mysql://${DB_HOST:168.138.192.184}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
username: ${DB_USER:flow}
password: ${DB_PWD:f321123}
third:
driverClassName: net.sf.log4jdbc.sql.jdbcapi.DriverSpy
url: jdbc:log4jdbc:mysql://${DB_HOST:140.249.182.90}:${DB_PORT:3306}/${DB_NAME:eladmin}?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useSSL=false
username: ${DB_USER:liuniu}
password: ${DB_PWD:Lnkj2022}
#配置MyBatis
mybatis:
type-aliases-package: me.zhengjie.modules.system.pojo

@ -1,5 +1,5 @@
<?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="me.zhengjie.modules.system.mapper.IStoreMapper">
<mapper namespace="me.zhengjie.mapper.db1.IStoreMapper">
<select id="getAll" resultType="IStore"> select * from sys_store </select>
</mapper>

@ -0,0 +1,5 @@
<?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="me.zhengjie.mapper.db2.IStoreMapper2">
<select id="getAll" resultType="IStore"> select * from sys_store </select>
</mapper>

@ -199,7 +199,7 @@
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
<version>2.3.1</version>
</dependency>
</dependencies>

Loading…
Cancel
Save