I. configuration file structure
First of all, the structure of the configuration file is as follows
Here are a few key configurations
1. jdbc.properties
#============================================================================ # datasource 1 #============================================================================ #Database connection jdbc.driverClass=com.mysql.jdbc.Driver #Database connection url jdbc.jdbcUrl=jdbc:mysql://127.0.0.10:3306/device_operlog #Database user name jdbc.user=root #Database password jdbc.password=root #============================================================================ # datasource 2 #============================================================================ #Database connection jdbc2.driverClass=com.mysql.jdbc.Driver #Database connection url jdbc2.jdbcUrl=jdbc:mysql://127.0.0.11:3306/device_operlog #Database user name jdbc2.user=root #Database password jdbc2.password=root
2. spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- read jdbc,config configuration file,To read multiple configuration files, you need to add ignore-unresolvable="true",If there are the same fields, the first one scanned shall prevail --> <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true" /> <!-- Hikari Datasource 1 --> <bean id="dataSource1" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown"> <property name="driverClassName" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.jdbcUrl}" /> <property name="username" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> <!-- Configured to connect to a read-only database true, Ensure safety --> <property name="readOnly" value="false" /> <!-- The maximum time (in milliseconds) to wait for a connection pool to allocate a connection. A connection that has not been available for more than this time will occur SQLException, default:30 second --> <property name="connectionTimeout" value="30000" /> <!-- A connection idle Maximum duration of state in milliseconds, timeout is released( retired),default:10 Minute --> <property name="idleTimeout" value="600000" /> <!-- The lifetime of a connection (in milliseconds), timeout and unused are released( retired),default:30 Minutes, 30 seconds less than the database timeout is recommended. Refer to MySQL wait_timeout Parameters ( show variables like '%timeout%';) --> <property name="maxLifetime" value="1800000" /> <!-- The maximum number of connections allowed in the connection pool. Default value: 10; recommended formula:((core_count * 2) + effective_spindle_count) --> <property name="maximumPoolSize" value="20" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource1" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:mappers/mybatis-display.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <property name="basePackage" value="com.xxx.display.dao" /> </bean> <!-- Hikari Datasource 2 --> <bean id="dataSource2" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown"> <property name="driverClassName" value="${jdbc2.driverClass}" /> <property name="jdbcUrl" value="${jdbc2.jdbcUrl}" /> <property name="username" value="${jdbc2.user}" /> <property name="password" value="${jdbc2.password}" /> <property name="readOnly" value="false" /> <property name="connectionTimeout" value="30000" /> <property name="idleTimeout" value="600000" /> <property name="maxLifetime" value="1800000" /> <property name="maximumPoolSize" value="20" /> </bean> <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource2" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:mappers/mybatis-certificate.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1" /> <property name="basePackage" value="com.xxx.certificate.dao" /> </bean> <!-- Start the package scanning function, multiple packages are separated by commas, so as to register with the@Controller,@Service,@repository,@Component The annotated class becomes spring Of bean --> <context:component-scan base-package="com.xxx.controller,com.xxx.service" /> </beans>
3. mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> <configuration> <settings> <!-- Global mapper enable caching --> <setting name="cacheEnabled" value="false" /> <!-- Turn off immediate loading of associated objects to improve performance when querying --> <setting name="lazyLoadingEnabled" value="true" /> <!-- Set the loading form of the associated object, here is the on-demand loading field(Load fields by SQL Appoint),All fields of the associated table are not loaded to improve performance --> <setting name="aggressiveLazyLoading" value="false" /> <!-- For unknown SQL Queries that allow different result sets to be returned for general purpose results --> <setting name="multipleResultSetsEnabled" value="true" /> <!-- Allow column labels in place of column names --> <setting name="useColumnLabel" value="true" /> <!-- Allow custom primary key values(For example, generated by programs UUID 32 Bit code as key value),Data table PK Build policy will be overridden --> <setting name="useGeneratedKeys" value="true" /> <!-- Give nested resultMap In fields-Mapping support for properties --> <setting name="autoMappingBehavior" value="FULL" /> <!-- For batch update operation cache SQL To improve performance --> <!-- <setting name="defaultExecutorType" value="BATCH" /> --> <!-- Timeout if the database fails to respond for more than 25000 seconds --> <setting name="defaultStatementTimeout" value="25000" /> </settings> <!-- Global alias settings, in the mapping file, you only need to write the alias, not the entire classpath --> </configuration>
Note that the corresponding mapper has been configured in the spring-mybatis.xml file, so there is no need to repeat the configuration, such as:
<mappers> <mapper resource="mappers/mybatis-certificate.xml"/> <mapper resource="mappers/mybatis-display.xml"/> </mappers>
Otherwise, an error will be reported.
4. Mybatis-certificate.xml < omit mybatis-display.xml >
Here, the namespace writes the full package name of the corresponding dao
II. Package structure
DisplayServiceImpl.java
CeritificateServiceImpl.java
Call their dao interfaces respectively, the same on the controller, and call different service s according to different businesses.
pom.xml configuration is attached
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <properties> <springframework>4.0.6.RELEASE</springframework> <servlet>3.1.0</servlet> <mybatis-version>3.3.1</mybatis-version> </properties> <dependencies> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.13</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.13</version> </dependency> <!-- Spring web mvc --> <!-- spring Core package --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${springframework}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${springframework}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${springframework}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${springframework}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${springframework}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${springframework}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${springframework}</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.31</version> </dependency> <!-- mybatis Core package --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis-version}</version> </dependency> <!-- mybatis-spring package --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP --> <dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP-java7</artifactId> <version>2.4.13</version> </dependency> </dependencies> <build> <finalName>xxx</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> </plugin> </plugins> </build> </project>