SpringBoot是Spring項目中的一個子工程。主要做用就是幫助我能快速構建龐大的spring項目並儘量的減小一切的xml配置java
案例:經過Springboot整合SpringMVC 和mybatismysql
1.建立工程,添加父工程座標和啓動器和所需的依賴 jdk版本管理等 完整pom.xmlweb
1 <?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.leyou.demo</groupId> 8 <artifactId>springboot-demo</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <java.version>1.8</java.version> 13 </properties> 14 15 <parent> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-parent</artifactId> 18 <version>2.0.4.RELEASE</version> 19 </parent> 20 21 <dependencies> 22 <!--web啓動器--> 23 <dependency> 24 <groupId>org.springframework.boot</groupId> 25 <artifactId>spring-boot-starter-web</artifactId> 26 </dependency> 27 <!--druid不須要, 用springboot自帶的HiKariCP更優秀--> 28 <!--<dependency>--> 29 <!--<groupId>com.alibaba</groupId>--> 30 <!--<artifactId>druid</artifactId>--> 31 <!--<version>1.1.6</version>--> 32 <!--</dependency>--> 33 <!--mybatis 啓動器 是mybatis本身寫的 --> 34 <dependency> 35 <groupId>org.mybatis.spring.boot</groupId> 36 <artifactId>mybatis-spring-boot-starter</artifactId> 37 <version>1.3.2</version> 38 </dependency> 39 <!--鏈接數據庫的驅動--> 40 <dependency> 41 <groupId>mysql</groupId> 42 <artifactId>mysql-connector-java</artifactId> 43 </dependency> 44 <!--jdbc事務管理相關--> 45 <dependency> 46 <groupId>org.springframework.boot</groupId> 47 <artifactId>spring-boot-starter-jdbc</artifactId> 48 </dependency> 49 </dependencies> 50 </project>
2:編寫啓動類 類名能夠任意(後面的全部java代碼都要和啓動類在同一個包或這包的子類包中)spring
3. 編寫controllersql
1 package com.practice.controller; 2 3 4 import com.practice.pojo.User; 5 import com.practice.service.UserService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.web.bind.annotation.GetMapping; 8 import org.springframework.web.bind.annotation.PathVariable; 9 import org.springframework.web.bind.annotation.RestController; 10 11 12 @RestController //表示該類下的因此方法的返回值所有用json數據類型接收 13 public class HelloController { 14 15 @Autowired 16 private UserService userService; 17 18 19 @GetMapping("hello1/{id}") 20 public User hello(@PathVariable("id") Long id) { 21 User user = userService.queryUserById(id); 22 return user; 23 } 24 }
4. 建立service 和userMapper (user對象要建立)數據庫
1 import com.practice.pojo.User; 2 3 public interface UserService { 4 User queryUserById(Long id); 5 } 6 7 import com.practice.mapper.UserMapper; 8 import com.practice.pojo.User; 9 import com.practice.service.UserService; 10 import org.springframework.beans.factory.annotation.Autowired; 11 import org.springframework.stereotype.Service; 12 13 @Service("userService") 14 public class UserServiceImpl implements UserService { 15 @Autowired 16 private UserMapper userMapper; 17 @Override 18 public User queryUserById(Long id) { 19 return userMapper.queryUserById(id); 20 } 21 } 22 23 24 import com.practice.pojo.User; 25 import org.apache.ibatis.annotations.Param; 26 27 public interface UserMapper { 28 /** 29 * 經過id查詢用戶 30 * @param id 31 * @return 32 */ 33 User queryUserById(@Param("id") Long id); 34 }
5. 建立application.yml這是SoringBoot的默認讀取的屬性文件名(也能夠是application.properties)apache
6. 建立UserMapper.xmljson
而後測試:運行application mian函數springboot
結果mybatis
**訪問靜態資源:
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public
只要靜態資源放在這些目錄中任何一個,SpringMVC都會幫咱們處理。
例如:
訪問結果
還有就是上面的註解做用能夠本身再翻源碼瞭解瞭解!