服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - spring-data-jpa实现增删改查以及分页操作方法

spring-data-jpa实现增删改查以及分页操作方法

2021-04-04 14:21Smos JAVA教程

下面小编就为大家分享一篇spring-data-jpa实现增删改查以及分页操作方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

有几个坑一定要注意:

实现删除操作的时候一定要在各层类中 增加

?
1
@Transactional

注释,否则会一直报错

在自己使用@Query定义操作时,会碰到编译器报错,这个时候只需要禁用QL的语法检查即可

以下是部分代码:

//Repository

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.example.myproject.dao;
import com.example.myproject.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import javax.transaction.Transactional;
import java.util.List;
/**
 * Created by lenovo on 2017/4/19.
 */
@Transactional
public interface UserRepository extends JpaRepository<User,Long> {
 
  void deleteById(int id);
  void deleteByName(String name);
  void deleteBySex(String sex);
  void deleteByAge(int age);
  void removeByNameAndAge(String name, int age);
  @Modifying
  @Query(value = "update user u set u.name = :newName where u.name = :oldName",nativeQuery = true)
  void updateNameByName(@Param("oldName") String oldName, @Param("newName") String newName);
  List<User> findAll();
  Page<User> findAll(Pageable pageable);
  List<User> findByName(String name);
  List<User> findBySex(String sex);
  List<User> findByAge(int age);
  User findByNameAndSex(String name, String sex);
}

//Controller

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.example.myproject.web;
 
import com.example.myproject.dao.UserRepository;
import com.example.myproject.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import javax.transaction.Transactional;
import java.util.List;
 
/**
 * Created by lenovo on 2017/4/19.
 */
@RestController
@RequestMapping("/")
@Transactional
public class UserController {
 @Autowired
 private UserRepository userRepository;
 
 @RequestMapping(value = "/userSave")
 public String userSave(@RequestParam String name,@RequestParam String sex,@RequestParam int age) {
  User user = new User();
  user.setName(name);
  user.setAge(age);
  user.setSex(sex);
  userRepository.save(user);
  return "true";
 }
 
 @RequestMapping(value = "/deleteById")
 public String deleteById(@RequestParam int id) {
  userRepository.deleteById(id);
  return "success";
 }
 
 @RequestMapping(value = "/deleteByName")
 public String deleteByName(@RequestParam String name) {
  userRepository.deleteByName(name);
  return "success";
 }
 
 @RequestMapping(value = "/deleteBySex")
 public String deleteBySex(@RequestParam String sex) {
  userRepository.deleteBySex(sex);
  return "success";
 }
 
 @RequestMapping(value = "/deleteByAge")
 public String deleteByAge(@RequestParam int age) {
  userRepository.deleteByAge(age);
  return "success";
 }
 
 @RequestMapping(value = "/deleteByNameAndAge")
 public String deleteByNameAndAge(@RequestParam String name, @RequestParam int age) {
  userRepository.removeByNameAndAge(name,age);
  return "success";
 }
 
 @RequestMapping(value = "/updateNameByName")
 public String updateNameByName(@RequestParam String oldName, @RequestParam String newName) {
  userRepository.updateNameByName(oldName,newName);
  return "success";
 }
 
 @RequestMapping(value = "/findall")
 public List<User> findAll() {
  List<User> l =userRepository.findAll();
  return l;
 }
 
 @RequestMapping(value = "/findallByPage")
 public Page<User> findAllByPage() {
  Sort sort = new Sort(Sort.Direction.ASC,"id");
  Pageable pageable = new PageRequest(2,6,sort);
  Page<User> page = userRepository.findAll(pageable);
  return page;
 }
 
 @RequestMapping(value = "/findByName")
 public List<User> findByName(@RequestParam String name) {
  List<User> list =userRepository.findByName(name);
  return list;
 }
 
 @RequestMapping(value = "/findByAge")
 public List<User> findByAge(@RequestParam int age) {
  List<User> list =userRepository.findByAge(age);
  return list;
 }
 
 @RequestMapping(value = "/findBySex")
 public List<User> findBySex(@RequestParam String sex) {
  List<User> list =userRepository.findBySex(sex);
  return list;
 }
 
 @RequestMapping(value = "/findByNameAndSex")
 public User findByNameAndSex(@RequestParam String name, @RequestParam String sex) {
  User list =userRepository.findByNameAndSex(name,sex);
  return list;
 }
}

//User

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.example.myproject.domain;
import javax.persistence.*;
/**
 * Created by lenovo on 2017/4/19.
 */
@Entity
public class User {
 @Id
 @GeneratedValue()
 private Integer id;
 @Column
 private String name;
 @Column
 private String sex;
 @Column
 private int age;
 public Integer getId() {
  return id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
}

以上这篇spring-data-jpa实现增删改查以及分页操作方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/m0_37770300/article/details/70244517

延伸 · 阅读

精彩推荐