小編給大家分享一下spring-data-jpa如何實(shí)現(xiàn)增刪改查以及分頁操作,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
成都創(chuàng)新互聯(lián)始終堅(jiān)持【策劃先行,效果至上】的經(jīng)營理念,通過多達(dá)10年累計(jì)超上千家客戶的網(wǎng)站建設(shè)總結(jié)了一套系統(tǒng)有效的全網(wǎng)營銷解決方案,現(xiàn)已廣泛運(yùn)用于各行各業(yè)的客戶,其中包括:成都不銹鋼雕塑等企業(yè),備受客戶贊許。
有幾個(gè)坑一定要注意:
實(shí)現(xiàn)刪除操作的時(shí)候一定要在各層類中 增加
@Transactional
注釋,否則會(huì)一直報(bào)錯(cuò)
在自己使用@Query定義操作時(shí),會(huì)碰到編譯器報(bào)錯(cuò),這個(gè)時(shí)候只需要禁用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{ 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 findAll(); Page findAll(Pageable pageable); List findByName(String name); List findBySex(String sex); List 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 ListfindAll() { List l =userRepository.findAll(); return l; } @RequestMapping(value = "/findallByPage") public Page findAllByPage() { Sort sort = new Sort(Sort.Direction.ASC,"id"); Pageable pageable = new PageRequest(2,6,sort); Page page = userRepository.findAll(pageable); return page; } @RequestMapping(value = "/findByName") public List findByName(@RequestParam String name) { List list =userRepository.findByName(name); return list; } @RequestMapping(value = "/findByAge") public List findByAge(@RequestParam int age) { List list =userRepository.findByAge(age); return list; } @RequestMapping(value = "/findBySex") public List findBySex(@RequestParam String sex) { List 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; } }
看完了這篇文章,相信你對(duì)“spring-data-jpa如何實(shí)現(xiàn)增刪改查以及分頁操作”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!