学习写代码呀

导航

springboot JPA+mybatis双数据源

pom文件依赖:
<spring.boot.version>2.0.6.RELEASE</spring.boot.version>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
  </dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>




application.yml:
spring:
  datasource:
jpasource:
driverClassName: org.sqlite.JDBC
url: jdbc:sqlite:./test.sqllite
sql-script-encoding: utf-8
mybatis:
jdbc-url: localhost:8080
driver-class-name:
username: test
password: 123456



package com.config.datasource;

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.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfig {
@Bean(name = "jpaDataSource")
@Qualifier("jpaDataSource")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.jpasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "mybatisDataSource")
@Qualifier("mybatisDataSource")
@ConfigurationProperties(prefix = "spring.datasource.mybatis")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "mybatisJdbcTemplate")
@Primary
@Qualifier("mybatisJdbcTemplate")
public NamedParameterJdbcTemplate primaryJdbcTemplate(
@Qualifier("mybatisDataSource") DataSource dataSource) {
return new NamedParameterJdbcTemplate(dataSource);
}

}

package com.config.datasource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;

@Configuration
@EnableJpaRepositories(entityManagerFactoryRef ="jpaEntityManagerFactory",transactionManagerRef ="jpaTransactionManager",basePackages ="com.travelsky.repository")
@EnableTransactionManagement
public class JpaConfig {
@Autowired
@Qualifier("jpaDataSource")
private DataSource jpaDataSource;

@Bean(name ="jpaEntityManager")
@Primary
public EntityManager entityManager() {
return entityManagerFactory().getObject().createEntityManager();
}

@Bean(name ="jpaEntityManagerFactory")
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory()
{
HibernateJpaVendorAdapter japVendor =new HibernateJpaVendorAdapter();
japVendor.setGenerateDdl(true);
japVendor.setDatabasePlatform("org.hibernate.dialect.SQLiteDialect");
japVendor.setShowSql(true);
LocalContainerEntityManagerFactoryBean entityManagerFactory =new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setDataSource(jpaDataSource);
entityManagerFactory.setJpaVendorAdapter(japVendor);
entityManagerFactory.setPackagesToScan("com.travelsky.model");
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.SQLiteDialect");
properties.setProperty("hibernate.hbm2ddl.auto", "update");
entityManagerFactory.setJpaProperties(properties);
return entityManagerFactory;
}

@Bean(name ="jpaTransactionManager")
@Primary
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory)
{
JpaTransactionManager manager =new JpaTransactionManager();
manager.setEntityManagerFactory(entityManagerFactory);
return manager;
}

@Bean
public BeanPostProcessor persistenceTranslation() {
return new PersistenceAnnotationBeanPostProcessor();
}
}

package com.config.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.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = {"com.travelsky.mapper"}, sqlSessionFactoryRef = "sqlSessionFactory")
public class MyBatisConfig {

@Autowired
@Qualifier("mybatisDataSource")
private DataSource mybaitsDs;

@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
// 使用mybatisDataSource数据源, 连接业务库
factoryBean.setDataSource(mybaitsDs);
Resource[] mapperLocations = (Resource[]) new PathMatchingResourcePatternResolver().getResources("classpath*:com/travelksy/mapper/OtherInfoMapper.xml");
factoryBean.setMapperLocations(mapperLocations);
return factoryBean.getObject();

}
@Bean
public SqlSessionTemplate sqlSessionTemplate() throws Exception {
// 使用上面配置的Factory
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory());
return template;
}

}

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisDataUtil {
@Autowired
private SqlSessionTemplate sqlSessionTemplate;

public Long getEfrontInfo(String empNo){
Long count = sqlSessionTemplate.selectOne("com.travelksy.mapper.OtherInfoMapper.findUserById",empNo);
System.out.println(count);
return count;
}
}


import org.apache.ibatis.annotations.Param;

public interface OtherInfoMapper {
Long findNewsTitles(@Param("no") String no);
}

<?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.travelsky.mapper.OtherInfoMapper">
<!--1.必须加上:statementType="STATEMENT"-->
<!--2.只能用${},不能用#{}-->
<select id="findNewsTitles" statementType="STATEMENT" resultType="java.lang.Long">
select count(*) from test_user where user_id='${no}'
</select>
</mapper>

jpa获取不到数据源dataSource
 
 
 
 
 
 
 
 

posted on 2020-06-29 20:55  学习写代码呀  阅读(433)  评论(0编辑  收藏  举报