SpringData JPA只是SpringData中的一个子模块

JPA是一套标准接口,而Hibernate是JPA的实现

SpringData JPA 底层默认实现是使用Hibernate

1.      添加pom

#只会执行ddl

spring.jpa.hibernate.ddl-auto=update

spring.jpa.show-sql=true

spring.jackson.serialization.indent_output=true

spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect

3.      DDL

dropdatabaseifexists mybatis;

createdatabase mybatis;

use mybatis;

createtablemybatis.CUSTOMERS (

ID bigint auto_increment notnull,

NAMEvarchar(15) notnull,

EMAIL varchar(128) ,

PASSWORDvarchar(8) ,

PHONE int ,

ADDRESS varchar(255),

SEX char(1) ,

IS_MARRIED bit,

DESCRIPTION text,

IMAGE blob,

BIRTHDAY date,

REGISTERED_TIME timestamp,

primarykey (ID)

);

INSERTINTOmybatis.CUSTOMERS (NAME,PHONE,ADDRESS) VALUES ('老赵', '123456' , 'address 1');

INSERTINTOmybatis.CUSTOMERS (NAME,PHONE,ADDRESS) VALUES ('老王', '654321' , 'address 2');

会自动执行DDL

4.      配置SwaggerConfig

5.      使用jpa生成Customers实体

注意:需要在自增的id get方法上加上@GeneratedValue(strategy =GenerationType.AUTO)

@Id

@Column(name = "ID", unique = true, nullable = false)

@GeneratedValue(strategy = GenerationType.AUTO)

public Long getId() {

returnthis.id;

}

6.      生产CustomersJpaRepository和CustomersRepository

注意:sql里的表名必须和对象名完全一致,包括大小写

package com.example.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.domain.Customers;

publicinterface CustomersJpaRepository extends JpaRepository<Customers,Long>{

}

package com.example.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Query;

import org.springframework.data.repository.Repository;

import org.springframework.data.repository.query.Param;

import com.example.domain.Customers;

//注意:sql里的表名必须和对象名完全一致,包括大小写

publicinterface CustomersRepository extends Repository<Customers,Long>{

@Query(value = "fromCustomers o where id=(select max(id) from Customers p)")

public Customers getCustomersByMaxId();

@Query(value = "fromCustomers o where o.name=?1 and o.phone=?2")

public List<Customers> queryParams1(String name, Integer phone);

@Query(value = "fromCustomers o where o.name=:name and o.phone=:phone")

public List<Customers> queryParams2(@Param("name")String name, @Param("phone")Integer phone);

@Query(value = "fromCustomers o where o.name like %?1%")

public List<Customers> queryLike1(String name);

@Query(value = "fromCustomers o where o.name like %:name%")

public List<Customers> queryLike2(@Param("name")String name);

@Query(nativeQuery = true, value = "select count(1) from Customers o")

publiclong getCount();

}

Repository:是SpringData的一个核心接口,它不提供任何方法,开发者需要在自己定义的接口中声明需要的方法。

CrudRepository:继承Repository,提供增删改查方法,可以直接调用。

PagingAndSortingRepository:继承CrudRepository,具有分页查询和排序功能(本类实例)

JpaRepository:继承PagingAndSortingRepository,针对JPA技术提供的接口

JpaSpecificationExecutor:可以执行原生SQL查询

继承不同的接口,有两个不同的泛型参数,他们是该持久层操作的类对象和主键类型。

7.      配置customersService并且加缓存

package com.example.service;

import java.util.List;

import org.springframework.data.repository.query.Param;

import com.example.domain.Customers;

publicinterface CustomersService {

public Customers getCustomersByMaxId();

public List<Customers> queryParams1(String name, Integer phone);

public List<Customers> queryParams2(@Param("name")String name, @Param("phone")Integer phone);

public List<Customers> queryLike1(String name);

public List<Customers> queryLike2(@Param("name")String name);

publiclong getCount();

public List<Customers> findAll();

public Customers findOne(Long id);

publicvoid delete(longid);

publicvoid deleteAll();

publicvoid save(List<Customers> entities);

publicvoid save(Customers entity);

}

package com.example.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.cache.annotation.CacheConfig;

import org.springframework.cache.annotation.Cacheable;

import org.springframework.stereotype.Service;

importorg.springframework.transaction.annotation.Transactional;

import com.example.domain.Customers;

import com.example.repository.CustomersJpaRepository;

