天天看點

maven搭建 Spinrgboot + mybais 實作多資料源(SQL server) 1.檔案配置1.maven目錄結構

1.maven目錄結構

     這裡隻簡單介紹目錄結構: 

maven搭建 Spinrgboot + mybais 實作多資料源(SQL server) 1.檔案配置1.maven目錄結構

     多的不說,直接上圖

maven搭建 Spinrgboot + mybais 實作多資料源(SQL server) 1.檔案配置1.maven目錄結構

1.1接下來配置pom.xml檔案

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.wh.springboot</groupId>
	<artifactId>Text</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>interfaceManage Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.4.1.RELEASE</version>
	</parent>

	<dependencies>
		<!-- Spring Boot -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- Spring boot AOP -->

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>


		<!-- AspectJ -->
		<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
		<dependency>
			<groupId>aopalliance</groupId>
			<artifactId>aopalliance</artifactId>
			<version>1.0</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-tomcat</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- MyBatis -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>

		<!--啟動時啟動内置tomcat -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
		</dependency>

		<!--對Jsp支援 -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
		</dependency>

		<!-- 支援jstl -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- SQL server驅動 -->
		<dependency>
			<groupId>com.microsoft.sqlserver</groupId>
			<artifactId>sqljdbc4</artifactId>
			<version>4.1</version>
		</dependency>
		<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> 
			<version>1.3.8.RELEASE</version> </dependency> -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.yaml</groupId>
			<artifactId>snakeyaml</artifactId>
			<version>${snakeyaml.version}</version>
		</dependency>
		<!--啟動時啟動内置tomcat -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
	</dependencies>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.7</java.version>
	</properties>
	<build>
		<finalName>interfaceManage</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>
           

 1.2配置appliaction.yml 檔案

  這裡需要注意一下。appliaction.yml檔案需要放在main/resource根目錄下。

spring:
 profiles:
  active: env

server:
  port: 8080
  address:  localhost
  context-path: /interfaceManage

tomcat:
  uri-encoding: utf-8
logging:
 level:
  com.ruoyi: debug
  org.springframework: WARN
  org.spring.springboot.dao: debug
# 配置jdbc資料源
datasource:
 ds1:
  driverClassName: "com.microsoft.sqlserver.jdbc.SQLServerDriver"
  url: "jdbc:sqlserver://ip位址:端口号;DatabaseName=資料庫名"
  userName: 賬号
  password: 密碼
  initial-size: 10
  max-active: 1000
  min-idle: 10
  max-wait: 60000
  pool-prepared-statements: true
  max-pool-prepared-statement-per-connection-size: 20
  time-between-eviction-runs-millis: 60000
  min-evictable-idle-time-millis: 300000
  validation-query: SELECT 1 FROM DUAL
  test-while-idle: true
  test-on-borrow: false
  test-on-return: false
datasource:
 ds2:
  driverClassName: "com.microsoft.sqlserver.jdbc.SQLServerDriver"
  url: "jdbc:sqlserver://ip位址:端口号;DatabaseName=資料庫名"
  userName: 賬号
  password: 密碼
  initial-size: 10
  max-active: 1000
  min-idle: 10
  max-wait: 60000
  pool-prepared-statements: true
  max-pool-prepared-statement-per-connection-size: 20
  time-between-eviction-runs-millis: 60000
  min-evictable-idle-time-millis: 300000
  validation-query: SELECT 1 FROM DUAL
  test-while-idle: true
  test-on-borrow: false
  test-on-return: false
 
http:
ncoding.charset: UTF-8
encoding.enable: true
encoding.foce: true
mybatis:
typeAliasesPackage: # 掃描實體包的路徑 例 com.wh.spring.entity
mapperLocations:  掃描xml檔案的路徑位址 例 classpath:com.wh.spring.persistence/*.xml
configLocation: mybatis配置檔案路徑 例 classpath:mybatis/mybatis-config.xml
           

1.3配置mybatis檔案:mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	<settings>
		<setting name="cacheEnabled" value="true" />  <!-- 全局映射器啟用緩存 -->
		<setting name="useGeneratedKeys" value="true" />  <!-- 允許 JDBC 支援自動生成主鍵 -->
		<setting name="defaultExecutorType" value="REUSE" /> <!-- 配置預設的執行器 -->
		<setting name="logImpl" value="SLF4J" /> <!-- 指定 MyBatis 所用日志的具體實作 -->
		<!-- <setting name="mapUnderscoreToCamelCase" value="true"/> 駝峰式命名 -->
	</settings>
</configuration>
           

1.4配置log檔案:logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<!-- %m輸出的資訊,%p日志級别,%t線程名,%d日期,%c類的全名,,,, -->
	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<pattern>%d %p (%file:%line\)- %m%n</pattern>
			<charset>UTF-8</charset><!-- 此處設定字元集 -->
		</encoder>
	</appender>
	<appender name="baselog"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<File>log/base.log</File>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>log/base.log.%d.%i</fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<!-- or whenever the file size reaches 64 MB -->
				<maxFileSize>64 MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>
		<encoder>
			<pattern>
				%d %p (%file:%line\)- %m%n
			</pattern>
			<charset>UTF-8</charset> <!-- 此處設定字元集 -->
		</encoder>
	</appender>
	<root level="info">
		<appender-ref ref="STDOUT" />
	</root>
	<logger name="com.example" level="DEBUG">
		<appender-ref ref="baselog" />
	</logger>
</configuration>
           

繼續閱讀