SpringBoot的認識和基本使用

1、認識html

Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化Spring應用的初始搭建以及開發過程。 -使用springboot之後,搭建一個spring應用和開發變得很簡單.java

Springboot就是一些寫好了maven的模塊,咱們在使用SPring就不需以傳統的方式來用,只須要以maven導入對應的springboot模塊,就能完成一大堆操做。簡單的說,它使用maven的方式對Spring應用開發進行進一步封裝和簡化。mysql

Spring Boot使編碼更簡單,使配置更簡單,使部署更簡單,使監控更簡單。Springboot就是爲了簡化spring應用搭建,開發,部署,監控的開發工具。git

2、入門準備github

開發環境JDK 1.8web

項目管理工具( Maven )spring

開發工具(Eclipse/idea)sql

1.建立普通 maven項目 springboot_parent數據庫

2.配置pom.xmlapache

 1 <!--
 2         spring boot 父節點依賴,引入這個以後相關的引入
 3         就不須要添加version配置,
 4         spring boot會自動選擇最合適的版本進行添加。
 5     -->
 6     <parent>
 7         <groupId>org.springframework.boot</groupId>
 8         <artifactId>spring-boot-starter-parent</artifactId>
 9         <version>2.0.5.RELEASE</version>
10     </parent>
11 
12     <dependencyManagement>
13         <dependencies>
14             <!--springboot版本管理-->
15             <dependency>
16                 <groupId>org.springframework.boot</groupId>
17                 <artifactId>spring-boot-dependencies</artifactId>
18                 <version>2.0.5.RELEASE</version>
19                 <type>pom</type>
20                 <scope>import</scope>
21             </dependency>
22         </dependencies>
23     </dependencyManagement>

3、springBoot+springMVC

1.經過maven建立一個web模塊

2.配置pom.xml

 1    <!-- tomcat和springMVC-->
 2     <dependencies>
 3         <dependency>
 4             <groupId>org.springframework.boot</groupId>
 5             <artifactId>spring-boot-starter-web</artifactId>
 6         </dependency>
 7         <!-- 熱部署 ctrl+f9-->
 8         <dependency>
 9             <groupId>org.springframework.boot</groupId>
10             <artifactId>spring-boot-devtools</artifactId>
11             <optional>true</optional>
12             <scope>true</scope>
13         </dependency>
14     </dependencies>
15 
16     <build>
17         <plugins>
18             <plugin>
19                 <groupId>org.springframework.boot</groupId>
20                 <artifactId>spring-boot-maven-plugin</artifactId>
21                 <configuration>
22                     <!--fork :  若是沒有該項配置,可能devtools不會起做用,即應用不會restart -->
23                     <fork>true</fork>
24                 </configuration>
25             </plugin>
26         </plugins>
27     </build>

3.準備tomcat啓動類

1 @SpringBootApplication
2 public class MVCApp {
3 
4     public static void main(String[] args) {
5         SpringApplication.run(MVCApp.class);
6     }
7 }

4.經過controller層進行訪問

1 @RestController//包含JSON格式和controller
2 public class MvcController {
3 
4     @RequestMapping("/hello")
5     public String index(){
6         return "suSu";
7     }
8 }

 

4、springBoot+jsp跳轉

1.經過maven建立一個web模塊

2.配置pom.xml

 1     <properties>
 2         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 3         <maven.compiler.source>1.8</maven.compiler.source>
 4         <maven.compiler.target>1.8</maven.compiler.target>
 5     </properties>
 6 
 7     <dependencies>
 8         <dependency>
 9             <groupId>junit</groupId>