import com.example.repository.CustomersRepository;

import com.example.service.CustomersService;

@Service(value = "customersService")

@Transactional

@CacheConfig(cacheNames = "customers")

publicclass CustomersServiceImpl implements CustomersService{

@Autowired

private CustomersRepository customersRepository;

@Autowired

private CustomersJpaRepository customersJpaRepository;

@Override

@Cacheable

public Customers getCustomersByMaxId() {

returncustomersRepository.getCustomersByMaxId();

}

@Override

@Cacheable

public List<Customers> queryParams1(String name, Integer phone) {

returncustomersRepository.queryParams1(name, phone);

}

@Override

@Cacheable

public List<Customers> queryParams2(String name, Integer phone) {

return  customersRepository.queryParams2(name, phone);

}

@Override

@Cacheable

public List<Customers> queryLike1(String name) {

return  customersRepository.queryLike1(name);

}

@Override

@Cacheable

public List<Customers> queryLike2(String name) {

return  customersRepository.queryLike2(name);

}

@Override

@Cacheable

publiclong getCount() {

return  customersRepository.getCount();

}

@Override

@Cacheable

public List<Customers> findAll() {

returncustomersJpaRepository.findAll();

}

@Override

@Cacheable

public Customers findOne(Long id) {

returncustomersJpaRepository.findOne(id);

}

@Override

@Cacheable

publicvoid deleteAll(){

customersJpaRepository.deleteAll();

}

@Override

@Cacheable

publicvoid delete(longid){

customersJpaRepository.delete(id);

}

@Override

@Cacheable

publicvoid save(List<Customers> entities){

customersJpaRepository.save(entities);

}

@Override

@Cacheable

publicvoid save(Customers entity){

customersJpaRepository.save(entity);

}

}

8.      配置CustomersController

package com.example.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.repository.query.Param;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.RestController;

import com.example.domain.Customers;

import com.example.service.CustomersService;

@RestController

@RequestMapping("/customers")

publicclass CustomersController {

@Autowired

private CustomersService customersService;

@RequestMapping(value="getCustomersByMaxId", method=RequestMethod.GET)

public Customers getCustomersByMaxId(){

returncustomersService.getCustomersByMaxId();

}

@RequestMapping(value="queryParams1/{name}/{phone}", method=RequestMethod.POST)

public List<Customers> queryParams1(String name, Integer phone){

returncustomersService.queryParams1(name, phone);

}

//http://localhost:8080/customers/queryParams2/%7Bname%7D/%7Bphone%7D?name=老赵&phone=123456

@RequestMapping(value="queryParams2/{name}/{phone}", method=RequestMethod.POST)

public List<Customers> queryParams2(@Param("name")String name, @Param("phone")Integer phone){

returncustomersService.queryParams2(name, phone);

}

@RequestMapping(value="queryLike1/{name}", method=RequestMethod.POST)

public List<Customers> queryLike1(String name){

returncustomersService.queryLike1(name);

}

//http://localhost:8080/customers/queryLike2/%7Bname%7D?name=老王

@RequestMapping(value="queryLike2/{name}", method=RequestMethod.POST)

public List<Customers> queryLike2(@Param("name")String name){

returncustomersService.queryLike2(name);

}

@RequestMapping(value="getCount", method=RequestMethod.GET)

publiclong getCount(){

returncustomersService.getCount();

}

@RequestMapping(value="findAll", method=RequestMethod.GET)

public List<Customers> findAll() {

returncustomersService.findAll();

}

@RequestMapping(value="findOne", method=RequestMethod.POST)

public Customers findOne(Long id) {

returncustomersService.findOne(id);

}

@RequestMapping(value="deleteAll", method=RequestMethod.GET)

publicvoid deleteAll(){

customersService.deleteAll();

}

@RequestMapping(value="delete", method=RequestMethod.POST)

publicvoid delete(longid){

customersService.delete(id);

}

@RequestMapping(value="saveAll", method=RequestMethod.POST)

publicvoid save(List<Customers> entities){

customersService.save(entities);

}

@RequestMapping(value="save", method=RequestMethod.POST)

publicvoid save(Customers entity){

customersService.save(entity);

}

}

9.      配置启动项DemoApplication

package com.example;

import org.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication

@EnableCaching

publicclass DemoApplication {

publicstaticvoid main(String[] args) {

SpringApplication.run(DemoApplication.class, args);

}

//to visithttp://localhost:8080/swagger-ui.html

}

