1. 程式人生 > >SpringBoot+MyBatis+Shiro框架配置

SpringBoot+MyBatis+Shiro框架配置

1.引言

基於SpringBoot與MyBatis框架在Java開發中越來越流行,最近公司剛好需要技術變革,筆者也是頗費了寫心血做了框架的搭建和幾次框架的一直工作,本框架除了SpringBoot和MyBatista另外也揉入了當下比較流行的許可權安全認證框架Shiro,附帶架構設計,希望能幫助到有需要的人。

1.1 框架版本說明

技術的變革瞬息萬變,此處有必要對各個第三方框架的版本做一下說明:SpringBoot1.5.8,Mybatis1.3.2,shiro1.3.2,其他工具jar中帶有版本資訊,不在贅述。

2. SpringBoot框架配置說明

2.1 主要配置的包和檔案位置

在這裡插入圖片描述
專案結構規範參考阿里開發手冊

2.2 配置前準備工作

2.2.1 已構建一個maven環境
略,網上資料很多

2.2.2 規範專案結構

專案結構規範參考阿里開發手冊

2.2.3 在pom.xml檔案引入準備jar包

<?xml version="1.0" encoding="UTF-8"?>
<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>com.example</groupId> <artifactId>fanshion</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>fanshion</name> <description>Demo project for Spring Boot<
/description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- http呼叫依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- shiro 依賴 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.3.2</version> </dependency> <!-- shiro快取管理依賴 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.3.2</version> </dependency> <!-- 日誌管理依賴 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> </dependencies> <!--編譯相關配置--> <build> <finalName>ROOT</finalName> <plugins> <!-- add linux shell plugin --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version><!--$NO-MVN-MAN-VER$--> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> <compilerArguments> <verbose /> <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version><!--$NO-MVN-MAN-VER$--> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> </project> <?xml version="1.0" encoding="UTF-8"?> <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>com.example</groupId> <artifactId>fanshion</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>fanshion</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- http呼叫依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- shiro 依賴 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.3.2</version> </dependency> <!-- shiro快取管理依賴 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-ehcache</artifactId> <version>1.3.2</version> </dependency> <!-- 日誌管理依賴 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> </dependencies> <!--編譯相關配置--> <build> <finalName>ROOT</finalName> <plugins> <!-- add linux shell plugin --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.5.1</version><!--$NO-MVN-MAN-VER$--> <configuration> <source>${java.version}</source> <target>${java.version}</target> <encoding>${project.build.sourceEncoding}</encoding> <compilerArguments> <verbose /> <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath> </compilerArguments> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version><!--$NO-MVN-MAN-VER$--> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> </project>

2.2.4 在application-dev.yml檔案中新增專案相關配置
(1) 新建檔案application.yml

spring:
  profiles:
    active: dev  #對應loc表示本地,dev表示生產或者測試表的配置

(2)新建檔案application-loc.yml

server:
  port: 8080
  context-path: /

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/ejchina_pro_test?useSSL=false
    username: root
    password: fyx
    zeroDateTimeBehavior: convertToNull
  session:
      store-type: none
  redis:
    database: 0
    host: 118.190.64.183
    port: 6379
    password: redis1906
    pool:
      max-active: 8
      max-idle: 8
      max-wait: -1
      min-idle: 0
    timeout: 0
  freemarker:
    enabled: true
    cache: false
    charset: CESU-8
    content-type: text/html
    template-loader-path: classpath:/templates/
    suffix: .ftl
    request-context-attribute: request
    expose-session-attributes: true
    expose-request-attributes: true
    expose-spring-macro-helpers: true
    allow-request-override: true
    allow-session-override: true
    settings:
      date_format: yyyy-MM-dd
      time_format: HH:mm:ss
      datetime_format: yyyy-MM-dd HH:mm:ss
  mvc:
    static-path-pattern: /static/**
  devtools:
    restart:
      enabled: false
      additional-paths: src/main/java
      additional-exclude: target/classes

(3)新建檔案application-dev.yml

server:       #配置服務
  port: 8089          #配置服務埠
  context-path: /   #專案路徑,Spring boot預設是/,這樣直接通過http://ip:port/就可以訪問到index頁面

spring:              #Spring相關配置
  http:              # 整合HttpClient,封裝常用客戶端工具類
    multipart:
      enabled: true                #是否啟用
      max-file-size: -1            #限制檔案大小
      max-request-size: -1          #限制檔案大小
  datasource:                             #配置資料來源
    driver-class-name: com.mysql.jdbc.Driver       #資料庫驅動名稱
    url: jdbc:mysql://47.104.13.151:3306/ej_comment?useSSL=false       #資料庫地址
    username: ej_comment                    #資料庫名
    password: fZoPCyNINC+g8yH0A8Y68ApYTJgmtjM9PnkZlc3I1DxSLAUATn/TnEbiU3QlNq/ZUeKcSTjcjbVslQtvM+j6tQ==  #資料庫密碼(加密後)
    zeroDateTimeBehavior: convertToNull #把日期異常轉換為null代替異常處理
    type: com.alibaba.druid.pool.DruidDataSource #資料來源/連線池型別
    initialSize: 2                 #定義初始連線數
    minIdle: 5              #定義最小空閒 minIdle=1
    maxActive: 20       #定義最大連線數
    maxWait: