Spring Boot 菜鸟教程 2 Data JPA
GitHub
src="//ghbtns.com/github-btn.html?user=je-ge&repo=spring-boot&type=watch&count=true" scrolling="0" width="110" height="20">
Spring Data JPA
用来简化创建 JPA 数据访问层和跨存储的持久层功能。
官网文档连接
http://docs.spring.io/spring-data/jpa/docs/current/reference/html/
Spring Data JPA提供的接口
- Repository:最顶层的接口,是一个空的接口,目的是为了统一所有Repository的类型,且能让组件扫描的时候自动识别
- CrudRepository :是Repository的子接口,提供CRUD的功能
- PagingAndSortingRepository:是CrudRepository的子接口,添加分页和排序的功能
- JpaRepository:是PagingAndSortingRepository的子接口,增加了一些实用的功能,比如:批量操作等
- JpaSpecificationExecutor:用来做负责查询的接口
- Specification:是Spring Data JPA提供的一个查询规范,要做复杂的查询,只需围绕这个规范来设置查询条件即可
Repository接口查询规则
关键字 | 案例 | 效果 |
---|---|---|
And | findByLastnameAndFirstname | … where x.lastname = ?1 and x.firstname = ?2 |
Or | findByLastnameOrFirstname | … where x.lastname = ?1 or x.firstname = ?2 |
Is,Equals | findByFirstname,findByFirstnameIs,findByFirstnameEquals | … where x.firstname = ?1 |
Between | findByStartDateBetween | … where x.startDate between ?1 and ?2 |
LessThan | findByAgeLessThan | … where x.age < ?1 |
LessThanEqual | findByAgeLessThanEqual | … where x.age <= ?1 |
GreaterThan | findByAgeGreaterThan | … where x.age > ?1 |
GreaterThanEqual | findByAgeGreaterThanEqual | … where x.age >= ?1 |
After | findByStartDateAfter | … where x.startDate > ?1 |
Before | findByStartDateBefore | … where x.startDate < ?1 |
IsNull | findByAgeIsNull | … where x.age is null |
IsNotNull,NotNull | findByAge(Is)NotNull | … where x.age not null |
Like | findByFirstnameLike | … where x.firstname like ?1 |
NotLike | findByFirstnameNotLike | … where x.firstname not like ?1 |
StartingWith | findByFirstnameStartingWith | … where x.firstname like ?1 (parameter bound with appended %) |
EndingWith | findByFirstnameEndingWith | … where x.firstname like ?1 (parameter bound with prepended %) |
Containing | findByFirstnameContaining | … where x.firstname like ?1 (parameter bound wrapped in %) |
OrderBy | findByAgeOrderByLastnameDesc | … where x.age = ?1 order by x.lastname desc |
Not | findByLastnameNot | … where x.lastname <> ?1 |
In | findByAgeIn(Collection ages) | … where x.age in ?1 |
NotIn | findByAgeNotIn(Collection age) | … where x.age not in ?1 |
TRUE | findByActiveTrue() | … where x.active = true |
FALSE | findByActiveFalse() | … where x.active = false |
IgnoreCase | findByFirstnameIgnoreCase | … where UPPER(x.firstame) = UPPER(?1) |
项目图片
pom.xml
-只需要在pom.xml引入需要的数据库配置,就会自动访问此数据库,如果需要配置其他数据库,可以在application.properties进行添加
-默认使用org.apache.tomcat.jdbc.pool.DataSource创建连接池
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.jege.spring.boot</groupId><artifactId>spring-boot-data-jpa</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>spring-boot-data-jpa</name><url>http://maven.apache.org</url><!-- 公共spring-boot配置,下面依赖jar文件不用在写版本号 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.1.RELEASE</version><relativePath /></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><java.version>1.8</java.version></properties><dependencies><!-- 持久层 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- h2内存数据库 --><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><finalName>spring-boot-data-jpa</finalName><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>${java.version}</source><target>${java.version}</target></configuration></plugin></plugins></build>
</project>
模型对象User
package com.jege.spring.boot.data.jpa.entity;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;/*** @author JE哥* @email 1272434821@qq.com* @description:jpa模型对象*/
@Entity
@Table(name = "t_user")
public class User {@Id@GeneratedValueprivate Long id;private String name;private Integer age;public User() {}public User(String name, Integer age) {this.name = name;this.age = age;}}
持久层UserRepository
package com.jege.spring.boot.data.jpa.repository;import java.util.List;import org.springframework.data.jpa.repository.JpaRepository;import com.jege.spring.boot.data.jpa.entity.User;/*** @author JE哥* @email 1272434821@qq.com* @description:持久层接口,由spring自动生成其实现*/
public interface UserRepository extends JpaRepository<User, Long> {List<User> findByNameLike(String name);}
启动类Application
package com.jege.spring.boot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @author JE哥* @email 1272434821@qq.com* @description:spring boot 启动类*/@SpringBootApplication
public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
配置文件application.properties
## JPA Settings
spring.jpa.generate-ddl: true
spring.jpa.show-sql: true
spring.jpa.hibernate.ddl-auto: create
spring.jpa.properties.hibernate.format_sql: false
测试类UserRepositoryTest
package com.jege.spring.boot.data.jpa;import static org.assertj.core.api.Assertions.assertThat;import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.jege.spring.boot.data.jpa.entity.User;
import com.jege.spring.boot.data.jpa.repository.UserRepository;/*** @author JE哥* @email 1272434821@qq.com* @description:*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
public class UserRepositoryTest {@AutowiredUserRepository userRepository;// 打印出class com.sun.proxy.$Proxy66表示spring注入通过jdk动态代理获取接口的子类@Testpublic void proxy() throws Exception {System.out.println(userRepository.getClass());}@Testpublic void save() throws Exception {for (int i = 0; i < 10; i++) {User user = new User("jege" + i, 25 + i);userRepository.save(user);}}@Testpublic void all() throws Exception {save();assertThat(userRepository.findAll()).hasSize(10);}@Testpublic void findByName() throws Exception {save();assertThat(userRepository.findByNameLike("jege%")).hasSize(10);}@Afterpublic void destroy() throws Exception {userRepository.deleteAll();}}
源码地址
https://github.com/je-ge/spring-boot
如果觉得我的文章或者代码对您有帮助,可以请我喝杯咖啡。
您的支持将鼓励我继续创作!谢谢!
Spring Boot 菜鸟教程 2 Data JPA相关推荐
- Spring Boot 菜鸟教程 12 EasyPoi导出Excel下载
GitHub src="//ghbtns.com/github-btn.html?user=je-ge&repo=spring-boot&type=watch&cou ...
- Spring Boot 菜鸟教程 异常 集锦
异常1.集成SPRing Data JPA 异常信息摘要: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataS ...
- Spring Boot 菜鸟教程 application.properties 常用配置
SPRING CONFIG (ConfigFileApplicationListener) spring.config.name 配置文件名称,默认为application spring.config ...
- Spring Boot 菜鸟教程 3 MyBatis
GitHub src="//ghbtns.com/github-btn.html?user=je-ge&repo=spring-boot&type=watch&cou ...
- spring boot 菜鸟教程学习:spring是一个超级大工厂能够管理java对象(bean)和他们之间的关系(依赖注入)
springboot的java对象叫做bean 用一个叫依赖注入的方法来管理bean的依赖关系 说白了 就是bean是节点 依赖注入能够构建节点之间的关系 创建bean的三种方式 如何依赖注入?
- springboot做网站_Github点赞接近 100k 的Spring Boot学习教程+实战项目推荐!
" 本文已经收录进:awesome-java (Github 上非常棒的 Java 开源项目集合) 很明显的一个现象,除了一些老项目,现在 Java 后端项目基本都是基于 Spring Bo ...
- 全网Star最多「近20k」的Spring Boot开源教程 2019 年要继续更新了
点击蓝色"程序猿DD"关注我哟 从2016年1月开始写博客,默默地更新<Spring Boot系列教程>,从无人问津到千万访问,作为一个独立站点(http://blog ...
- 全网Star最多(近20k)的Spring Boot开源教程 2019 年要继续更新了!
从2016年1月开始写博客,默默地更新<Spring Boot系列教程>,从无人问津到千万访问,作为一个独立站点(http://blog.didispace.com),相信只有那些跟我一样 ...
- Spring Boot入门教程(四十):微信支付集成-刷卡支付
分享一个朋友的人工智能教程.比较通俗易懂,风趣幽默,感兴趣的朋友可以去看看. 一:准备工作 使用微信支付需要先开通服务号,然后还要开通微信支付,最后还要配置一些开发参数,过程比较多. 申请服务号(企业 ...
- Spring Boot 基础教程:集成 Knife4j
前言 之前介绍了如何在 Spring Boot 中集成 Swagger2 和 Swagger3,对于我们日常的接口管理已经够用了.但是作为一个颜值党,无论是 Swagger2 还是 Swagger3, ...
最新文章
- 怎么把写好的python代码打包成exe-【Python之点到为止】如何优雅的将你的代码打包成EXE...
- iOS安全攻防(三):使用Reveal分析他人app
- Java笔记:与系统交互、系统相关的类,Object类
- 进站公交车碾起积水溅上轿车两男子驾车撞伤公交司机
- 16个推荐系统开放公共数据集整理分享
- php 常用的系统函数
- 程序员应该多逛的几个技术网站
- mybatis学习(22):查询排序
- 实验 5 编写、调试具有多个段的
- python scrapy cookies 处理
- C++socket编程(三):3.4 listen监听
- php转化为2位小数的数字,学习猿地-php 转化为两位小数的方法
- python导入图片语法_MarkDown添加图片的三种方式
- 深度学习各场景评估指标总结
- 获取选择的当前天、周、月、年的时间段
- 关于平方根倒数速算法(雷神之锤3,牛B)
- Rust: 如何生成一个水仙花数?
- 笔记本电脑华硕N56VZ的几种螺丝规格
- oracle中asm是什么,什么是ASM?
- Arduino 读取 Pin2 的电平信号,并把结果打印到串口,也同时反映到 LED 灯
热门文章
- Shell脚本学习指南(一)——基本概念
- EndNote编辑毕业论文格式
- java计算机毕业设计网上书店管理系统源码+系统+数据库+lw文档+mybatis+运行部署
- 深入解析数码相机CCD坏点及噪点检测!【图解教程】
- php滑动门效果,js实现简洁的TAB滑动门效果代码
- 英伟达显卡不同架构_NVIDIA显卡架构代号的另一面:他们都是伟大的科学先驱
- vc6.0编译c++程序后在vc2008中调试的技巧(符号信息和链接)
- 视频教程-js+ajax+jquery+easyui从入门到精通(项目实战)-JavaScript
- 【MTK 驱动开发---camera 基础知识1】
- Minecraft 1.12.2模组开发(三十七) 3D盔甲