SpringData JPA示例的更多相关文章

  1. Spring、SpringMVC、SpringData + JPA 整合详解

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7759874.html ------------------------------------ ...

  2. 6.4 SpringData JPA的使用

    引言:该文档是参考尚硅谷的关于springboot教学视屏后整理而来.当然后面还加入了一些自己从网上收集整理而来的案例! 一.SpringData JPA初步使用 1. springdata简介 2. ...

  3. Springboot集成SpringData JPA

    序 StringData JPA 是微服务框架下一款ORM框架,在微服务体系架构下,数据持久化框架,主要为SpringData JPA及Mybatis两种,这两者的具体比较,本文不做阐述,本文只简单阐 ...

  4. 从一个简单的 JPA 示例开始

    本文主要讲述 Spring Data JPA,但是为了不至于给 JPA 和 Spring 的初学者造成较大的学习曲线,我们首先从 JPA 开始,简单介绍一个 JPA 示例:接着重构该示例,并引入 Sp ...

  5. springdata jpa使用Example快速实现动态查询

    Example官方介绍 Query by Example (QBE) is a user-friendly querying technique with a simple interface. It ...

  6. 【极简版】SpringBoot+SpringData JPA 管理系统

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 在上一篇中已经讲解了如何从零搭建一个SpringBo ...

  7. 带你搭一个SpringBoot+SpringData JPA的环境

    前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 不知道大家对SpringBoot和Spring Da ...

  8. 尚硅谷springboot学习34-整合SpringData JPA

    SpringData简介

  9. 一篇 SpringData+JPA 总结

    概述 SpringData,Spring 的一个子项目,用于简化数据库访问,支持 NoSQL 和关系数据库存储 SpringData 项目所支持 NoSQL 存储 MongDB(文档数据库) Neo4 ...

随机推荐

  1. POJ1201 Intervals【差分约束系统】

    Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p ...

  2. MySQL实战 | 04 为什么要使用索引?

    原文链接:MySQL实战 | 为什么要使用索引? 用过 MySQL 的应该都知道索引是干啥的吧,应该多少都设置过索引,但是若是问你索引是怎么实现的,你能说上来吗? 索引是什么? MySQL 官方对索引 ...

  3. 使用反相器的rc振荡电路

    多谐振荡器是一种自激振荡电路,该电路在接通电源后无需外接触发信号就能产生一定频率和幅值的矩形脉冲波或方波.由于多谐振荡器在工作过程中不存在稳定状态,故又称为无稳态电路. 一.门电路组成的多谐振荡器 1 ...

  4. gradle build scan 插件使用

    1. 添加插件 build.gradle plugins { id 'com.gradle.build-scan' version '1.10.2' } buildScan { // Uncommen ...

  5. 【转】一步一步教你在Ubuntu12.04搭建gstreamer开发环境

    原文网址:http://blog.csdn.net/xsl1990/article/details/8333062 闲得蛋疼    无聊寂寞冷    随便写写弄弄 看到网上蛮多搭建gstreamer开 ...

  6. VMware虚拟机克隆CentOS 6.5后网卡修改方法

    VMware虚拟机安装好CentOS6.5系统后,纯净的系统多克隆几份出来方便后期做试验.克隆步骤很简单,克隆后出现的问题是克隆后的网卡MAC地址和原系统MAC地址一样,在局域网内会有冲突,需要更改克 ...

  7. awk&sed 小实例

    1.打印文件奇数行sed -n 'p;n'sed 'n;d' sed -n '$!N;P'sed -n '1~2p'awk 'i=!i'awk 'NR%2'2.打印文件偶数行sed -n 'n;p's ...

  8. 保证service不被杀死的方法

    Service设置成START_STICKY kill 后会被重启(等待5秒左右),重传Intent,保持与重启前一样 提升service优先级 在AndroidManifest.xml文件中对于in ...

  9. thinkphp 3.2.3 计划任务具体实现实例教程

    thinkphp 3.2.3 计划任务具体实现实例教程 很多情况下,我们网站都会用到计划任务即定时更新做一些处理,类似Discuz后台的计划任务,比如更新每日发帖数目等等! 这里TP也是可以实现的,首 ...

  10. Joker的运维开发之路

    python 1--数据类型,流程控制 2--数据类型详细操作,文件操作,字符编码 https://mp.weixin.qq.com/s/i3lcIP82HdsSr9LzPgkqww 点开更精彩 目前 ...