Spring Boot + Mybatis + Spring MVC環境配置(一) :Spring Boot初始化,依賴新增

hky87發表於2018-08-18

最近在搭建一個Spring Boot + Mybatis + Spring MVC的環境,折騰來折騰去,兩三天才搞定,記錄下大概過程和遇到的錯誤

看一下Spring Boot官方的介紹 http://spring.io/projects/spring-boot   :

Spring Boot讓開發者省去了大多數Spring的配置,讓其只需要關注主要的開發和進行少量的Spring配置。

嵌入了Tomcat,已定義好大多數的依賴關係,簡化了專案的構建配置。我們在使用的時候不用關心框架之間的相容性,適用版本等問題,想使用的東西,僅僅新增一個配置就可以了,所以使用spring boot非常適合構建微服務。


第一步,訪問 https://start.spring.io/  生成一個spring boot專案


生成之後會下載一個壓縮包,解壓後,在Eclipse中Import為Maven專案


第二步,新增額外需要的依賴,如Mybatis

pom.xml內容如下:

<?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.kai</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.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</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<optional>true</optional>
		</dependency>
		<!-- <dependency> <groupId>io.kwy.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> 
			<version>1.5.3.1.RELEASE</version> </dependency> -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.6</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.2</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>2.11.1</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>com.zaxxer</groupId>
			<artifactId>HikariCP</artifactId>
			<version>3.2.0</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<fork>true</fork>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>


引入web模組

Spring Boot Web Starter

Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container

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

pom.xml檔案中預設有兩個模組:

spring-boot-starter:核心模組,包括自動配置支援、日誌和YAML;

spring-boot-starter-test:測試模組,包括JUnit、Hamcrest、Mockito。


開發環境的除錯

熱啟動在正常開發專案中已經很常見了吧,雖然平時開發web專案過程中,改動專案啟重啟總是報錯;但springBoot對除錯支援很好,修改之後可以實時生效,需要新增以下的配置:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
   </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <fork>true</fork>
            </configuration>
        </plugin>
   </plugins>
</build>

完整環境下載地址: https://github.com/CatherineHu/Spring-Boot-Mybatis-MVC  


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10314474/viewspace-2200331/,如需轉載,請註明出處,否則將追究法律責任。

相關文章