10             <artifactId>junit</artifactId>
11             <version>4.11</version>
12             <scope>test</scope>
13         </dependency>
14         <!-- web支持: 一、web mvc; 二、restful; 三、jackjson支持; 四、aop ........ -->
15         <dependency>
16             <groupId>org.springframework.boot</groupId>
17             <artifactId>spring-boot-starter-web</artifactId>
18         </dependency>
19 
20         <!-- servlet 依賴. -->
21         <dependency>
22             <groupId>javax.servlet</groupId>
23             <artifactId>javax.servlet-api</artifactId>
24         </dependency>
25 
26         <dependency>
27             <groupId>javax.servlet</groupId>
28             <artifactId>jstl</artifactId>
29         </dependency>
30 
31         <!-- tomcat 的支持. -->
32         <dependency>
33             <groupId>org.springframework.boot</groupId>
34             <artifactId>spring-boot-starter-tomcat</artifactId>
35         </dependency>
36         <dependency>
37             <groupId>org.apache.tomcat.embed</groupId>
38             <artifactId>tomcat-embed-jasper</artifactId>
39         </dependency>
40     </dependencies>
41 
42     <build>
43         <finalName>springboot_jsp</finalName>
44         <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
45             <plugins>
46                 <plugin>
47                     <artifactId>maven-clean-plugin</artifactId>
48                     <version>3.1.0</version>
49                 </plugin>
50                 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
51                 <plugin>
52                     <artifactId>maven-resources-plugin</artifactId>
53                     <version>3.0.2</version>
54                 </plugin>
55                 <plugin>
56                     <artifactId>maven-compiler-plugin</artifactId>
57                     <version>3.8.0</version>
58                 </plugin>
59                 <plugin>
60                     <artifactId>maven-surefire-plugin</artifactId>
61                     <version>2.22.1</version>
62                 </plugin>
63                 <plugin>
64                     <artifactId>maven-war-plugin</artifactId>
65                     <version>3.2.2</version>
66                 </plugin>
67                 <plugin>
68                     <artifactId>maven-install-plugin</artifactId>
69                     <version>2.5.2</version>
70                 </plugin>
71                 <plugin>
72                     <artifactId>maven-deploy-plugin</artifactId>
73                     <version>2.8.2</version>
74                 </plugin>
75             </plugins>
76         </pluginManagement>
77     </build>

3.在resources中添加文件 application.properties  配置視圖解析器

1 # 頁面默認前綴目錄
2 spring.mvc.view.prefix=/WEB-INF/jsp/
3 # 響應頁面默認後綴
4 spring.mvc.view.suffix=.jsp

 4.經過controller層訪問

1 @Controller
2 public class JspController {
3 
4     @RequestMapping("/index")
5     public String index(){
6         return "index";
7     }
8 }

5.webapp

 

 6.修改 working directory配置

 

 

 5、springBoot+freemarker模板技術

 1.利用maven構建webapp模塊

 2.配置pom.xml  注意修改mainClass路徑

 1 <properties>
 2         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 3         <maven.compiler.source>1.8</maven.compiler.source>
 4         <maven.compiler.target>1.8</maven.compiler.target>
 5     </properties>
 6 
 7     <dependencies>
 8         <dependency>
 9             <groupId>junit</groupId>
10             <artifactId>junit</artifactId>
11             <version>4.11</version>
12             <scope>test</scope>
13         </dependency>
14         <dependency>
15             <groupId>org.springframework.boot</groupId>
16             <artifactId>spring-boot-starter-web</artifactId>
17         </dependency>
18         <!-- 對freemark的支持-->
19         <dependency>
20             <groupId>org.springframework.boot</groupId>
21             <artifactId>spring-boot-starter-freemarker</artifactId>
22         </dependency>
23     </dependencies>
24 
25     <build>
26         <plugins>
27             <plugin>
28                 <groupId>org.apache.maven.plugins</groupId>
29                 <artifactId>maven-compiler-plugin</artifactId>
30                 <configuration>
31                     <source>1.8</source>
32                     <target>1.8</target>
33                     <encoding>utf-8</encoding>
34                 </configuration>
35             </plugin>
36             <plugin>
37                 <groupId>org.springframework.boot</groupId>
38                 <artifactId>spring-boot-maven-plugin</artifactId>
39                 <configuration>
40                     <mainClass>cn.su.FreemarkApp</mainClass> <!--主類 包含main-->
41                     <layout>JAR</layout>
42                 </configuration>
43             </plugin>
44         </plugins>
45     </build>

3.在  main/resources/templates 中創建  index.ftl模板  注意templates 有 s

 1 <!DOCTYPE html>
 2 <html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"
 3       xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
 4 <head>
 5     <title>Hello World!</title>
 6 </head>
 7 <body>
 8 <h1>Hello , ${msg}</h1>
 9 </body>
10 </html>

4.爲模板配置屬性文件  application.properties

 1 # FreeeMarker 模板引擎配置
 2 #       設定ftl文件路徑   注意templates有 s
 3 spring.freemarker.tempalte-loader-path=classpath:/templates
 4 #        關閉緩存,及時刷新,上線生產環境須要修改成true
 5 spring.freemarker.cache=false
 6 spring.freemarker.charset=UTF-8
 7 spring.freemarker.check-template-location=true
 8 spring.freemarker.content-type=text/html
 9 spring.freemarker.expose-request-attributes=true
10 spring.freemarker.expose-session-attributes=true
11 spring.freemarker.request-context-attribute=request
12 spring.freemarker.suffix=.ftl

5.啓動類和controller

 1 @SpringBootApplication
 2 public class FreemarkApp {
 3 
 4     public static void main(String[] args) {
 5         SpringApplication.run(FreemarkApp.class);
 6     }
 7 }
 8 
 9 
10 @Controller
11 public class IndexController {
12 
13     @RequestMapping("/index")
14     public String index(Model model){
15         model.addAttribute("msg", "suSu");
16         return "index";
17     }
18 }

6.也能夠支持返回JSON格式    時間格式在get上配置  @JsonFormat(pattern = "yyyy-MM-dd",timezone = "GTM+8")

 1 @Controller
 2 public class JsonController {
 3 
 4     @RequestMapping("/user")
 5     @ResponseBody
 6     public List<User> list(){
 7         List<User> users = Arrays.asList(new User(1L, "s", new Date()),
 8                 new User(2L, "u", new Date()),
 9                 new User(3L, "q", new Date()));
10         return users;
11     }
12 }

6、支持yaml

 YAML是便於人閱讀基於unicode編碼的各類語言的序列號標準。它的用途普遍,用於配置文件,日誌文件,跨語言數據共享,對象持久化,複雜的數據結構。

原則:

一、大小寫敏感

二、使用縮進表示層級關係

四、縮進長度沒有限制,只要元素對齊就表示這些元素屬於一個層級。

五、使用#表示註釋

六、字符串能夠不用引號標註

語法:

 使用  :表示鍵值對   age  :18

7、springBoot+springJDBC

1.利用maven構建webapp項目

2.配置pom.xml

 1 <properties>
 2         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 3         <maven.compiler.source>1.8</maven.compiler.source>
 4         <maven.compiler.target>1.8</maven.compiler.target>
 5     </properties>
 6 
 7     <dependencies>
 8         <dependency>
 9             <groupId>junit</groupId>
10             <artifactId>junit</artifactId>
11             <version>4.12</version>
12         </dependency>
13         <!-- web test springjdbc mysql-->
14         <dependency>
15             <groupId>org.springframework.boot</groupId>
16             <artifactId>spring-boot-starter-web</artifactId>
17         </dependency>
18         <dependency>
19             <groupId>org.springframework.boot</groupId>
20             <artifactId>spring-boot-starter-test</artifactId>
21         </dependency>
22 
23         <dependency>
24             <groupId>mysql</groupId>
25             <artifactId>mysql-connector-java</artifactId>
26         </dependency>
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-jdbc</artifactId>
30         </dependency>
31     </dependencies>
32 
33     <build>
34         <finalName>springboot_springjdbc</finalName>
35         <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
36             <plugins>
37                 <plugin>
38                     <artifactId>maven-clean-plugin</artifactId>
39                     <version>3.1.0</version>
40                 </plugin>
41                 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
42                 <plugin>
43                     <artifactId>maven-resources-plugin</artifactId>
44                     <version>3.0.2</version>
45                 </plugin>
46                 <plugin>
47                     <artifactId>maven-compiler-plugin</artifactId>
48                     <version>3.8.0</version>
49                 </plugin>
50                 <plugin>
51                     <artifactId>maven-surefire-plugin</artifactId>
52                     <version>2.22.1</version>
53                 </plugin>
54                 <plugin>
55                     <artifactId>maven-war-plugin</artifactId>
56                     <version>3.2.2</version>
57                 </plugin>
58                 <plugin>
59                     <artifactId>maven-install-plugin</artifactId>
60                     <version>2.5.2</version>
61                 </plugin>
62                 <plugin>
63                     <artifactId>maven-deploy-plugin</artifactId>
64                     <version>2.8.2</version>
65                 </plugin>
66             </plugins>
67         </pluginManagement>
68     </build>

3.利用application.properties鏈接數據庫

1 spring.datasource.url = jdbc:mysql://localhost:3306/shop
2 spring.datasource.username = root
3 spring.datasource.password = 000000
4 spring.datasource.driverClassName = com.mysql.jdbc.Driver
5 spring.datasource.max-active=20
6 spring.datasource.max-idle=8
7 spring.datasource.min-idle=8
8 spring.datasource.initial-size=10

4.接着就是咱們熟悉的domain、dao、service(事物只用配置註解  @Transactional)、controller層

5.啓動項和SpringBoot測試類,注意須要與domain寫在同一層 ,不然須要作些其餘配置

 1 //啓動
 2 @SpringBootApplication
 3 public class JDBCApp {
 4     public static void main(String[] args) {
 5         SpringApplication.run(JDBCApp.class);
 6     }
 7 }
 8 
 9 //測試
10 @RunWith(SpringRunner.class)
11 @SpringBootTest(classes = JDBCApp.class)
12 public class JDBCTest {
13 
14     @Autowired
15     private IUserService userService;
16 
17     @Test
18     public void test() throws Exception{
19         userService.save(new User(1L,"su"));
20     }
21 }

8、springBoot+mybatis及pageHelper分頁

1.利用maven構建webapp模塊

2.配置pom.xml

 1 <properties>
 2         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 3         <maven.compiler.source>1.8</maven.compiler.source>
 4         <maven.compiler.target>1.8</maven.compiler.target>
 5     </properties>
 6 
 7     <dependencies>
 8         <dependency>
 9             <groupId>org.springframework.boot</groupId>
10             <artifactId>spring-boot-starter-web</artifactId>
11         </dependency>
12         <dependency>
13             <groupId>org.springframework.boot</groupId>
14             <artifactId>spring-boot-starter-test</artifactId>
15         </dependency>
16         <dependency>
17             <groupId>junit</groupId>
18             <artifactId>junit</artifactId>
19             <version>4.12</version>
20         </dependency>
21         <!-- mysql 數據庫驅動. -->
22         <dependency>
23             <groupId>mysql</groupId>
24             <artifactId>mysql-connector-java</artifactId>
25         </dependency>
26         <!--
27                     spring-boot mybatis依賴:
28 
29                     請不要使用1.0.0版本,由於還不支持攔截器插件,
30                 1.1.1 是博主寫帖子時候的版本,你們使用最新版本便可
31                  -->
32         <dependency>
33             <groupId>org.mybatis.spring.boot</groupId>
34             <artifactId>mybatis-spring-boot-starter</artifactId>
35             <version>1.1.1</version>
36         </dependency>
37         <!--
38             MyBatis提供了攔截器接口,咱們能夠實現本身的攔截器,
39             將其做爲一個plugin裝入到SqlSessionFactory中。
40                 Github上有位開發者寫了一個分頁插件,我以爲使用起來還能夠,挺方便的。
41                 Github項目地址: https://github.com/pagehelper/Mybatis-PageHelper
42              -->
43         <dependency>
44             <groupId>com.github.pagehelper</groupId>
45             <artifactId>pagehelper</artifactId>
46             <version>4.1.0</version>
47         </dependency>
48     </dependencies>
49 
50     <build>
51         <finalName>springboot_mybatis</finalName>
52         <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
53             <plugins>
54                 <plugin>
55                     <artifactId>maven-clean-plugin</artifactId>
56                     <version>3.1.0</version>
57                 </plugin>
58                 <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
59                 <plugin>
60                     <artifactId>maven-resources-plugin</artifactId>
61                     <version>3.0.2</version>
62                 </plugin>
63                 <plugin>
64                     <artifactId>maven-compiler-plugin</artifactId>
65                     <version>3.8.0</version>
66                 </plugin>
67                 <plugin>
68                     <artifactId>maven-surefire-plugin</artifactId>
69                     <version>2.22.1</version>
70                 </plugin>
71                 <plugin>
72                     <artifactId>maven-war-plugin</artifactId>
73                     <version>3.2.2</version>
74                 </plugin>
75                 <plugin>
76                     <artifactId>maven-install-plugin</artifactId>
77                     <version>2.5.2</version>
78                 </plugin>
79                 <plugin>
80                     <artifactId>maven-deploy-plugin</artifactId>
81                     <version>2.8.2</version>
82                 </plugin>
83             </plugins>
84         </pluginManagement>
85     </build>

