SpringBoot專案建立流程--SpringMVC

希小木發表於2021-06-28

SpringBoot專案建立步驟

1. 建立SpringBoot工程

(1) File New Project Spring Initilizr

(2) NameMySpringBoot

(3) Group: com.baidu

(4) java: 8

(5) Next

 

(6) Finish

(7) This Window

 

 

 

 

(8) 配置maven環境:

① File Settings Build,Execution,Deployment Build Tools Maven

② Maven Home PathMaven檔案路徑

③ User setting files:  Maven檔案路徑 → conf logging setting檔案

④ Local repository:自建路徑D:\Java\repository

⑤ OK

2. 建立Maven工程

(1) MySpringBoot下右鍵 → New moudle Maven

(2) Next

 

(3) Namespringboot_demo4

(4) Groupid :com.baidu

(5) Finish

 

3. 編輯Spring MVC

目錄結構如下:

 

操作步驟:

(1) 編輯pom.xml檔案

 


 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>com.jt</groupId>
 8     <artifactId>springboot_demo3</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10 
11     <!--1.parent標籤-->
12     <!--集中定義版本號-->
13     <parent>
14         <groupId>org.springframework.boot</groupId>
15         <artifactId>spring-boot-starter-parent</artifactId>
16         <version>2.5.1</version>
17         <relativePath/> <!-- lookup parent from repository -->
18     </parent>
19 
20 
21     <!--2.新增依賴項-->
22     <!--依賴: 專案中需要新增什麼功能,則依賴什麼jar包.-->
23     <dependencies>
24         <!--引入SpringMVC功能-->
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter-web</artifactId>
28         </dependency>
29         <!--SpringBoot的測試功能-->
30         <dependency>
31             <groupId>org.springframework.boot</groupId>
32             <artifactId>spring-boot-starter-test</artifactId>
33             <scope>test</scope>
34         </dependency>
35 
36         <!--SpringBoot核心機制: "開箱即用"
37         只需匯入特定的jar包檔案 則可以直接使用其中的功能
38         根本原因: SpringBoot是對框架的簡化,內部進行了擴充套件,無需程式設計師操作.
39         -->
40         <!--支援熱部署 -->
41         <dependency>
42             <groupId>org.springframework.boot</groupId>
43             <artifactId>spring-boot-devtools</artifactId>
44         </dependency>
45 
46         <!--引入外掛lombok 自動的set/get/構造方法外掛  -->
47         <dependency>
48             <groupId>org.projectlombok</groupId>
49             <artifactId>lombok</artifactId>
50         </dependency>
51 
52         <!--引入資料庫驅動 -->
53         <dependency>
54             <groupId>mysql</groupId>
55             <artifactId>mysql-connector-java</artifactId>
56             <scope>runtime</scope>
57         </dependency>
58 
59         <!--springBoot資料庫連線  -->
60         <dependency>
61             <groupId>org.springframework.boot</groupId>
62             <artifactId>spring-boot-starter-jdbc</artifactId>
63         </dependency>
64 
65         <!--spring整合mybatis-plus MP中包含了mybatis的包所以將原來的包刪除 -->
66         <dependency>
67             <groupId>com.baomidou</groupId>
68             <artifactId>mybatis-plus-boot-starter</artifactId>
69             <version>3.4.3</version>
70         </dependency>
71 
72     </dependencies>
73 
74 
75     <!--3.新增build標籤-->
76     <!--該外掛是SpringBoot專案打包時必須新增的.
77        如果沒有該外掛 則導致jar包不能執行
78    -->
79     <build>
80         <plugins>
81             <plugin>
82                 <groupId>org.springframework.boot</groupId>
83                 <artifactId>spring-boot-maven-plugin</artifactId>
84                 <version>2.5.1</version>
85             </plugin>
86         </plugins>
87     </build>
88 </project>

 

 

 

 

 

