SpringBoot能夠很簡單的創建一個直接運行的單體Spring應用
特性:
- 單體Spring應用
- 內置的tomcat、Jetty
- 提供默認的starter來構建配置
- 自動配置Spring和第三方庫
推薦一個很好的學習教程,https://blog.csdn.net/u010486495/article/details/79348302
1 構建hello world工程
MAVEN構建
- pom.xml
parent、starter、test、web
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.4.RELEASE</version><relativePath /> <!-- lookup parent from repository --></parent><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></dependencies>
構建一個簡單的hello world工程就遇到了兩個坑,第一個springboot只會掃描啟動類所在包的類和子包的類;
第二個是寫測試類的時候發現@SpringApplicationConfiguration這個注解已經被取消掉了,使用@SpringBootTest這個注解
- 屬性文件
在src/main/resources目錄創建一個application.properties
-
定義Controller
package com.didispace.web;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@RequestMapping("/hello")public String index() {return "Hello World";}}
- 啟動類
package com.didispace;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Chapter1Application {public static void main(String[] args) {SpringApplication.run(Chapter1Application.class, args);}
}
- 測試
package com.didispace;import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;import com.didispace.web.HelloController;@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MockServletContext.class)
@WebAppConfiguration
public class Chapter1ApplicationTests {private MockMvc mvc;@Beforepublic void setUp() throws Exception {mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();}@Testpublic void getHello() throws Exception {mvc.perform(MockMvcRequestBuilders.get("/hello").accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().string(equalTo("Hello World")));System.out.println("OK");}}
2 屬性、隨機數、多環境配置
屬性文件application.properties
在application.properties中定義隨機數:net.csdn.chapter1.number=${random.int}
多環境配置:在application.properties中定義spring.profiles.active=prod,那么會加載application-prod.properties文件中配置的屬性
3 創建一套簡單的restfulapi,單元測試
- User.java
package cc.bean;public class User {private Long id;private String name;private Integer age;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}
- UserController.java
package cc.controller;import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import cc.bean.User;@RestController
@RequestMapping(path="/users")
public class UserController {static Map<Long,User> userMap = Collections.synchronizedMap(new HashMap<Long,User>());@RequestMapping(path="/",method=RequestMethod.GET)public List<User> getUserList(){List<User> userList = new ArrayList<User>(userMap.values());return userList;}@RequestMapping(path="/",method=RequestMethod.POST)public String addUser(@ModelAttribute User user){userMap.put(user.getId(), user);return "success";}@RequestMapping(path="/{id}",method=RequestMethod.GET)public User getUser(@PathVariable Long id){return userMap.get(id);}@RequestMapping(path="/{id}",method=RequestMethod.PUT)public String addUser(@PathVariable Long id,@ModelAttribute User user){User u = userMap.get(user.getId());u.setName(user.getName());u.setAge(user.getAge());userMap.put(u.getId(), u);return "success";}@RequestMapping(path="/{id}",method=RequestMethod.DELETE)public String delUser(@PathVariable Long id){userMap.remove(id);return "success";}}
- UserControllerTest.java
import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;import cc.Chapter2Application;
import cc.controller.UserController;@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Chapter2Application.class)
@WebAppConfiguration
public class UserControllerTest {private MockMvc mvc;@Beforepublic void setUp() {mvc = MockMvcBuilders.standaloneSetup(new UserController()).build();}@Testpublic void testUserController() throws Exception {
// 測試UserControllerRequestBuilder request = null;// 1、get查一下user列表,應該為空request = get("/users/");mvc.perform(request).andExpect(status().isOk()).andExpect(content().string(equalTo("[]")));// 2、post提交一個userrequest = post("/users/").param("id", "1").param("name", "測試大師").param("age", "20");mvc.perform(request)
// .andDo(MockMvcResultHandlers.print()).andExpect(content().string(equalTo("success")));// 3、get獲取user列表,應該有剛才插入的數據request = get("/users/");mvc.perform(request).andExpect(status().isOk()).andExpect(content().string(equalTo("[{\"id\":1,\"name\":\"測試大師\",\"age\":20}]")));// 4、put修改id為1的userrequest = put("/users/1").param("name", "測試終極大師").param("age", "30");mvc.perform(request).andExpect(content().string(equalTo("success")));// 5、get一個id為1的userrequest = get("/users/1");mvc.perform(request).andExpect(content().string(equalTo("{\"id\":1,\"name\":\"測試終極大師\",\"age\":30}")));// 6、del刪除id為1的userrequest = delete("/users/1");mvc.perform(request).andExpect(content().string(equalTo("success")));// 7、get查一下user列表,應該為空request = get("/users/");mvc.perform(request).andExpect(status().isOk()).andExpect(content().string(equalTo("[]")));}
}
除了可以用JUNIT做單元測試外,還可以用火狐瀏覽器Rested Client插件發送http請求。
4?springboot-mybatis簡單整合
- pom.xml
<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>springboot</groupId><artifactId>springboot-mybatis</artifactId><version>0.0.1-SNAPSHOT</version><description>springboot-mybatis整合</description><!-- Spring Boot 啟動父依賴 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.1.RELEASE</version></parent><dependencies><!-- Spring Boot Web 依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Spring Boot Test 依賴 --><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-devtools</artifactId><optional>true</optional></dependency><!-- Spring Boot Mybatis 依賴 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.2.0</version></dependency><!-- MySQL 連接驅動依賴 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency></dependencies>
</project>
- application.properties
## 數據源配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=a123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver## Mybatis 配置
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml
-
CityMapper.xml
mybatis映射文件,在src\main\resources\mapper目錄創建一個CityMapper.xml文件
使用逆向工程生成代碼
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="org.spring.springboot.dao.CityDao"><resultMap id="BaseResultMap" type="org.spring.springboot.domain.City"><result column="id" property="id" /><result column="province_id" property="provinceId" /><result column="city_name" property="cityName" /><result column="description" property="description" /></resultMap><parameterMap id="City" type="org.spring.springboot.domain.City"/><sql id="Base_Column_List">id, province_id, city_name, description</sql><select id="findByName" resultMap="BaseResultMap" parameterType="java.lang.String">select<include refid="Base_Column_List" />from citywhere city_name like '%${cityName}%'</select></mapper>
- entity、dao、service、controller
package org.spring.springboot.domain;/*** 城市實體類**/
public class City {/*** 城市編號*/private Long id;/*** 省份編號*/private Long provinceId;/*** 城市名稱*/private String cityName;/*** 描述*/private String description;public Long getId() {return id;}public void setId(Long id) {this.id = id;}public Long getProvinceId() {return provinceId;}public void setProvinceId(Long provinceId) {this.provinceId = provinceId;}public String getCityName() {return cityName;}public void setCityName(String cityName) {this.cityName = cityName;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}
}
package org.spring.springboot.dao;import org.apache.ibatis.annotations.Param;
import org.spring.springboot.domain.City;/*** 城市 DAO 接口類**/
public interface CityDao {/*** 根據城市名稱,查詢城市信息** @param cityName 城市名*/City findByName(@Param("cityName") String cityName);
}
package org.spring.springboot.service;import org.spring.springboot.domain.City;/*** 城市業務邏輯接口類**/
public interface CityService {/*** 根據城市名稱,查詢城市信息* @param cityName*/City findCityByName(String cityName);
}
package org.spring.springboot.service.impl;import org.spring.springboot.dao.CityDao;
import org.spring.springboot.domain.City;
import org.spring.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** 城市業務邏輯實現類**/
@Service
public class CityServiceImpl implements CityService {@Autowiredprivate CityDao cityDao;public City findCityByName(String cityName) {return cityDao.findByName(cityName);}}
package org.spring.springboot.controller;import org.spring.springboot.domain.City;
import org.spring.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@RestController
public class CityRestController {@Autowiredprivate CityService cityService;@RequestMapping(path="/api/city",method=RequestMethod.GET)public City findCityByName(@RequestParam(value="cityName",required=true) String cityName){return cityService.findCityByName(cityName);}@RequestMapping(path="/test/hotswapp")public String testHotSwapping(){return "測試熱交換";}}
- 啟動類
package org.spring.springboot;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@MapperScan("org.spring.springboot.dao")
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}
}
- db
/*
Navicat MySQL Data TransferSource Server : 本地實例
Source Server Version : 50528
Source Host : localhost:3306
Source Database : springbootdbTarget Server Type : MYSQL
Target Server Version : 50528
File Encoding : 65001Date: 2020-08-08 10:51:48
*/SET FOREIGN_KEY_CHECKS=0;-- ----------------------------
-- Table structure for city
-- ----------------------------
DROP TABLE IF EXISTS `city`;
CREATE TABLE `city` (`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市編號',`province_id` int(10) unsigned NOT NULL COMMENT '省份編號',`city_name` varchar(25) DEFAULT NULL COMMENT '城市名稱',`description` varchar(25) DEFAULT NULL COMMENT '描述',PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;-- ----------------------------
-- Records of city
-- ----------------------------
INSERT INTO `city` VALUES ('1', '1', '郭如飛的家', '郭如飛的家在武漢。');
?
問題總結
-
application.properties屬性文件中文亂碼問題,點擊這里,親測可用。