注意:本文中涉及代码已放到github上,有需要的请自行下载。
https://github.com/vanroid/springboot-demo.git
Spring boot 产生自Spring的4.0版,解决传统Spring极其繁琐的配置,Spring boot 最大化地实现了约定大于配置,只需要通过简单的properties配置即可。Spring boot内置了tomcat、Jetty容器,无需打包成WAR包即可部署。对Maven和Gradle提供了强大的支持,构建应用不需要写脚本。运行使用java -jar形式的Java应用程序即可,集成部署快。综合来说,Spring boot 简化了Spring的配置、构建、部署等操作,极大的实现了自动化。
其缺点是由于封装,不如Spring xml配置灵活,版本更新快,版本之间有部分不兼容的问题。
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.0modelVersion>
<groupId>springbootgroupId>
<artifactId>springboot-mybatisartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springboot-mybatis :: 整合 Mybatis Demoname>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.1.RELEASEversion>
parent>
<properties>
<mybatis-spring-boot>1.2.0mybatis-spring-boot>
<mysql-connector>5.1.39mysql-connector>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis-spring-boot}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql-connector}version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
project>
由于Spring boot 很多配置都是通过约定好的,因此只要依赖了org.mybatis.spring.boot
包后,只需要配置一下实体目录和映射文件目录即可完成mybatis集成。
application.properties代码:
## 数据源配置
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml
与传统的spring单元测试一样,需要使用SpringJUnit4ClassRunner来执行,此外还需要指定StringBoot的执行类
@SpringBootTest(classes = Application.class)
, 具体代码如下:
package org.spring.springboot;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.spring.springboot.domain.City;
import org.spring.springboot.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class CityServiceTest {
@Autowired
private CityService cityService;
@Test
public void test(){
City city = cityService.findCityByName("温岭市");
Assert.assertNotNull(city);
}
}
对于Web项目,可以直接通过网页、浏览器的形式没测试WebConctroller,而对于Restful API的Conctroller则需要第三方工具。为了简化这个过程,Spring测试框架提供了MockMvc对象,可以不需要客户端-服务端请求情况下进行MVC测试 ,完全在服务端执行Conctroller执行请求,模拟了服务器真实的运行情况。
在Conctroller上可以使用@RestConctroller
注解来替代Conctroller注解。
package org.spring.springboot.controller;
import org.spring.springboot.domain.User;
import org.spring.springboot.service.UserService;
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.RestController;
@RestController
@RequestMapping("user")
public class UserConctroller {
@Autowired
private UserService userService;
@RequestMapping(value = "get", method = RequestMethod.GET)
public User getUser(Long id) {
User user = userService.findById(id);
return user;
}
}
mock测试代码
package org.spring.springboot;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.spring.springboot.controller.UserConctroller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
public class UserConctrollerMockTest {
@Autowired
private UserConctroller userConctroller;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.standaloneSetup(userConctroller).build();
}
@Test
public void testGetUser() {
try {
ResultActions result = mockMvc.perform(MockMvcRequestBuilders.get("/user/get?id=1"));
String res = result.andReturn().getResponse().getContentAsString();
System.out.println(res);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Spring boot 可以将项目打包成jar,使用java -jar即可运行。
打包命令为:
mvn clean package
如果运行时提示:springboot-mybatis-0.0.1-SNAPSHOT.jar中没有主清单属性,是maven默认的打包方法没有把主类标识打包进去,需要添加一个spring-boot-maven-plugin springboot专用的maven打包插件。
pom文件添加配置:
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
再次打包即可运行。