1、maven引入jar包(jpa和mysql)

        <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

2、配置文件

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver server.port=8080

3、创建实体类(示例)

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable; @Entity
@Table(name="student")
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Integer sid;
private String sname;
private Integer sage;
private String ssex; public Integer getSid() {
return sid;
} public void setSid(Integer sid) {
this.sid = sid;
} public String getSname() {
return sname;
} public void setSname(String sname) {
this.sname = sname;
} public Integer getSage() {
return sage;
} public void setSage(Integer sage) {
this.sage = sage;
} public String getSsex() {
return ssex;
} public void setSsex(String ssex) {
this.ssex = ssex;
}
}

4、创建对应实体的接口

import com.demo.entity.Student;
import org.springframework.data.jpa.repository.JpaRepository;

//1、这个类中自带一些简单的增删改查方法,可以直接调用
public interface StudentRepository extends JpaRepository<Student,Integer> { /*2、也支持执行sql语句(例如)*/
@Query("from Student where sage = ?1")
List<Student> getStudentsBySage(@Param("sage") Integer sage); /*3、支持根据方法名的查询(例如)*/
Student findBySid(Integer sid); }
Keyword Sample JPQL snippet

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<Age> ages)

… where x.age in ?1

NotIn

findByAgeNotIn(Collection<Age> 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)

详细介绍:

https://blog.csdn.net/ityouknow/article/details/52688664

最新文章

  1. PHP 传值和传引用、传地址的区别
  2. 理解ruby on rails中的ActiveRecord::Relation
  3. C语言中.h和.c文件解析(很精彩)
  4. JSON取代XML?--JSON入门指南
  5. 纪录一个table元素里面的tr th td
  6. ASP.NET自定义控件组件开发 第五章 模板控件开发
  7. Halloc内存分配器
  8. hdu 2114 Calculate S(n) 数论(简单题)
  9. protobuf那些事
  10. Next Greater Element I
  11. ZooKeeper集群的安装、配置、高可用测试
  12. SpriteBuilder中的距离关节的min和max距离属性值
  13. jQuery.rotate.js(控制图片转动)
  14. Android studio报Error:(26, 13)-v7:27.错误的解决方法
  15. How to enable AHCI on Windows7
  16. 使用nvidia-smi命令查看显卡信息
  17. 论文笔记 SSD: Single Shot MultiBox Detector
  18. 《十天学会单片机和C语言编程》
  19. MySQL 过滤数据(WHERE子句)
  20. node中可读流、可写流

热门文章

  1. 滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919
  2. Linux 实用指令(5)--组管理和权限管理
  3. tensorboard在Mac OS X系统环境下如何启动
  4. 黑阀 adb 命令
  5. Clover config.plist Boot部分
  6. Tensortflow安装
  7. 线性dp——求01串最大连续个数不超过k的方案数,cf1027E 好题!
  8. 模拟——1031D
  9. LocalSessionFactoryBean有几个属性查找hibernate映射文件
  10. Spring MVC(十三)--保存并获取属性参数