【使用篇二】SpringBoot整合SpringDataJPA(18)
一、pom.xml添加依赖
<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring data jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<!--test-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
二、配置数据源以及jpa
server:
port: 8080 #数据源
spring:
datasource:
url: jdbc:mysql://192.168.178.5:12345/cloudDB01?useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
jpa:
database: MySQL
show-sql: true
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
三、创建实体
@Entity
@Table(name = "dept")
public class DeptDTO { @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "deptno")
private Integer deptNo;
@Column(name = "dname")
private String dName;
@Column(name = "db_source")
private String dbSource; public Integer getDeptNo() {
return deptNo;
} public void setDeptNo(Integer deptNo) {
this.deptNo = deptNo;
} public String getdName() {
return dName;
} public void setdName(String dName) {
this.dName = dName;
} public String getDbSource() {
return dbSource;
} public void setDbSource(String dbSource) {
this.dbSource = dbSource;
}
}
四、创建jpa
public interface DeptRepository extends JpaRepository<DeptDTO, Integer>, JpaSpecificationExecutor<DeptDTO>, Serializable {
}
我们DeptRepository 继承了JpaRepository接口(SpringDataJPA提供的简单数据操作接口)、JpaSpecificationExecutor(SpringDataJPA提供的复杂查询接口)、Serializable(序列化接口)。我们并不需要做其他的任何操作了,因为SpringBoot以及SpringDataJPA会为我们全部搞定,SpringDataJPA内部使用了类代理的方式让继承了它接口的子接口都以spring管理的Bean的形式存在,也就是说我们可以直接使用@Autowired注解在spring管理bean使用
五、创建控制器controller
@RestController
@RequestMapping("/dept")
public class DeptController { @Autowired
private DeptRepository deptRepository; @RequestMapping(value = "/findAll", method = {RequestMethod.POST})
public List<DeptDTO> findAllDept(){
return deptRepository.findAll(); //findAll是jpa提供的查询接口
} @RequestMapping(value="/addDept", method={RequestMethod.POST})
public DeptDTO saveDept(@RequestBody DeptDTO deptDTO){
deptRepository.save(deptDTO);
return deptDTO;
} }
六、测试controller
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {JpaApplication.class}) //是该项目的启动类
@WebAppConfiguration
@ContextConfiguration
public class DeptControllerTest { @Autowired
private WebApplicationContext context; private MockMvc mvc; @Before
public void setUp() throws Exception {
mvc = MockMvcBuilders
.webAppContextSetup(context)
.build();
} @Test
public void testQuery() throws Exception {
MvcResult result=mvc.perform(MockMvcRequestBuilders.post("/dept/findAll")).andReturn();
MockHttpServletResponse response = result.getResponse();
String content = response.getContentAsString();
List<DeptDTO> deptDTOS = JSON.parseArray(content, DeptDTO.class);
for(DeptDTO deptDTO : deptDTOS){
System.out.println(deptDTO.getdName());
}
} @Test
public void testAdd() throws Exception {
DeptDTO deptDto = new DeptDTO();
deptDto.setdName("海盗船");
deptDto.setDbSource("cloudDB1");
System.out.println(JSON.toJSONString(deptDto));
MvcResult result=mvc.perform(MockMvcRequestBuilders.post("/dept/addDept")
.contentType(MediaType.APPLICATION_JSON).content(JSON.toJSONString(deptDto)))
.andReturn();
MockHttpServletResponse response = result.getResponse();
String content = response.getContentAsString();
DeptDTO deptDTO = JSON.parseObject(content, DeptDTO.class);
System.out.println(deptDTO.getDeptNo());
}
}
【使用篇二】SpringBoot整合SpringDataJPA(18)的更多相关文章
- 从无到有Springboot整合Spring-data-jpa实现简单应用
本文介绍Springboot整合Spring-data-jpa实现简单应用 Spring-data-jpa是什么?这不由得我们思考一番,其实通俗来说Spring-data-jpa默认使用hiberna ...
- jackson学习之十(终篇):springboot整合(配置类)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- Canal 实战 | 第一篇:SpringBoot 整合 Canal + RabbitMQ 实现监听 MySQL 数据库同步更新 Redis 缓存
一. Canal 简介 canal [kə'næl],译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费 早期阿里巴巴因为杭州和美国双机房部署,存在跨机房同 ...
- springboot整合springdata-jpa
1.简介 SpringData : Spring 的一个子项目.用于简化数据库访问,支持NoSQL 和 关系数据存储.其主要目标是使数据库的访问变得方便快捷. SpringData 项目所支持 No ...
- springboot 整合springDataJPA
springboot 整合springDataJPA 〇.搭建springboot环境 一.添加依赖 mysql <!-- mysql驱动 --> <dependency> & ...
- SpringBoot整合SpringDataJPA及在页面yaml中显示
SpringBoot整合SpringDataJPA及在页面yaml中显示 1:创建对应的数据表 2:添加依赖 3:配置数据源 1:创建对应的数据表 CREATE TABLE `user` ( `id` ...
- springboot整合springdatajpa时jar冲突
1.springboot整合springdatajpa测试时报No bean named 'entityManagerFactory' available错误 2.运行springboot主程序时报以 ...
- SpringBoot整合SpringDataJPA,今天没啥事情就看了一下springboot整合springdataJPA,实在是香啊,SQL语句都不用写了
SpringBoot整合SpringDataJPA 1.JPA概念 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映 ...
- 七、springboot整合Spring-data-jpa
1.Spring Data JPA是什么 由Spring提供的一个用于简化JPA开发的框架.可以在几乎不用写实现的情况下,实现对数据的访问和操作.除了CRUD外,还包括如分页.排序等一些常用的功能 1 ...
随机推荐
- 详谈springboot启动类的@SpringBootApplication注解
前几天我们学会了如何创建springboot项目今天我们说一下他是怎么运行的为什么不需要我们再去编写繁重的配置文件的 @SpringBootApplication 首先我们看一下这个注解,他是用来标注 ...
- 【Vuejs】397- Vue 3最值得期待的五项重大更新
作者|Filip Rakowski 译者|王强 编辑|王文婧 最近关于即将发布的 Vue.js 的第 3 个大版本的消息越来越密集.虽然本文所讨论的内容还没有完全确定下来,但作者已经可以肯定它将是对当 ...
- screen虚拟终端工具
说明:有时候我们要执行一个命令或脚本,需要几小时甚至几天,但是不能中断,有时想查看当前输出信息的时候,可以将它丢到后台运行,但是后台运行却无法显示或输出相关信息出来:我们可以使用一个虚拟终端工具scr ...
- 一线互联网公司Redis使用精髓,你必须要掌握这4点!
先来看一下这些Redis面试题你会几道? 1.什么是 Redis?简述它的优缺点? 2.Redis 与 memcached 相比有哪些优势? 3.Redis 支持哪几种数据类型? 4.Redis 主要 ...
- Scala函数式编程(四)函数式的数据结构 下
前情提要 Scala函数式编程指南(一) 函数式思想介绍 scala函数式编程(二) scala基础语法介绍 Scala函数式编程(三) scala集合和函数 Scala函数式编程(四)函数式的数据结 ...
- Vue ---- 组价 组件化 子传父 父传子
目录 补充js的for循环: 组件 1.组件的分类: 2.组件的特点 3.创建局部组件 4.全局组件 二.组件化 一.组件传参父传子 二.组件传参:子传父 补充js的for循环: // for in遍 ...
- wc命令统计目录下所有文件行数
想统计一下最近一个项目的代码行数,一个一个文件统计显然不是程序员的思维,wc命令可以统计一个文本的行数,结合find命令可以实现我的需求(注意符号):
- DHCP服务相关实验
一.DHCP 相关介绍 1.dhcp服务相关 软件名: dhcp #DHCP服务软件包 dhcp-common #DHCP命令软件包(默认已安装) 服务名: dhcpd #DHCP服务名 dhcrel ...
- Intel和AMD的CPU性能对比图
Intel和AMD的CPU性能对比图:
- 菜鸟刷面试题(四、Spring/Spring MVC/Spring Boot/Spring Cloud篇)
目录: 为什么要使用 spring? 解释一下什么是 aop? 解释一下什么是 ioc? spring 有哪些主要模块? spring 常用的注入方式有哪些? spring 中的 bean 是线程安全 ...