一、包结构
二、添加相关依赖
三个依赖:JPA、Web、hsqldb(内存数据库)
<?xml version="1.0" encoding="UTF-8"?>
<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.yan</groupId>
<artifactId>otlan</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>otlan</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
三、业务代码
实体类
package com.yan.otlan.sys.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person {
@Id
@GeneratedValue
private Long id;
private String name;
public Person() {
super();
}
public Person(String name) {
super();
this.name = name;
}
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;
}
}
数据访问
package com.yan.otlan.sys.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import com.yan.otlan.sys.domain.Person;
public interface PersonRepository extends JpaRepository<Person, Long> {
}
控制器
package com.yan.otlan.sys.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.yan.otlan.sys.dao.PersonRepository;
import com.yan.otlan.sys.domain.Person;
@RestController
@RequestMapping("/person")
public class PersonController {
@Autowired
PersonRepository personRepository;
@RequestMapping(method = RequestMethod.GET,produces = {MediaType.APPLICATION_JSON_VALUE} )
public List<Person> findAll(){
return personRepository.findAll();
}
@RequestMapping("/add")
public List<Person> add() {
Person person = new Person();
person.setName("zhangsan");
personRepository.save(person);
return personRepository.findAll();
}
}
四、测试用例
package com.yan.otlan;
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.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yan.otlan.sys.dao.PersonRepository;
import com.yan.otlan.sys.domain.Person;
@RunWith(SpringRunner.class)
@SpringBootTest
public class OtlanApplicationTests {
@Autowired
PersonRepository personRepository;
MockMvc mvc;
@Autowired
WebApplicationContext webApplicationContext;
String expectedJson;
@Before //3
public void setUp() throws JsonProcessingException{
Person p1 = new Person("wyf");
Person p2 = new Person("wisely");
personRepository.save(p1);
personRepository.save(p2);
expectedJson =Obj2Json(personRepository.findAll()); //4
mvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
protected String Obj2Json(Object obj) throws JsonProcessingException{//5
ObjectMapper mapper = new ObjectMapper();
return mapper.writeValueAsString(obj);
}
@Test
public void testPersonController() throws Exception {
String uri="/person";
MvcResult result = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON))
.andReturn(); //6
int status = result.getResponse().getStatus(); //7
String content = result.getResponse().getContentAsString(); //8
System.out.println(content);
Assert.assertEquals("错误,正确的返回值为200",200, status); //9
Assert.assertEquals("错误,返回值和预期返回值不一致", expectedJson,content); //10
}
}
运行结果