SpringBoot集成Jpa
Jpa的介绍
Jpa是SUN官方提出的Java持久化规范。它为开发人员提供了一种对象/关联映射工
具来管理Java应用中的数据关系。主要是为简化现有的持久化开发工作和整合OR
M技术结束再Hibernate,TopLink,JDO等ORM框架各自为营的局面。JPA是再充分吸
收了现这些ORM框架的基础上发展而来的,具有易于使用,伸缩性强等优点。
注意:JPA是一套规范,不是一套产品。
Spring data jpa
Spring Data JPA是Spring基于ORM框架、Jpa规范的基础之上封装的一套Jpa应用
框架,可以让开发者用极简的代码就能实现对数据的访问和操作。它提供了包括CR
UD等在内的常用功能,且易于扩展。 使用时需要继承JpaRepository!
/**
*
* @author Mr.qian
*
*/
public interface UserRepository extends JpaRepository<User, Long>{
/**
* 通过姓名和年龄查询
*/
public User findByNameAndAge(String name,Integer age);
}
Jpa的基本查询
方法名解析
实体类-user
package com.qc.springboot.dao;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author Mr.qian
*
*/
@Entity
@Table(name="user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="name")
private String name;
@Column(name="age")
private Integer age;
@Column(name="sex")
private Integer sex;
@Column(name="password")
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
注解介绍
@Entity 代表这个类是实体类
@Table name属性对应数据库中的表名
@Id 主键
@Column name属性对应表中的列名
@GeneratedValue注解
生成实体类唯一标识的主键、提供主键生成策略。
@GeneratedValue有两个属性,strategy和generator.
generator属性的值是一个字符串,默认为"",其声明了主键生成器的名称
(对应于同名的主键生成器@SequenceGenerator和@TableGenerator)。
strategy属性:提供四种值:-AUTO主键由程序控制, 是默认选项 .
-IDENTITY 主键由数据库生成, 采用数据库自增长, Oracle不支持这种方式
-SEQUENCE 通过数据库的序列产生主键, MYSQL 不支持
-Table 提供特定的数据库产生主键, 该方式更有利于数据库的移植
默认SpringBoot的@GeneratedValue 是不需要加参数的,但是如果数据库
控制主键自增 (auto_increment), 不加参数就会报错.
DAO层-UserRepository
package com.qc.springboot.repository;
import java.io.Serializable;
import org.springframework.data.jpa.repository.JpaRepository;
import com.qc.springboot.dao.User;
/**
*
* @author Mr.qian
*
*/
public interface UserRepository extends JpaRepository<User, Long>{
/**
* 通过姓名和年龄查询
*/
public User findByNameAndAge(String name,Integer age);
}
JpaRepository<User, Long> 第一个参数:实体类,第二个参数主键类型
控制层-JpaController
package com.qc.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.qc.springboot.dao.User;
import com.qc.springboot.service.UserService;
@RestController
@RequestMapping("/jpa")
public class JpaController {
@Autowired
private UserService userService;
/**
* 通过姓名和年龄查询
*/
@RequestMapping("/getByNameAndAge")
public User getByNameAndAge(String name, Integer age) {
return userService.getByNameAndAge(name, age);
}
}
处理层-UserService
package com.qc.springboot.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qc.springboot.dao.User;
import com.qc.springboot.repository.UserRepository;
/**
*
* @author Mr.qian
*
*/
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
/**
* 通过姓名和年龄查询
*/
public User getByNameAndAge(String name, Integer age) {
return userRepository.findByNameAndAge(name, age);
}
}