瞭解微服務以及Springboot整合mybaits+thymeleaf模板引擎實戰D經驗分享
前言: 首先肯定大家最想知道的還是Spring boot到底是幹啥的?其實我也是初學者,雖然接觸時間不長,但是也想把自己的理解和大家做一個交流。 簡單來說,Springboot就是一個更加靈活,配置起來更簡單,廣泛應用於微服務的一個開源框架。那大家肯定還會問,到底什麼是微服務呢?接下來我就先給大家通俗的講解一下我理解的微服務,其中還會給大家講一下分散式的一些概念。 想必大家都去食堂吃過飯,食堂有很多的不同飲食的視窗,每個視窗在食堂這個大廳內部各自負責自己的生意。其實我們就可以把這一個個視窗看作是一個個小的服務。我們想吃什麼可以直接去看對應的視窗就好了,菜品就相當於我們程式設計所說API。但是這樣還有一個問題就是如果重慶小面的視窗發生了意外,比如打翻了鍋,過著出現了擁擠,那旁邊的麻辣香鍋視窗一定會受一些影響。還有一個問題,假如西苑的同學想吃東苑風味餐廳的重慶小面,那該怎麼辦,我想大家想到的就是在西苑再開一家重慶小面,而不是再開一個風味餐廳吧。那麼如果不考慮佔地問題,最好的就是將餐廳的熱門視窗都分散在校園的最需要他們的不同地方吧。所以總的來說,微服務就是把食堂的各個視窗分散開,緩解了整個食堂的壓力。這一個個視窗就是我們說的微服務。 還有一個概念和微服務比較相似,那就是分散式,這個也是聽起來很高階,那到底什麼是分散式呢。直觀來說分散式就是一個業務分拆多個子業務,部署在不同的伺服器上。達到我們所說的分佈,通過縮短單個任務的執行時間來提升效率。從某一個角度來看,我認為分散式是屬於微服務的。微服務與分散式的細微差別是,微服務的應用不一定是分散在多個伺服器上,他也可以是同一個伺服器。 那瞭解了微服務,話題回到我們所說的springboot,為什麼說springboot可以廣泛應用到微服務架構上面呢,首先springboot設計的目的是為了簡化Spring應用初期工程的搭建以及開發過程。相信學習過Spring+Struts2+Hibernate, Spring+SpringMVC+Mybaits框架的同學,都能感受到前期 xml配置的複雜。 從一定的角度上說,springboot是基於習慣大於配置的思想,通過定義的註解替代了Spring應用中的.xml配置檔案,基於普遍習慣的配置思想,使得專案的搭建、開發和部署變得簡單。並且整合封裝了一些第三方框架,例如HIbernate,Mybaits,redis等,說到底其實並沒有增添新的東西。 再來說微服務,他是將一個專案將會被分成很多個子專案,專案之間獨立部署並通過協議進行資料互動。因此,利用SpringBoot這種輕便靈活的框架可以使得編碼、配置、部署、監控變得簡單自如。是把笨重的大型專案分開,提高效率,靈活輕便就是首先考慮的。 接下來我們就通過一個我最近寫的小demo來了解一下SpringBoot這個小傢伙。 首先,以IDEA為例,新建專案,選擇Spring Initializr,NEXT
<?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.jerry</groupId> <artifactId>mock-spring</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>mock-spring</name> <description>Demo project for Spring Boot</description> <!--要成為一個spring boot專案 首先就必須在pom.xml中繼承spring-boot-starter-parent,同時指定其版本--> <!--配置parent--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--配置引數 例如jdk版本--> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <!--配置依賴即用來配置spring boot自動引入的各個jar包的版本--> <dependencies> <!--thymeleafController--> <!--Springboot預設是不支援JSP的,預設使用thymeleaf模板引擎--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!--lombok--> <!-- Lombok 是一種 Java 實用工具,可用來幫助開發人員消除 Java 的冗長 尤其是對於簡單的 Java 物件(POJO)。它通過註解實現這一目的 由於 Lombok 採取的註解形式的, 在編譯後,自動生成相應的方法,為了不讓 ide 瘋了,需要下載外掛了支援它--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--單元測試的jar包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <!--自動生成文件和測試介面的ui,隔離前後端,很方便,後期慢慢解釋--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <!--springboot的熱部署--> <!--提高開發者的開發效率,無需手動重啟Spring Boot應用。--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <!--配置web容器--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--新增bootstrap依賴--> <dependency> <groupId>org.webjars</groupId> <artifactId>bootstrap</artifactId> <version>3.3.5</version> </dependency> <dependency> <groupId>org.webjars</groupId> <artifactId>jquery</artifactId> <version>3.1.1</version> </dependency> <!--解除html嚴格語法限制--> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> </dependency> <!-- 阿里巴巴連線池druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> <!--配置mybaits--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!--配置mysql驅動jar包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <!--配置mybaits自動生成程式碼的依賴--> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.5</version> </dependency> <!-- mybatis分頁功能 --> <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency> <!--排除spring boot對spring-boot-starter-logging的依賴, 並增加依賴包spring-boot-starter-log4j.jar--> <!--這裡我們使用log4j2進行日誌輸出--> <!--這樣寫可以忽略springboot自帶的日誌框架.--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <!-- 引入log4j2依賴 , 因為我的parent配置的版本太高沒有log4j的jar,所以我選擇了log4j2--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!--因為log4j2棄用了配置屬性檔案的方式,採用了yml和xml檔案配置的方式 新增能夠使用log4j2.yml配置的依賴--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> </dependency> </dependencies> <build> <!--配置外掛--> <plugins> <!--這裡給大家演示mybaits逆向生成mapper和pojo類的外掛 生成一些簡單的sql語句--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency> </dependencies> </plugin> <!--maven外掛--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <!--我們用到的資原始檔--> <!--其中**/*這樣的寫法,是為了保證各級子目錄下的資原始檔被打包--> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
因為用的是Springboot+mybaits這一套,使用了逆向工程工具。接下來就去配置一下。resources檔案下新建generatorConfig.xml檔案。在這之前按照SpringMVC那一套建好檔案目錄。 在generatorConfig中配置以下程式碼,生成檔案和資料庫的配置都有註釋可供參考。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <classPathEntry location="C:\Users\l\.m2\repository\mysql\mysql-connector-java\5.1.6\mysql-connector-java-5.1.6.jar" /> <context id="mysqlTables" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true" /> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springbootdb?serverTimezone=UTC" userId="root" password="jerry8199"> </jdbcConnection> <!--指定生成的型別為java型別,避免資料庫中number等型別欄位 --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--自動生成的實體的存放包路徑 --> <javaModelGenerator targetPackage="com.jerry.springbootdemo.entity" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--自動生成的*Mapper.xml檔案存放路徑 --> <sqlMapGenerator targetPackage="com.jerry.springbootdemo.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--自動生成的*Mapper.java存放路徑 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.jerry.springbootdemo.dao" targetProject="src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <table tableName="hero" domainObjectName="Hero" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <generatedKey column="ID" sqlStatement="selectuuid_short()" identity="false"/> </table> </context> </generatorConfiguration>
接下來再說一下application. properties這個屬性檔案的配置方式,內容自定義,都有註釋
#伺服器埠號
server.port=8877
#資料庫配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=jerry8199
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybaits的配置
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml
#thymeleaf的配置
#配置檔案中修改對映時檢視的前後綴
#(字首prefix是”classpath:/templates/”,字尾suffix是”.html” )
#禁用模板引擎的快取,要不然看不到實時頁面,其實感覺使用了熱部署就沒必要禁用快取了
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
接下來我們去配置一下swagger 首先來看一下Spring Boot的入口函式 開啟SpringBootDemoApplication 首先加上註解@EnableSwagger2 加上掃描包的註解 按照如下程式碼配置
package com.jerry.springbootdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.Random;
@EnableSwagger2
@MapperScan("com.jerry.springbootdemo.dao")
@SpringBootApplication
public class SpringBootDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoApplication.class, args);
}
@Bean
public Random random(){
return new Random();
}
@Bean
public Docket createRestApi(){
// System.out.println(this.getClass().getSimpleName().split("Application")[0].toLowerCase());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.jerry.springbootdemo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder().title("Spring Boot中使用Swagger構建Rest Api version Member")
.version("1.0").build();
}
}
剩下的就是去寫MVC了 實體類
package com.jerry.springbootdemo.entity;
public class Hero {
private Integer id;
private String heroName;
private String resource;
private String background;
private Integer attackPower;
private Integer defentPower;
private Integer faceScore;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getHeroName() {
return heroName;
}
public void setHeroName(String heroName) {
this.heroName = heroName == null ? null : heroName.trim();
}
public String getResource() {
return resource;
}
public void setResource(String resource) {
this.resource = resource == null ? null : resource.trim();
}
public String getBackground() {
return background;
}
public void setBackground(String background) {
this.background = background == null ? null : background.trim();
}
public Integer getAttackPower() {
return attackPower;
}
public void setAttackPower(Integer attackPower) {
this.attackPower = attackPower;
}
public Integer getDefentPower() {
return defentPower;
}
public void setDefentPower(Integer defentPower) {
this.defentPower = defentPower;
}
public Integer getFaceScore() {
return faceScore;
}
public void setFaceScore(Integer faceScore) {
this.faceScore = faceScore;
}
}
HeroMapper和HeroMapper.xml是自動生成的不多說了,貼程式碼
package com.jerry.springbootdemo.dao;
import com.jerry.springbootdemo.entity.Hero;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface HeroMapper {
int deleteByPrimaryKey(Integer id);
int insert(Hero record);
int insertSelective(Hero record);
Hero selectByPrimaryKey(Integer id);
List<Hero> getAllHeros();
int updateByPrimaryKeySelective(Hero record);
int updateByPrimaryKey(Hero record);
}
<?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.jerry.springbootdemo.dao.HeroMapper" >
<resultMap id="BaseResultMap" type="com.jerry.springbootdemo.entity.Hero" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="hero_name" property="heroName" jdbcType="VARCHAR" />
<result column="resource" property="resource" jdbcType="VARCHAR" />
<result column="background" property="background" jdbcType="VARCHAR" />
<result column="attack_power" property="attackPower" jdbcType="INTEGER" />
<result column="defent_power" property="defentPower" jdbcType="INTEGER" />
<result column="Face_score" property="faceScore" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, hero_name, resource, background, attack_power, defent_power, Face_score
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from hero
where id = #{id,jdbcType=INTEGER}
</select>
<select id="getAllHeros" resultMap="BaseResultMap">
SELECT * FROM hero
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from hero
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.jerry.springbootdemo.entity.Hero" >
<selectKey resultType="java.lang.Integer" keyProperty="id" order="BEFORE" >
selectuuid_short()
</selectKey>
insert into hero (id, hero_name, resource,
background, attack_power, defent_power,
Face_score)
values (#{id,jdbcType=INTEGER}, #{heroName,jdbcType=VARCHAR}, #{resource,jdbcType=VARCHAR},
#{background,jdbcType=VARCHAR}, #{attackPower,jdbcType=INTEGER}, #{defentPower,jdbcType=INTEGER},
#{faceScore,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.jerry.springbootdemo.entity.Hero" >
<selectKey resultType="java.lang.Integer" keyProperty="id" order="BEFORE" >
selectuuid_short()
</selectKey>
insert into hero
<trim prefix="(" suffix=")" suffixOverrides="," >
id,
<if test="heroName != null" >
hero_name,
</if>
<if test="resource != null" >
resource,
</if>
<if test="background != null" >
background,
</if>
<if test="attackPower != null" >
attack_power,
</if>
<if test="defentPower != null" >
defent_power,
</if>
<if test="faceScore != null" >
Face_score,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
#{id,jdbcType=INTEGER},
<if test="heroName != null" >
#{heroName,jdbcType=VARCHAR},
</if>
<if test="resource != null" >
#{resource,jdbcType=VARCHAR},
</if>
<if test="background != null" >
#{background,jdbcType=VARCHAR},
</if>
<if test="attackPower != null" >
#{attackPower,jdbcType=INTEGER},
</if>
<if test="defentPower != null" >
#{defentPower,jdbcType=INTEGER},
</if>
<if test="faceScore != null" >
#{faceScore,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.jerry.springbootdemo.entity.Hero" >
update hero
<set >
<if test="heroName != null" >
hero_name = #{heroName,jdbcType=VARCHAR},
</if>
<if test="resource != null" >
resource = #{resource,jdbcType=VARCHAR},
</if>
<if test="background != null" >
background = #{background,jdbcType=VARCHAR},
</if>
<if test="attackPower != null" >
attack_power = #{attackPower,jdbcType=INTEGER},
</if>
<if test="defentPower != null" >
defent_power = #{defentPower,jdbcType=INTEGER},
</if>
<if test="faceScore != null" >
Face_score = #{faceScore,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.jerry.springbootdemo.entity.Hero" >
update hero
set hero_name = #{heroName,jdbcType=VARCHAR},
resource = #{resource,jdbcType=VARCHAR},
background = #{background,jdbcType=VARCHAR},
attack_power = #{attackPower,jdbcType=INTEGER},
defent_power = #{defentPower,jdbcType=INTEGER},
Face_score = #{faceScore,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
Service層 介面類
package com.jerry.springbootdemo.service;
import com.jerry.springbootdemo.entity.Hero;
import java.util.List;
public interface IHeroService {
public Hero getHeroById(int id);
boolean addHero(Hero record);
List<Hero> getAllheros();
}
實現類
package com.jerry.springbootdemo.service.serviceimpl;
import com.jerry.springbootdemo.dao.HeroMapper;
import com.jerry.springbootdemo.entity.Hero;
import com.jerry.springbootdemo.service.IHeroService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
@Service("HeroService")
public class HeroServiceimpl implements IHeroService {
@Resource
private HeroMapper heroMapper;
@Override
@Transactional
public Hero getHeroById(int id) {
return heroMapper.selectByPrimaryKey(id);
}
@Override
@Transactional
public boolean addHero(Hero record) {
boolean result=false;
try{
heroMapper.insertSelective(record);
result=true;
}catch (Exception e){
e.printStackTrace();
}
return result;
}
@Override
@Transactional
public List<Hero> getAllheros() {
return heroMapper.getAllHeros();
}
}
Controller層 測試了返回json字串和直接返回頁面兩種
package com.jerry.springbootdemo.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jerry.springbootdemo.dao.HeroMapper;
import com.jerry.springbootdemo.entity.Hero;
import com.jerry.springbootdemo.service.serviceimpl.HeroServiceimpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
//controller註解不能使用@RestController,要用@Controller,
//因為@RestController [email protected][email protected],會直接返回josn資料
@Controller
@RequestMapping(value="templates")
public class thymeleafController {
@Resource
private HeroServiceimpl heroService;
@Resource
private HeroMapper heromapper;
public final static Logger logger = LoggerFactory.getLogger(thymeleafController.class);
@RequestMapping(value = "/getUserById", method = RequestMethod.GET)
public @ResponseBody Hero getUserInfo(Integer id){
Hero hero = heromapper.selectByPrimaryKey(1);
logger.debug("this is debug info");
logger.info("this is info info");
logger.warn("this is warn info");
logger.error("this is error info");
return hero;
}
@RequestMapping(value = "/thymeleaf")
public String ThymeleaPage(Model model){
List<Hero> heroes = heromapper.getAllHeros();
model.addAttribute("pageInfo", heroes);
return "index";
}
@GetMapping(value = "/json")
public @ResponseBody List<Hero> getUserInfo(){
List<Hero> heroes = heromapper.getAllHeros();
return heroes;
}
}
前端頁面用的boostrap框架的東西。這裡我用的是springboot比較支援的thymeleaf模板引擎,化靜為動還是蠻爽的,類似EL表示式,大家慢慢學習就知道了。以後還會練習RESTful的樣子,到時候再給大家分享。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World</title>
<link rel="stylesheet" href="/webjars/bootstrap/3.3.5/css/bootstrap.min.css" />
<script src="/webjars/jquery/3.1.1/jquery.min.js"></script>
<script src="/webjars/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container"><br/>
<div class="alert alert-success">
<a href="#" class="close" data-dismiss="alert" aria-label="close">x</a>
Hello, <strong>Jerry!</strong>
</div>
<div>
<table class="table table-striped">
<tr>
<th>id</th>
<th>name</th>
<th>resource</th>
<th>background</th>
<th>attack</th>
<th>defent</th>
<th>face</th>
</tr>
<tr th:each="hero : ${pageInfo}">
<td th:text="${hero.id}"></td>
<td th:text="${hero.heroName}"></td>
<td th:text="${hero.resource}"></td>
<td th:text="${hero.background}"></td>
<td th:text="${hero.attackPower}"></td>
<td th:text="${hero.defentPower}"></td>
<td th:text="${hero.faceScore}"></td>
</tr>
</table>
</div>
</div>
</body>
</html>
log4j2配置的東西有註釋。
Configuration:
status: warn
Properties: # 定義變數
Property:
- name: log.path
value: d:\\tmp\\logs
- name: project.name
value: spring-boot-log
Appenders:
Console: #輸出到控制檯
name: CONSOLE
target: SYSTEM_OUT
ThresholdFilter:
level: trace
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"
# 輸出到檔案,超過128MB歸檔
RollingFile:
- name: ROLLING_FILE
ignoreExceptions: false
fileName: ${log.path}/${project.name}.log
filePattern: "${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"
Policies:
SizeBasedTriggeringPolicy:
size: "128 MB"
DefaultRolloverStrategy:
max: 1000
Loggers:
Root:
level: info
AppenderRef:
- ref: CONSOLE
- ref: ROLLING_FILE
Logger: # 為com.xjj包配置特殊的Log級別,方便除錯
- name: com.test.dao
additivity: false
level: debug
AppenderRef:
- ref: CONSOLE
- ref: ROLLING_FILE
執行成功圖片 我們可以在localhost:埠號/swagger-ui.html中看api 專案成功截圖 到這裡就講解完啦,沒有很細緻。大家有問題可以給我留言大家一起交流。