JpaRepository继承自PagingAndSortingRepository接口,JpaRepository基于JPA的Repository接口,极大减少了JPA作为数据访问的代码,JpaRepository是实现Spring Data JPA技术访问数据库的关键接口。
示例:简单条件查询
创建持久化类
程序清单:/jpa/src/main/java/com/dwx/bean/User.java
package com.dwx.bean;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="t_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer userId;
private String userName;
private String sex;
private Integer age;
public User() {}
public User(String userName,String sex,Integer age) {
this.userName=userName;
this.sex=sex;
this.age=age;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
定义数据访问层接口
程序清单:/jpa/src/main/java/com/dwx/repository/UserRepository.java
package com.dwx.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import com.dwx.bean.User;
public interface UserRepository extends JpaRepository {
User findByUserName(String userName);
List findByUserNameAndAge(String userName,Integer age);
List findByUserNameLike(String userName);
}
定义业务层类
程序清单:/jpa/src/main/java/com/dwx/service/UserService.java
package com.dwx.service;
import java.util.List;
import javax.annotation.Resource;
import javax.transaction.Transactional;
import org.springframework.stereotype.Service;
import com.dwx.bean.User;
import com.dwx.repository.UserRepository;
@Service
public class UserService {
@Resource
private UserRepository userRepository;
@Transactional
public void saveAll(List users) {
userRepository.saveAll(users);
}
public User getUserByUserName(String userName) {
return userRepository.findByUserName(userName);
}
public List getUserByUserNameAndAge(String userName,Integer age){
return userRepository.findByUserNameAndAge(userName, age);
}
public List getUserByUserNameLike(String userName){
return userRepository.findByUserNameLike(userName);
}
}
定义控制器类
程序清单:/jpa/src/main/java/com/dwx/controller/UserController.java
package com.dwx.controller;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dwx.bean.User;
import com.dwx.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@RequestMapping("/save")
public String save() {
User user1=new User("john","男",25);
User user2=new User("lucy","女",18);
User user3=new User("Tom","男",16);
List users=new ArrayList<>();
users.add(user1);
users.add(user2);
users.add(user3);
userService.saveAll(users);
return "保存成功";
}
@RequestMapping("/userName")
public User getByUserName(String userName){
return userService.getUserByUserName(userName);
}
@RequestMapping("/userNameAndAge")
public List getByUserNameAndAge(String userName,Integer age){
return userService.getUserByUserNameAndAge(userName, age);
}
@RequestMapping("/userNameLike")
public List getByUserNameLike(String userName){
return userService.getUserByUserNameLike(userName);
}
}
测试应用
添加用户信息,在浏览器中输入以下地址:http://localhost:8080/user/save
根据姓名和年龄查询,在浏览器中输入以下地址:http://localhost:8080/user/userNameAndAge?userName=john&age=25
根据姓名模糊查询,在浏览器中输入以下地址:http://localhost:8080/user/userNameLike?userName=jack