main
xingzi 2 years ago
parent d2099524ac
commit af3a894c14

@ -15,9 +15,6 @@
*/ */
package me.zhengjie; 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.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.ApplicationPidFileWriter; 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.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import me.zhengjie.annotation.rest.AnonymousGetMapping;
import me.zhengjie.utils.SpringContextHolder;
/** /**
* -> @EnableJpaAuditing * -> @EnableJpaAuditing
* *
@ -60,6 +62,7 @@ public class AppRun {
* @return / * @return /
*/ */
@AnonymousGetMapping("/") @AnonymousGetMapping("/")
@Transactional
public String index() { public String index() {
return "Backend service started successfully"; 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.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -16,7 +16,7 @@ public class DataSourceConfig {
* @return * @return
*/ */
@Primary @Primary
@Bean(name = "primaryDataSource") @Bean(name = "primaryJpaDataSource")
@ConfigurationProperties("spring.datasource.druid") @ConfigurationProperties("spring.datasource.druid")
public DataSource dataSource() { public DataSource dataSource() {
return DruidDataSourceBuilder.create().build(); return DruidDataSourceBuilder.create().build();
@ -27,7 +27,7 @@ public class DataSourceConfig {
* *
* @return * @return
*/ */
@Bean(name = "secondDataSource") @Bean(name = "secondJpaDataSource")
@ConfigurationProperties("spring.datasource.secondary") @ConfigurationProperties("spring.datasource.secondary")
public DataSource secondDataSource() { public DataSource secondDataSource() {
return DruidDataSourceBuilder.create().build(); 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.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties; import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
@ -22,7 +22,9 @@ import java.util.Map;
@Configuration @Configuration
@EnableTransactionManagement @EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPrimary", transactionManagerRef = "transactionManagerPrimary", basePackages = { @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 { public class JPAPrimaryConfig {
@Resource @Resource
@ -32,7 +34,7 @@ public class JPAPrimaryConfig {
private HibernateProperties hibernateProperties; private HibernateProperties hibernateProperties;
@Resource @Resource
@Qualifier("primaryDataSource") @Qualifier("primaryJpaDataSource")
private DataSource primaryDataSource; private DataSource primaryDataSource;
@Primary @Primary
@ -47,7 +49,7 @@ public class JPAPrimaryConfig {
Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(), Map<String, Object> properties = hibernateProperties.determineHibernateProperties(jpaProperties.getProperties(),
new HibernateSettings()); new HibernateSettings());
return builder.dataSource(primaryDataSource).properties(properties) 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(); .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.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties; import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
@ -36,7 +36,7 @@ public class JPASecondaryConfig {
private HibernateProperties hibernateProperties; private HibernateProperties hibernateProperties;
@Resource @Resource
@Qualifier("secondDataSource") @Qualifier("secondJpaDataSource")
private DataSource secondaryDataSource; private DataSource secondaryDataSource;
@Bean(name = "entityManagerSecondary") @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 @NoArgsConstructor
@ToString @ToString
public class IStore { public class IStore {
private Long id; private Long store_id;
private String name; private String name;
private Integer subCount;
private String code;
} }

@ -50,15 +50,16 @@ spring:
multi-statement-allow: true multi-statement-allow: true
secondary: secondary:
db-type: com.alibaba.druid.pool.DruidDataSource 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 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 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} username: ${DB_USER:flow}
password: ${DB_PWD:f321123} 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
mybatis: mybatis:
type-aliases-package: me.zhengjie.modules.system.pojo type-aliases-package: me.zhengjie.modules.system.pojo

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?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"> <!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> <select id="getAll" resultType="IStore"> select * from sys_store </select>
</mapper> </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> <dependency>
<groupId>org.mybatis.spring.boot</groupId> <groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId> <artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version> <version>2.3.1</version>
</dependency> </dependency>
</dependencies> </dependencies>

Loading…
Cancel
Save