一、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)的更多相关文章

  1. 从无到有Springboot整合Spring-data-jpa实现简单应用

    本文介绍Springboot整合Spring-data-jpa实现简单应用 Spring-data-jpa是什么?这不由得我们思考一番,其实通俗来说Spring-data-jpa默认使用hiberna ...

  2. jackson学习之十(终篇):springboot整合(配置类)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  3. Canal 实战 | 第一篇:SpringBoot 整合 Canal + RabbitMQ 实现监听 MySQL 数据库同步更新 Redis 缓存

    一. Canal 简介 canal [kə'næl],译意为水道/管道/沟渠,主要用途是基于 MySQL 数据库增量日志解析,提供增量数据订阅和消费 早期阿里巴巴因为杭州和美国双机房部署,存在跨机房同 ...

  4. springboot整合springdata-jpa

    1.简介  SpringData : Spring 的一个子项目.用于简化数据库访问,支持NoSQL 和 关系数据存储.其主要目标是使数据库的访问变得方便快捷. SpringData 项目所支持 No ...

  5. springboot 整合springDataJPA

    springboot 整合springDataJPA 〇.搭建springboot环境 一.添加依赖 mysql <!-- mysql驱动 --> <dependency> & ...

  6. SpringBoot整合SpringDataJPA及在页面yaml中显示

    SpringBoot整合SpringDataJPA及在页面yaml中显示 1:创建对应的数据表 2:添加依赖 3:配置数据源 1:创建对应的数据表 CREATE TABLE `user` ( `id` ...

  7. springboot整合springdatajpa时jar冲突

    1.springboot整合springdatajpa测试时报No bean named 'entityManagerFactory' available错误 2.运行springboot主程序时报以 ...

  8. SpringBoot整合SpringDataJPA,今天没啥事情就看了一下springboot整合springdataJPA,实在是香啊,SQL语句都不用写了

    SpringBoot整合SpringDataJPA 1.JPA概念 JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映 ...

  9. 七、springboot整合Spring-data-jpa

    1.Spring Data JPA是什么 由Spring提供的一个用于简化JPA开发的框架.可以在几乎不用写实现的情况下,实现对数据的访问和操作.除了CRUD外,还包括如分页.排序等一些常用的功能 1 ...

随机推荐

  1. 【CSS】357- 坚定地使用 CSS Custom Properties

    自定义属性(Custom Properties)是一个很有魅力的 CSS 新特性,现代浏览器广泛 支持.但是遇到那些不支持 CSS Custom Properties 的老掉牙浏览器我们该怎么办?等着 ...

  2. layui扩展组件,下拉树多选

      项目介绍 项目中需要用到下拉树多选功能,找到两个相关组件moretop-layui-select-ext和wujiawei0926-treeselect,但是moretop-layui-selec ...

  3. Linux---centos7.0安装、配置

    参考:https://blog.csdn.net/qq_37057095/article/details/81240450

  4. 前端表单提交,提交有图片出现的问题,及解决方案 兼容ie9

    更新一下我的小园子,主要说的是jq文件上传的过程中,如果出现上传的文件里有图片问题 其实文件上传有图片的情况下,不是什么大问题,对于前端来说,但是,如果需要兼容ie9的时候,就需要处理一下 文件上传如 ...

  5. 【Java Web开发学习】Spring构造器和属性注入

    测试类 public class Construct { private String address; private long phone; public Construct(String nam ...

  6. hibernate mysql中文检出无效

    在学习ssh框架是发现,检索条件是英文时,sql就能按照条件过滤出数据,当我换成中文是,检索出来的数据就是空,最后发现没有设置数据库连接url的编码格式 1.数据库编码 COLLATE='utf8_g ...

  7. Docker swarm实战总结

    一.简介 Swarm 是 Docker 官方提供的一款集群管理工具,其主要作用是把若干台 Docker 主机抽象为一个整体,并且通过一个入口统一管理这些 Docker 主机上的各种 Docker 资源 ...

  8. poj 2955 Brackets (区间dp基础题)

    We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...

  9. 数据库day01

    作业: 1. 查看岗位是teacher的员工姓名.年龄 select name,age from staff_info where jobs = 'teacher'; 查看岗位是teacher且年龄大 ...

  10. vue-UI(mui和muit-UI)

    MUI和MUIT-UI 这里使用了连个UI---mui和mit-ui mit-ui是基于vue.js的,而mui是一个高性能前端框架(H5+提供的),类似于bootstrap,所以在引入时区别还是很大 ...