1. 程式人生 > >Spring Boot(二)使用Maven建立springboot專案入門+整合Mybatis

Spring Boot(二)使用Maven建立springboot專案入門+整合Mybatis

前言

    SpringBoot本身已經提供了依託於maven的Spring Initializr作為建立專案的快速方式,但是當前使用Maven建立SpringBoot專案更容易按照我們的要求對專案進行配置。

建立過程

    一、建立專案(什麼快捷框架也不選)
        maven –>next –>next

    二、在main下新建webapp Package包

    三、新增maven管理配置

<packaging>war</packaging>

<parent>
    <groupId>
org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.1.RELEASE</version> </parent> <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>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> </dependencies>

    四、打包配置

<packaging>war</packaging>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

    五、在java目錄下新建層級com.basic.springbootmvn,新建Application.java作為入口類

@SpringBootApplication
@MapperScan("com.basic.springbootmvn.Dao")//非常重要
@ImportResource(locations={"classpath:Mapper/*.xml"})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

    六、 在resource中新建application.yml
         新增如下配置


server:
  port: 8081
  tomcat:
    uri-encoding: utf-8
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.38.128:3306/teststu
    username: root
    password: 1234
mybatis:
  typeAliasesPackage: com.basic.springbootmvn.Entity
  mapperLocations: classpath*:/Mapper/*.xml

    七、新建Entity、Dao、service、serviceImpl、Controller,在resource中新建Mapper 新建.xml檔案
         和在Spring Initializr建立相同 詳見:https://blog.csdn.net/idomyway/article/details/80516848

    八、執行配置

         Run/Debug configurations 中新增springboot 配置名稱和入口類即可執行