JpaRepository繼承自PagingAndSortingRepository接口,JpaRepository基于JPA的Repository接口,極大減少了JPA作為數(shù)據(jù)訪問(wèn)的代碼,JpaRepository是實(shí)現(xiàn)Spring Data JPA技術(shù)訪問(wèn)數(shù)據(jù)庫(kù)的關(guān)鍵接口。
示例:簡(jiǎn)單條件查詢(通過(guò)eclipse軟件實(shí)現(xiàn))
選擇基本依賴
配置數(shù)據(jù)庫(kù)
#spring配置 spring : #配置數(shù)據(jù) datasource : url : jdbc:mysql://127.0.0.1:3306/zhuangzi?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai username : root password : root driver-class-name : com.mysql.cj.jdbc.Driver #配置視圖 mvc : view : prefix : /WEB-INF/views/ suffix : .jsp resources : static-locations : classpath:/static/, file:F:/java/ #JPA控制臺(tái)打印sql jpa: show-sql: true
注意:
com.mysql.jdbc.Driver 是 mysql5的jdbc驅(qū)動(dòng)
com.mysql.cj.jdbc.Driver 是 mysql6的jdbc驅(qū)動(dòng)
需要注意的是com.mysql.cj.jdbc.Driver是要配置時(shí)區(qū)的,這里配置為 Asia/Shanghai
定義實(shí)體類(lèi)
里面的注解看這里
package com.yt.entity; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY ) private int id; @Column(name = "names", unique = false, nullable = true, length = 50) private String names; @Column(name = "email", length = 30) private String email; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getNames() { return names; } public void setNames(String names) { this.names = names; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } }
定義Repository接口
package com.yt.repository; import org.springframework.data.jpa.repository.JpaRepository; import com.yt.entity.Student; public interface StudentRepository extends JpaRepository<Student,Integer>{ }
實(shí)現(xiàn)service層
StuService.java 定義了基本的增刪改查接口
package com.yt.service; import com.yt.entity.Student; public interface StuService { Student saveStudent(Student stu); void deleteStudent(Student stu); void deleteStudent(int id); Student updateStudent(Student stu); Student findStudent(int id); }
StuServiceImpl.java StuService接口實(shí)現(xiàn)類(lèi)
package com.yt.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.yt.entity.Student; import com.yt.repository.StudentRepository; @Service public class StuServiceImpl implements StuService{ @Autowired private StudentRepository stuRepository; @Override public Student saveStudent(Student stu) { Student stu1 = stuRepository.save(stu); return stu1; } @Override public void deleteStudent(Student stu) { stuRepository.delete(stu); } @Override public void deleteStudent(int id) { stuRepository.deleteById(id); } @Override public Student updateStudent(Student stu) { return stuRepository.save(stu); } @Override public Student findStudent(int id) { return stuRepository.findById(id).get(); } }
實(shí)現(xiàn)控制器層
package com.yt.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.yt.entity.Student; import com.yt.service.StuServiceImpl; @RestController @RequestMapping(value="stu") public class StuController { @Autowired private StuServiceImpl stuSer; @GetMapping("add") private Student addStu(String names, String email) { Student s = new Student(); s.setNames(names); s.setEmail(email); return stuSer.saveStudent(s); } @GetMapping("del") private String delStu(Integer id) { stuSer.deleteStudent(id); return "success"; } @GetMapping("update") private Student updateStu(Integer id, String names,String email) { Student s = new Student(); s.setId(id); s.setNames(names); s.setEmail(email); stuSer.updateStudent(s); return s; } @GetMapping("find.html") private Student findStu(int id) { return stuSer.findStudent(id); } }
運(yùn)行
打開(kāi)瀏覽器,依次訪問(wèn)下面鏈接
新增
http://localhost:8080/stu/add?names=zhuangzi&[email protected]
新增Stu用戶 names為zhuangzi,email為:[email protected]
修改
http://localhost:8080/stu/add?names=zhuangzi123&[email protected]&id=1
將Id為1的用戶names為zhuangzi123,email為[email protected]
刪除
http://localhost:8080/stu/del?id=1
刪除id為1的學(xué)生
查詢
http://localhost:8080/student/find?id=1
查詢Id為1的學(xué)生
Springboot data jpa多條件分頁(yè)排序查詢