【使用篇二】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 ...
随机推荐
- 【JS】376- Axios 使用指南
点击上方"前端自习课"关注,学习起来~ 来源 | https://www.jianshu.com/p/df464b26ae58 一.axios 基于promise用于浏览器和 ...
- 国内加速git下载速度
主要是配置hosts文件 151.101.72.133 assets-cdn.github.com151.101.73.194 github.global.ssl.fastly.net192.30.2 ...
- salt
更新于 3.25 23:16 salt简介 SaltStack是一个服务器基础架构集中化管理平台,具备配置管理.远程执行.监控等功能,基于Python语言实现,结合轻量级消息队列(ZeroMQ)与Py ...
- 面试BAT前先搞定这18道MySQL经典面试题(含答案解析)
一.MySQL的复制原理以及流程 (1)复制基本原理流程 1. 主:binlog线程——记录下所有改变了数据库数据的语句,放进master上的binlog中: 2. 从:io线程——在使用start ...
- 你不知道的JavaScript(上)this和对象原型(一)
第一章 关于this 1.this 既不指向函数自身也不指向函数的词法作用域 2.this 实际上是在函数被调用时发生的绑定,它指向什么完全取决于函数在哪里被调用(调用位置). 第二章 this全面 ...
- So Easy - 在Linux服务器上部署 .NET Core App
.NET Core 是微软提供的免费.跨平台和开源的开发框架,可以构建桌面应用程序.移动端应用程序.网络应用程序.物联网应用程序和游戏应用程序等.如果你是 Windows 平台下的 dotnet 开发 ...
- 《Java基础知识》Java正则表达式
正则表达式定义了字符串的模式. 正则表达式可以用来搜索.编辑或处理文本. 正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别. 正则表达式实例 一个字符串其实就是一个简单的正则表达式,例如 ...
- 《Java算法》排序算法-快速排序
排序算法-快速排序: /** * 给定一个数组:按照从小到大排序. * 思路: * 1. 获取第一个数放入临时变量data,将大于data的数放右边,小于data的数放在左边. * 2. data左边 ...
- VS2019 开发Django(十一)------表单
导航:VS2019开发Django系列 今天是中华人民共和国成立70周年的日子,普天同庆,看阅兵看得满腔热血,热泪盈眶,祖国都这么优秀了,我要更加努力才行啊! 这个Django系列的文章,没有很深入的 ...
- Python基础-day01-9
变量的命名 目标 标识符和关键字 变量的命名规则 0.1 标识符和关键字 1.1 标识符 标示符就是程序员定义的 变量名.函数名 名字 需要有 见名知义 的效果,见下图: 标示符可以由 字母.下划线 ...