JPA:Java持久化规范。JPA(Java Persistence API)是Sun官方提出的Java持久化规范。为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据。它的出现是为了简化现有的持久化开发工作和整合ORM技术。结束各个ORM框架各自为营的局面。
Spring Data JPA是Spring基于ORM框架、JPA规范的基础上封装的一套JPA应用框架,是基于Hibernate之上构建的JPA使用解决方案,用极简的代码实现了对数据库的访问和操作,包括了增、删、改、查等在内的常用功能。
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
5.1.25
com.alibaba
druid
1.1.10
org.projectlombok
lombok
1.18.6
spring:
datasource:
url: jdbc:mysql://localhost:3306/springboot-001?serverTimezone=GMT%2B8&&characterEncoding=UTF-8
type: com.alibaba.druid.pool.DruidDataSource
username: root
password: 113846
driver-class-name: com.mysql.jdbc.Driver #5.x版本mysql
jpa:
hibernate:
ddl-auto: update
show-sql: true
ddl-auto: 四种规范,分别为
1.create:每次运行项目时候都会根据实体类生成对应的表,缺点造成数据丢失。
2.update:每次运行时先检查数据库有无表,有则运行无则生成
3.create:每次生成新表,但是sessionFactory每次关闭对应表也删除
4.validate:验证数据库表的结构
show-sql:显示sql语句,在控制台打印出来
@Entity
@Data //get set
@AllArgsConstructor //生成带有所有参数的构造方法
@NoArgsConstructor //无参数构造方法
public class Person {
@Id
//自动生成主键
@GeneratedValue
private Long id;
//nullable,将来创建表的字段值是否为空,length:长度20
@Column(name = "name", nullable = true, length = 20)
private String name;
@Column(name = "age", nullable = true, length = 4)
private int age;
}
@Entity:Entity注解作为实体类的证明,通常与Lombok中的@Data注解连用,降低开发中底层重复代码的编程量。
//Person:实体类类名 Long:主键类型
public interface PersonRepository extends JpaRepository {
//查询指定名字的人
public Person findByNameIs(String name);
//查询指定人的名字和年龄的人
public Person findByNameIsAndAgeIs(String name, int age);
//查询名字包含指定名字的人
public Person findByNameContaining(String name);
//JPQL语句中的Person不是表名,是实体类的类名
@Query("select p from Person p where p.name=:name") //JPQL语句,对象查询,完全支持面向对象
public Person getPerson1(@Param("name") String name);
@Query("select p from Person p where p.name=?1 and p.age=?2")
public Person getByNameIsAndAgeIs1(@Param("name") String name, @Param("age") int age);
@Query("select p from Person p where p.name like %:name%")
public Person getNameslike(@Param("name") String name);
//nativeQuery:表明这条语句是sql语句,而不是jpql语句
@Query(value = "select * from person where length(name)=6 ", nativeQuery = true) //SQL语句
public List getAllPersonByNameLengthSix();
}
@RestController
@RequestMapping(value = "/person")
public class PersonController {
@Autowired
PersonRepository personRepository;
@PostMapping(path = "addPerson")
public void addPerson(@RequestBody Person person) {
//保存新的用户
personRepository.save(person);
}
@GetMapping(path = "getAllPerson")
public List getPerson() {
return personRepository.findAll();
}
@DeleteMapping("deletePerson")
public void deletePerson(@RequestParam Long id) {
personRepository.deleteById(id);
}
@PutMapping(path = "updatePerson")
public void updatePerson(@RequestBody Person person) {
personRepository.saveAndFlush(person);
}
}
路径解析:
http://localhost:8080/person/addPerson :json格式传参
http://localhost:8080/person/getAllPerson
http://localhost:8080/person/deletePerson
http://localhost:8080/person/updatePerson
@RestController
@RequestMapping("/person2")
public class Person2Controller {
@Autowired
PersonRepository personRepository;
@GetMapping("findByNameIs/{name}")
public Person findByNameIs(@PathVariable String name) {
return personRepository.findByNameIs(name);
}
@GetMapping("findByNameIsAndAgeIs/{name}/{age}")
public Person findByNameIsAndAgeIs(@PathVariable String name, @PathVariable int age) {
return personRepository.findByNameIsAndAgeIs(name, age);
}
@GetMapping("findByNameContaining/{name}")
public Person findByNameContaining(@PathVariable String name) {
return personRepository.findByNameContaining(name);
}
}
路径解析:
http://localhost:8080/person2/findByNameIs/张思思
http://localhost:8080/person2/findByNameIsAndAgeIs/李思思/20
http://localhost:8080/person2/findByNameContaining/李思思
@RestController
@RequestMapping("/person3")
public class Person3Controller {
@Autowired
PersonRepository personRepository;
@GetMapping("getPerson1/{name}")
public Person getPerson1(@PathVariable String name) {
return personRepository.getPerson1(name);
}
//http://127.0.0.1/8080/person3/getByNameIsAndAgeIs1?name=值&&age=值
@GetMapping("getByNameIsAndAgeIs1")
public Person getByNameIsAndAgeIs1(String name, int age) {
return personRepository.getByNameIsAndAgeIs1(name, age);
}
@GetMapping("getNameslike")
public Person getNameslike(String name) {
return personRepository.getNameslike(name);
}
@GetMapping("getAllPersonByNameLengthSix")
public List getAllPersonByNameLengthSix() {
return personRepository.getAllPersonByNameLengthSix();
}
}
路径解析:
http://localhost:8080/person3/getByNameIsAndAgeIs1?name=李思思&age=20
http://localhost:8080/person3/getNameslike?name=李四 :精确查询
http://localhost:8080/person3/getAllPersonByNameLengthSix :获取长度为6的对象集
在本项目中加入resr依赖包
org.springframework.boot
spring-boot-starter-data-rest
//@RepositoryRestResource(path = "mypersons",exported = false)
//指定访问路径
@RepositoryRestResource(path = "mypersons")
public interface PersonDao2 extends JpaRepository {
//exported:禁止使用该路径访问,false直接该方法局访问不了了,也就是模糊查询不存在了
@RestResource(path = "findbynames", exported = false)
public List findByNameContains(@Param("name") String name);//模糊查询
//RestResource 指定访问路径
@RestResource(path = "findbyname")
public Person findByNameEquals(@Param("name") String name); //精准查询
}
路径:
http://localhost:8080/mypersons :全部查询
结果展示:
路径解析:
查询所有的人:
http://localhost:8080/mypersons
get请求
查询指定的页码,指定每页显示记录数:
http://localhost:8080/mypersons?page=0&size=10
按照指定属性排序:按照id降序输出,第一页,显示10条
http://localhost:8080/mypersons?page=0&size=10&sort=id,desc
按照指定的id查询(GET): 按照id为5查询person对象
http://localhost:8080/mypersons/5
修改指定的人(PUT): 按照id为5可以json格式修改姓名和年龄
http://localhost:8080/mypersons/5
删除指定的人(DELETE):
http://localhost:8080/mypersons/5
自定义请求路径:
@RepositoryRestResource(path = "mypersons")
http://localhost:8080/mypersons/8
自定义访问方法:@RestResource注解
@RestResource(path = "findbynames")
@RestResource(path = "findbyname")
http://localhost:8080/mypersons/search/findbynames?name=敏 模糊查询,上边禁用了哦
http://localhost:8080/mypersons/search/findbyname?name=张敏 精准查询