Spring-boot官方案例分析之data-jpa
Spring-boot官方案例分析之data-jpa
package sample.data.jpa;
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.SpringApplicationConfiguration;
import org.springframework.test.context.ActiveProfiles;
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.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
* Integration test to run the
application.
*
* @author Oliver
Gierke
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class)
@WebAppConfiguration
@ActiveProfiles("scratch")
// Separate profile for web tests to avoid clashing databases
public class SampleDataJpaApplicationTests {
@Autowired
private WebApplicationContext
context;
private MockMvc mvc;
@Before
public void setUp() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
@Test
public void testHome() throws Exception
{
this.mvc.perform(get("/")).andExpect(status().isOk())
.andExpect(content().string("Bath"));
}
}
- 首先测试类中选择了要使用的配置文件
@ActiveProfiles(“scratch”)
对应的properties为application-scratch.properties
内容为:
spring.datasource.url: jdbc:hsqldb:mem:scratchdb
定义了数据源的url;
@Autowired
private WebApplicationContext context;
注入应用上下文context;
定义MockMvc,然后@Before注解执行初始化容器
@Before
public void setUp() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
}
然后模拟发送请求测试:
类图关系:
根据这个测试用例来走一遍请求处理过程:
this.mvc.perform(get("/")).andExpect(status().isOk())
.andExpect(content().string("Bath"));
get请求处理:到SampleController
@Autowired
private CityService cityService; @RequestMapping("/")
@ResponseBody
@Transactional(readOnly = true)
public String helloWorld() {
return this.cityService.getCity("Bath", "UK").getName();
}
}
注入了CityService组件属性,事务类型为只读。
然后执行服务组件CityService中的getCity()方法;
并且传入参数name=”Bath”,country=”UK”,然后调用getname方法获取name值。
public interface CityService {
Page<City> findCities(CitySearchCriteria criteria, Pageable pageable);
City getCity(String name, String country);
Page<HotelSummary> getHotels(City city, Pageable pageable);
}
该接口中定义了查询方法;
把返回值存在Page对象中
在实现类中,标记为组件并设置id=cityService;这样程序执行会找到impl类;
@Component("cityService")
@Transactional
class CityServiceImpl implements CityService {
private final CityRepository cityRepository;
private final HotelRepository hotelRepository;
@Autowired
public CityServiceImpl(CityRepository cityRepository, HotelRepository hotelRepository) {
this.cityRepository = cityRepository;
this.hotelRepository = hotelRepository;
}
@Override
public Page<City> findCities(CitySearchCriteria criteria, Pageable pageable) {
Assert.notNull(criteria, "Criteria must not be null");
String name = criteria.getName();
if (!StringUtils.hasLength(name)) {
return this.cityRepository.findAll(null);
}
String country = "";
int splitPos = name.lastIndexOf(",");
if (splitPos >= 0) {
country = name.substring(splitPos + 1);
name = name.substring(0, splitPos);
}
return this.cityRepository
.findByNameContainingAndCountryContainingAllIgnoringCase(name.trim(),
country.trim(), pageable);
}
@Override
public City getCity(String name, String country) {
Assert.notNull(name, "Name must not be null");
Assert.notNull(country, "Country must not be null");
return this.cityRepository.findByNameAndCountryAllIgnoringCase(name, country);
}
@Override
public Page<HotelSummary> getHotels(City city, Pageable pageable) {
Assert.notNull(city, "City must not be null");
return this.hotelRepository.findByCity(city, pageable);
}
}
由于执行的是事务操作所以要加上@Transactional
通过构造函数注入其他组件;
@Override
public City getCity(String name, String country) {
Assert.notNull(name, "Name must not be null");
Assert.notNull(country, "Country must not be null");
return this.cityRepository.findByNameAndCountryAllIgnoringCase(name, country);
}
在该方法中进行断言判断是否为空,然后执行Dao查询;在jpa中会通过不同的关键字在接口中定义方法名来映射为sql查询语句,上面方法相当于select * from city c where name=”bath” 返回的是一个city对象,前提是name值唯一确定。
Spring-boot官方案例分析之data-jpa的更多相关文章
- 精尽Spring Boot源码分析 - Jar 包的启动实现
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- 精尽Spring Boot源码分析 - 剖析 @SpringBootApplication 注解
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- 精尽Spring Boot源码分析 - Condition 接口的扩展
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- Spring Boot 入门详细分析
推荐阅读: 我们为什么要学习 Spring Boot 我们搭建 Spring Boot 项目,可以使用 Spring 为我们提供的初始化网站,那个可能不太方便,今天呢,我们就来说说如何使用 IDEA ...
- Spring-boot官方案例分析之log4j
Spring-boot官方案例分析之log4j 运行单元测试分析: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfigur ...
- Spring Boot源码分析-配置文件加载原理
在Spring Boot源码分析-启动过程中我们进行了启动源码的分析,大致了解了整个Spring Boot的启动过程,具体细节这里不再赘述,感兴趣的同学可以自行阅读.今天让我们继续阅读源码,了解配置文 ...
- Spring Boot源码分析-启动过程
Spring Boot作为目前最流行的Java开发框架,秉承"约定优于配置"原则,大大简化了Spring MVC繁琐的XML文件配置,基本实现零配置启动项目. 本文基于Spring ...
- 精尽Spring Boot源码分析 - 序言
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
- 精尽Spring Boot源码分析 - 文章导读
该系列文章是笔者在学习 Spring Boot 过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring Boot 源码分析 GitHub 地址 进行阅读 Sprin ...
随机推荐
- 浏览器后退->清除原页面div中填写的数据
需求说明:页面表单用前端用div布局,提交之后跳转到另一个页面,但是考虑到客户奇怪的脑回路,可能会点击浏览器的后退按钮,不知道是个体情况还是都是一样,原本div中填写的数据还依然存在,所以需要让页面在 ...
- Linux 启动SVN服务
#使用默认端口3690启动svn服务svnserve -d -r /home/svndata # 如果出现#svnserve: Can't bind server socket: Address al ...
- TCP/IP五层模型详解
将应用层,表示层,会话层并作应用层,从tcp/ip五层协议的角度来阐述每层的由来与功能,搞清楚了每层的主要协议就理解了整个互联网通信的原理. 首先,用户感知到的只是最上面一层应用层,自上而下每层都依赖 ...
- js初级DOM&BOM知识点总结
第一章 js的组成DOM BOM ECMAScript javaScript 是一种直译是脚本语言 js语言特点 .脚本编写语言 .基于对象的语言 .简单性 .动态性 .安全性 .跨平台性 C/S是C ...
- iOS中使用RNCryptor对资源文件加密
原文:http://blog.csdn.net/chenpolu/article/details/46277587 RNCryptor源码https://github.com/RNCryptor/RN ...
- 关于Java中的线程安全(线程同步)
java中的线程安全是什么: 就是线程同步的意思,就是当一个程序对一个线程安全的方法或者语句进行访问的时候,其他的不能再对他进行操作了,必须等到这次访问结束以后才能对这个线程安全的方法进行访问 什么叫 ...
- ppt写作的注意事项
PPT推荐字体及大小: 宋体严谨,适合正文,显示最清晰 黑体庄重,适合标题,或者强调区 隶书楷体,艺术性强,不适合投影 如果通过文字排版突出重点:加粗.加大字号.变色 PPT文字太多怎么办? 1.抽象 ...
- 转:ArcGIS提取面状道路中心线(转载)
1.首先把所有的面要素merge成一个要素 2.把merge后的数据转成线数据 3.此时转换后的线数据一定是闭合的,为了防止提取中心线失败(只提取出外围轮廓)我们在随意一个道路末端使用打断工具打一个开 ...
- Android 多个activity之间的共享数据
最近打算做一个时间助手,一个service监听课表信息.课表信息可以通过另外的activity来设置,所以这里就涉及到了数据的同步问题.我设置后的信息必须同步到监听的信息去. 在java中我们用全局变 ...
- Ant Design项目记录和CSS3的总结和Es6的基本总结
这里主要是介绍自己运用ANT框架的一些小总结,以前写到word里,现在要慢慢传上来, 辅助生殖项目总结:从每个组件的运用的方法和问题来总结项目. 1.项目介绍 辅助生殖项目主要运用的是Ant.desi ...