3.鏈接數據庫配置  application.properties

 

 1 spring.datasource.url = jdbc:mysql://localhost:3306/shop
 2 spring.datasource.username = root
 3 spring.datasource.password = 000000
 4 spring.datasource.driverClassName = com.mysql.jdbc.Driver
 5 spring.datasource.max-active=20
 6 spring.datasource.max-idle=8
 7 spring.datasource.min-idle=8
 8 spring.datasource.initial-size=10
 9 #爲mapper使用domain配置別名
10 mybatis.type-aliases-package=cn.su.domain

4.省略domain、mapper、service、controller層

注意mapper的java文件與resources下的mapper的配置文件須要徹底對應

5.配置分頁插件和使用

 1 @Configuration //至關於application-Xxx.xml
 2 public class MyBatisConfiguration {
 3     //至關於配置文件裏面配置<bean>
 4     @Bean
 5     public PageHelper pageHelper(){
 6         PageHelper pageHelper = new PageHelper();
 7         Properties p = new Properties();
 8         //配置分頁的參數設置
 9         //和startPage中的pageNum效果同樣
10         p.setProperty("offsetAsPageNum", "true");
11         //設置爲true時,使用RowBounds分頁會進行count查詢
12         p.setProperty("rowBoundsWithCount", "true");
13         // 啓用合理化時,若是pageNum<1會查詢第一頁,若是pageNum>pages會查詢最後一頁
14         p.setProperty("reasonable", "true");
15         pageHelper.setProperties(p);
16         return pageHelper;
17     }
18 }
19 
20 
21 //使用
22 //1表示當前頁  2表示條數
23  PageHelper.startPage(1, 2);

6.啓動類和測試

 1 //啓動  須要掃描mapper
 2 @SpringBootApplication
 3 @MapperScan(value = "cn.su.mapper")
 4 public class MyBatisApp {
 5 
 6     public static void main(String[] args) {
 7         SpringApplication.run(MyBatisApp.class);
 8     }
 9 }
10 
11 
12 //測試
13 @RunWith(SpringRunner.class)
14 @SpringBootTest(classes = MyBatisApp.class)
15 public class MybatisTest {
16 
17     @Autowired
18     private IUserService userService;
19 
20     @Test
21     public void test1() throws Exception{
22         userService.saveByInsert(new User(2L,"W"));
23     }
24     @Test
25     public void test2() throws Exception{
26         userService.saveByXml(new User(3L,"Q"));
27     }
28     @Test
29     public void test3() throws Exception{
30         List<User> users = userService.findByQuery();
31         users.forEach(e->{
32             System.out.println(e);
33         });
34     }
35 }
相關文章
相關標籤/搜索