c语言sscanf函数的用法是什么
263
2022-09-25
SpringBoot添加多个数据源
SpringBoot添加多个数据源
1.启动类2.application.properties3.配置类4.新建服务类5.Mapper6.注意
1.启动类
package com.jack.hhitseat;import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication@EnableScheduling//启动定时任务//@MapperScan("com.jack.hhitseat.mapper.*")//EnableAutoConfiguration注解,关闭springBoot关于mybatis的一些自动注入@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})public class HhitseatApplication{ public static void main(String[] args) { SpringApplication.run(HhitseatApplication.class, args); }}
2.application.properties
server.port=8085debug=true ##打开sql执行语句打印日志###############################MySQL数据库配置################################ 数据库1spring.datasource.jdbc-url=jdbc:mysql://127.0.0.1:3306/hhitseat?characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=falsespring.datasource.username=rootspring.datasource.password=Jack..spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.type=com.alibaba.druid.pool.DruidDataSource# 数据库2spring.datasource.ginfo.jdbc-url=jdbc:mysql://139.999.666.888:3306/hhitseat?characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=falsespring.datasource.ginfo.username=rootspring.datasource.ginfo.password=JackWei..spring.datasource.ginfo.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.ginfo.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.druid.initial-size=5 spring.datasource.druid.min-idle=5 spring.datasource.druid.max-active=20 spring.datasource.druid.max-wait=60000 spring.datasource.druid.time-between-eviction-runs-millis=60000 spring.datasource.druid.min-evictable-idle-time-millis=300000 spring.datasource.druid.validation-query=SELECT 1 FROM DUAL spring.datasource.druid.test-while-idle=true spring.datasource.druid.test-on-borrow=false spring.datasource.druid.test-on-return=false spring.datasource.druid.pool-prepared-statements=true spring.datasource.druid.max-pool-prepared-statement-per-connection-size=20 spring.datasource.druid.filters=stat,wall,log4j logging.level.org.springframework.boot.autoconfigure=ERROR # Freemarker 模板配置#表示所有的访问都经过静态资源路径;spring.mvc.static-path-pattern=/**#在这里表示配置静态资源根路径spring.resources.static-locations=classpath:/static/spring.thymeleaf.cache=falsespring.thymeleaf.prefix=classpath:/templates/html/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTMLspring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/html#config page 配置分页插件pagehelper.helperDialect=mysqlpagehelper.reasonable=truepagehelper.supportMethodsArguments=truepagehelper.params = count=countSqlpagehelper.pageSize=5
3.配置类
package com.jack.hhitseat.config;import javax.sql.DataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;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;@Configuration@MapperScan(basePackages = "com.jack.hhitseat.mapper.ginfo",sqlSessionFactoryRef = "ginfoSqlSessionFactory")public class GinfoDataSourceConfig { @Primary @Bean(name = "ginfoDataSource") @ConfigurationProperties("spring.datasource.ginfo") public DataSource masterDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "ginfoSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("ginfoDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:com/jack/hhitseat/mapper/ginfo/*.xml")); return sessionFactoryBean.getObject(); }}
package com.jack.hhitseat.config;import javax.sql.DataSource;import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.boot.jdbc.DataSourceBuilder;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;@Configuration@MapperScan(basePackages = "com.jack.hhitseat.mapper.main",sqlSessionFactoryRef = "mainSqlSessionFactory")public class MainDataSourceConfig { @Primary @Bean(name = "mainDataSource") @ConfigurationProperties("spring.datasource") public DataSource masterDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "mainSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("mainDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:com/jack/hhitseat/mapper/main/*.xml")); return sessionFactoryBean.getObject(); }}
4.新建服务类
package com.jack.hhitseat.service.impl;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.jack.hhitseat.bean.Log;import com.jack.hhitseat.bean.LogExample;import com.jack.hhitseat.mapper.ginfo.GLogMapper;@Service("ginfoLogService")public class GinfoLogService { @Autowired private GLogMapper gLogMapper; public List
5.Mapper
package com.jack.hhitseat.mapper.ginfo;import com.jack.hhitseat.bean.Log;import com.jack.hhitseat.bean.LogExample;import java.util.List;import org.apache.ibatis.annotations.Param;import org.springframework.stereotype.Repository;public interface GLogMapper { List
6.注意
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~