package com.lylboot.firstBoot.service.impl;
import java.util.List;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.lylboot.firstBoot.Application;
import com.lylboot.firstBoot.bo.Student;
import com.lylboot.firstBoot.controller.StudentMybatisController;
import com.lylboot.firstBoot.dao.StudentDao;
import com.lylboot.firstBoot.service.StudentServiceMy;
/**
* spring boot 单元测试 分别对 dao service controller 层进行测试
* @author lylRi
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes= Application.class)// 启动spring boot类 run
public class StudentServiceTest {
@Test
public void testDao() {
System.out.println(studentDao.count());
Assert.assertEquals(2, studentDao.count());
}
@Autowired
private StudentDao studentDao;
@Autowired
private StudentServiceMy studentServiceMy;
@Test
public void testService(){
List<Student> stus = studentServiceMy.getStudents();
Assert.assertEquals(2, stus.size());
}
private MockMvc mvc;
@Autowired
private StudentMybatisController studentMybatisController;
@Before
public void setUp(){
// mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); 项目拦截器有效
mvc = MockMvcBuilders.standaloneSetup(studentMybatisController).build();//对单个controller
}
@Test
public void getStudentController(){
String str = "/studentMy/get";
try {
mvc.perform(MockMvcRequestBuilders.get(str).accept(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*RequestBuilder request = MockMvcRequestBuilders.get(str)
.contentType(MediaType.APPLICATION_JSON);
try {
MvcResult mvcResult = mvc.perform(request).andReturn();
System.out.println(mvcResult.getResponse().getContentAsString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}*/
}
}
因现在使用spring家族系的功能越来越多,常用的spring springMVC mybatis等框架带来的配置文件也越多,极不方便;因 约定优先配置 的思想
正是这种背景下 spring boot 孕育而生,它本身并不能替代spring 核心功能及扩展,而是结合spring 及集成了很多常用的第三方库配置,这些第三方库开箱即用,只需简单的配置,这样开发者主要关注于业务逻辑。
传统基于spring Javaweb 应用,我们需要配置web.xml 及applicationContext.xml 等 打包成war 部署在应用服务器eg:Tomcat中。
使用spring boot
采用 maven 构建一个工程 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>com.lylboot</groupId>
<artifactId>firstBoot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<!-- Additional lines to be added here... -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package com.lylboot.firstBoot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Configuration
@EnableAutoConfiguration
@RestController
public class Application {
@RequestMapping("/")
String home(){
return "hello world";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
使用内嵌的Tomcat
最简单例子好了;
@SpringBootApplication
代替
@Configuration
@EnableAutoConfiguration
@ComponentScan
--------------------------------------------------
@ComponentScan 默认扫描路径 main所在的类包下的注解 eg: @RestController 等 如果不在main方法类包路径下下,你需要指定 通过 @ComponentScan("你的包路径")
如果指定路径则默认的不起作用,都需要手动加上。
controller中的使用方式与spring mvc 一样
--------------------------------------------------------------添加jdbc 访问MySQL数据库
pom.xml 添加配置:
在上面基础上添加
<!-- MYSQL -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- Spring Boot JDBC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
application.properties
spring.datasource.url=jdbc\:mysql\://10.188.53.246\:3306/test?useUnicode\=true&characterEncoding\=utf-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
spring.datasource.validation-query=SELECT 1
spring.datasource.test-on-borrow=false
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=18800
spring.datasource.jdbc-interceptors=ConnectionState;SlowQueryReport(threshold=0)
基于上面的pom
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
启动main 添加扫描
@MapperScan("com.lylboot.firstBoot.dao")
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.lylboot.boot,com.lylboot.firstBoot.*")
@MapperScan("com.lylboot.firstBoot.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
最后单元测试:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>