spring configuration data source
Our spring configuration data sources commonly used in three ways
The first is a very common way
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
We can see, this is a very dead written in a way, there are two kinds of the following is written in a way that is more flexible
A data source configured using properties file
We create the Resource file in the folder (create) a file named suffix database of properties
This file we put in our database of the JDBC connection mysql database-driven statement
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8
user=root
password=root
Here we are configuring a data source
<!-- 1、配置数据源 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:database.properties</value>
</property>
</bean>
Then add in the following database configuration of our spring
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
values into value inside {} $
The corresponding data inside the {} brackets fill your database file, match
Another way is:
3. JNDI configuration data source
If the application is deployed in high-performance application server (such as Tomcat, the WebLogic), we may use the data source program provided by the server itself, the application server data source JNDI way users call, Spring to devote the references 1 JNDI resources JNDIObjectFactoryBean class
Using JNDI data source arranged
== provided that the application server must be configured on good data sources, we Tomcat, for example, need to configure the data source database-driven file into the lib directory of Tomcat and modify context.xml file in the conf directory of the Tomcat, == configuration data source
<Resource name="jdbc/news" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000" username="root"
password="root" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/newsmanagersystem?
useUnicode=true&characterEncoding=utf-8"/>
</Context>
Then back to our spring configuration file, add the code
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<!-- 通过jndiName指定引用的JNDI数据源名称 -->
<property name="jndiName" value="java:comp/env/jdbc/smbms"/>
</bean>
This way we completed the spring configuration data source
Original: https://www.cnblogs.com/liujunwei/p/11573287.html
Switching multiple data sources
Multi-database configurations:
As business needs, projects to use multiple databases for business development:
First, we must configure the custom in application.property two data sources, one using first.datasource. *, And the other using second.datasource. *, In order to enable others to see at a glance what the library is connected, you can use database name, such as user library, you can use user.datasource. *, in the use of multiple data sources, all necessary configurations can not be omitted.
first.datasource.url=jdbc:mysql://localhost/first
first.datasource.username=dbuser1
first.datasource.password=dbpass1
first.datasource.driver-class-name=com.mysql.jdbc.Driver
first.datasource.type=com.alibaba.druid.pool.DruidDataSource//我用的是Druid,也可以不加用默认的
second.datasource.url=jdbc:mysql://localhost/second
second.datasource.username=dbuser2
second.datasource.password=dbpass2
second.datasource.driver-class-name=com.mysql.jdbc.Driver
second.datasource.type=com.alibaba.druid.pool.DruidDataSource
Directly on the code, my practice is to use two data sources to create a two class configuration:
@Configuration
@MapperScan (basePackages = { "com.user.server.dao"}, sqlSessionTemplateRef = "userSqlSessionTemplate")
public class UserMybatisConfig {
@Bean (name = "userDataSource")
@Primary // This annotation can be added, or error, the next class is no need to add
@ConfigurationProperties (prefix = "first.datasource") // prefix value must be in the corresponding attribute prefix application.properteis
public userDataSource the DataSource () {
return DataSourceBuilder.create () Build ();.
}
@Bean
public a SqlSessionFactory userSqlSessionFactory (@Qualifier ( "userDataSource") the DataSource the dataSource) throws Exception {
the SqlSessionFactoryBean the SqlSessionFactoryBean the bean new new = ();
bean.setDataSource (the dataSource);
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath*:com/user/server/dao/mapping/*.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Bean
public SqlSessionTemplate userSqlSessionTemplate(@Qualifier("userSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
return template;
}
}
@Configuration
@MapperScan(basePackages = {"com.airmi.server.dao"}, sqlSessionTemplateRef = "autoTestSqlSessionTemplate")
public class AutoTestMybatisConfig {
@Bean
@ConfigurationProperties(prefix = "autotest.datasource")
public DataSource autoTestDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionTemplate autoTestSqlSessionTemplate(@Qualifier("autoTestSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory);
return template;
}
@Bean
public SqlSessionFactory autoTestSqlSessionFactory(@Qualifier("autoTestDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//添加XML目录
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath*:com/airmi/server/dao/mapping/*.xml"));
return bean.getObject();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
@Primary // The notes indicate when there is more of the same interface implementation class can be injected, which is selected by default, rather than being given autowire notes, the official website of the requirements when multiple data sources, you must specify a datasource, another datasource then do not add.
@Qualifier implanting by name, usually having a plurality of injection instances of the same type (e.g., multiple instances of type DataSource).
@MapperScan (basePackages = { "com.user.server.dao"}, sqlSessionTemplateRef = "userSqlSessionTemplate") instances for the package mapper basePackages where, sqlSessionTemplateRef to reference.
Original: https://blog.csdn.net/mxw2552261/article/details/78640062