spring-data-jpa實現(xiàn)增刪改查以及分頁操作方法
更新時間:2018年02月10日 09:58:54 作者:Smos
下面小編就為大家分享一篇spring-data-jpa實現(xiàn)增刪改查以及分頁操作方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
有幾個坑一定要注意:
實現(xiàn)刪除操作的時候一定要在各層類中 增加
@Transactional
注釋,否則會一直報錯
在自己使用@Query定義操作時,會碰到編譯器報錯,這個時候只需要禁用QL的語法檢查即可
以下是部分代碼:
//Repository
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
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
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實現(xiàn)增刪改查以及分頁操作方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于Spring MVC同名參數(shù)綁定問題的解決方法
Spring MVC中的參數(shù)綁定還是蠻重要的,最近在使用中遇到了同名參數(shù)綁定的問題,想著總結(jié)分享出來,下面這篇文章主要給大家介紹了關(guān)于Spring MVC同名參數(shù)綁定問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08
idea中g(shù)it如何刪除commit提交的log信息
這篇文章主要介紹了idea中g(shù)it如何刪除commit提交的log信息問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Java陷阱之a(chǎn)ssert關(guān)鍵字詳解
這篇文章詳細(xì)介紹了Java陷阱之a(chǎn)ssert關(guān)鍵字,有需要的朋友可以參考一下2013-09-09