(2) 編輯主啟動類

  ① @SpringBootApplication

  ② @MapperScan(“com.jt.mapper”)  --> 包掃描

 

 1 package com.jt;
 2 
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 
 7 @SpringBootApplication
 8 @MapperScan("com.jt.mapper")
 9 public class RunApp {
10     public static void main(String[] args) {
11         SpringApplication.run(RunApp.class, args);
12     }
13 }

 

 

 

(3) 編輯pojo包下的User

  ① @Data  --> 自動生成setter/getter/to string/構造方法等

  ② @Accessors(chain = true)   --> 啟動鏈式載入(重寫set方法)

  ③ @TableName(“demo_user”)  --> 使用MP, 類名與表名對映

 

 1 package com.jt.pojo;
 2 
 3 import com.baomidou.mybatisplus.annotation.TableName;
 4 import lombok.Data;
 5 import lombok.experimental.Accessors;
 6 
 7 import java.io.Serializable;
 8 
 9 @Data
10 @Accessors(chain = true)
11 @TableName("demo_user")
12                   //規定:POJO實體一般都需要實現序列號介面
13 public class User implements Serializable {
14     private Integer id;
15     private String name;
16     private Integer age;
17     private String sex;
18 }

 

 

 

(4) 編輯mapper層下的UserMapper介面

 

 1 package com.jt.mapper;
 2 
 3 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
 4 import com.jt.pojo.User;
 5 import org.apache.ibatis.annotations.Mapper;
 6 
 7 //@Mapper 每個介面都要加一次,麻煩,所以提取到主啟動類RunApp上加@MapperScan("com.jt.Mapper")註解
 8 public interface UserMapper extends BaseMapper<User> {
 9 
10 }

 

 

 

(5) 編輯resources--mappers包下的UserMapper.xml對映檔案

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <!--語法:namespace="介面的路徑" -->
 6 <mapper namespace="com.jt.mapper.UserMapper">
 7 
 8  <!--   <select id="findAll" resultType="User">
 9         select * from demo_user
10     </select>
11 -->
12 </mapper>

 

 

 

(6) 編輯resources包下的application.yml核心配置檔案

 

server:
  port: 8090
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/jt?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
    username: root
    #如果密碼以數字0開頭,則使用""號包裹  "0123456"
    password: root

#SpringBoot整合MybatisPlus
mybatis-plus:
  #設定別名包
  type-aliases-package: com.jt.pojo
  #載入對映檔案
  mapper-locations: classpath:/mappers/*.xml
  #開啟駝峰對映
  configuration:
    map-underscore-to-camel-case: true
#列印Sql語句
logging:
  level:
    #指定包路徑,日誌輸出
    com.jt.mapper: debug

 

 

 

(7) 編輯service層下的UserService介面

 

1 package com.jt.service;
2 
3 import com.jt.pojo.User;
4 
5 import java.util.List;
6 
7 public interface UserService {
8     List<User> findAll();
9 }

 

 

 

(8) 編輯Service層下的UserServiceImpl實現類

  ① @Service

  ② @AutoWired  --> 依賴注入

  ③ @Override  --> 重寫介面方法

 

 1 package com.jt.service;
 2 
 3 import com.jt.mapper.UserMapper;
 4 import com.jt.pojo.User;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 import java.util.List;
 9 
10 @Service
11 public class UserServiceImpl implements UserService {
12     @Autowired
13     public UserMapper userMapper;
14 
15     @Override
16     public List<User> findAll() {
17         return userMapper.selectList(null);
18     }
19 }

 

 

 

(9) 編輯controller層下的UserController

  ① @RestController

  ② @CrossOrigin  --> 解決跨域問題

  ③ AutoWired  --> 依賴注入

  ④ @RequestMapping(“findAll”)

 

 1 package com.jt.controller;
 2 
 3 import com.jt.pojo.User;
 4 import com.jt.service.UserService;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.web.bind.annotation.CrossOrigin;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.bind.annotation.RestController;
 9 
10 import java.util.List;
11 
12 @CrossOrigin
13 @RestController
14 public class UserController {
15     @Autowired
16     private UserService userService;
17 
18     @RequestMapping("findAll")
19     public List<User> find() {
20         return userService.findAll();
21     }
22 }

 

 

相關文章