我是靠谱客的博主 善良小懒猪,最近开发中收集的这篇文章主要介绍JpaRepository 简单条件查询,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

      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, Integer> {
User findByUserName(String userName);
List<User> findByUserNameAndAge(String userName,Integer age);
List<User> 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<User> users) {
userRepository.saveAll(users);
}
public User getUserByUserName(String userName) {
return userRepository.findByUserName(userName);
}
public List<User> getUserByUserNameAndAge(String userName,Integer age){
return userRepository.findByUserNameAndAge(userName, age);
}
public List<User> 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<User> 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<User> getByUserNameAndAge(String userName,Integer age){
return userService.getUserByUserNameAndAge(userName, age);
}
@RequestMapping("/userNameLike")
public List<User> 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

最后

以上就是善良小懒猪为你收集整理的JpaRepository 简单条件查询的全部内容,希望文章能够帮你解决JpaRepository 简单条件查询所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部