<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          精通 Spring Boot 系列文(8)

          共 4222字,需瀏覽 9分鐘

           ·

          2020-07-27 18:53

          閱讀全文,約 9 分鐘

          精通 Spring Boot 系列文(1)
          精通 Spring Boot 系列文(2)
          精通 Spring Boot 系列文(2)
          精通 Spring Boot 系列文(4)
          精通 Spring Boot 系列文(5)
          精通 Spring Boot 系列文(6)
          精通 Spring Boot 系列文(7)

          Spring Data JPA

          使用:將數據訪問層接口實現 JpaRepository 接口即可完成 Spring Data JPA 訪問數據。

          JpaRepository 極大簡化了 JPA 作為數據訪問的代碼。

          今天給大家介紹幾個案例:

          1. 簡單條件查詢

          2. 關聯查詢和 @Query 查詢

          3. @NamedQuery 查詢

          4. Specification 查詢

          案例1:簡單條件的查詢

          1)編輯 pom.xml 文件(與 CrudRepository 接口案例一樣)
          2)編輯 application.properties 文件(與 CrudRepository 接口案例一樣)
          3)創(chuàng)建 Student 持久化類
          package?nx.bean;

          import?java.io.Serializable;

          import?javax.persistence.Entity;
          import?javax.persistence.GeneratedValue;
          import?javax.persistence.GenerationType;
          import?javax.persistence.Id;
          import?javax.persistence.Table;
          //?學生
          @Entity
          @Table(name="tb_student")?//?學生表
          public?class?Student?implements?Serializable{

          ????private?static?final?long?serialVersionUID?=?1L;

          ????@Id
          ????@GeneratedValue(strategy?=?GenerationType.IDENTITY)
          ????private?int?id;?
          ????private?String?name?;?//?名字
          ????private?String?address?;?//?地址
          ????private?int?age?;?//?年齡
          ????private?char?sex;?//?性別

          ????//?getXxx?&?setXxx?方法
          }
          4)創(chuàng)建 StudentRepository 數據訪問接口
          package?nx.repository;

          import?java.util.List;

          import?nx.bean.Student;
          import?org.springframework.data.jpa.repository.JpaRepository;

          public?interface?StudentRepository?extends?JpaRepository<Student,?Integer>?{

          ????/**
          ?????*?通過學生姓名來查詢學生對象
          ?????*?此方法相當于JPQL語句代碼
          ?????*?select?s?from?Student?s?where?s.name?=??1
          ?????*/

          ????Student?findByName(String?name);

          ????/**
          ?????*?通過名字和地址查詢學生信息
          ?????*?此方法相當于JPQL語句代碼
          ?????*?select?s?from?Student?s?where?s.name?=??1?and?s.address=?2
          ?????*/

          ????List?findByNameAndAddress(String?name?,?String?address);

          ????/**
          ?????*?通過學生姓名模糊查詢學生信息?
          ?????*?此方法相當于JPQL語句代碼
          ?????*?select?s?from?Student?s?where?s.name?like??1
          ?????*/

          ????List?findByNameLike(String?name);

          }
          5)創(chuàng)建 StudentService 業(yè)務層類
          package?nx.service;

          import?java.util.List;

          import?javax.annotation.Resource;

          import?nx.bean.Student;
          import?nx.repository.StudentRepository;
          import?org.springframework.stereotype.Service;
          import?org.springframework.transaction.annotation.Transactional;
          @Service
          public?class?StudentService?{

          ????//?注入數據訪問層接口對象?
          ????@Resource
          ????private?StudentRepository?studentRepository;

          ????//?保存所有
          ????@Transactional
          ????public?void?saveAll(List?students)?{
          ????????studentRepository.saveAll(students);
          ????}

          ????//?根據名字查找
          ????public?Student?getStuByName(String?name)?{
          ????????return?studentRepository.findByName(name);
          ????}

          ????//?根據名字和地址查找
          ????public?List?getStusByNameAndAddress(String?name,String?address)?{
          ????????return?studentRepository.findByNameAndAddress(name,address);
          ????}

          ????//?根據名字模糊查詢
          ????public?List?getStusByNameLike(String?name)?{
          ????????return?studentRepository.findByNameLike("%"+name+"%");
          ????}
          }
          6)創(chuàng)建 StudentController 控制器類
          package?nx.controller;

          import?java.util.ArrayList;
          import?java.util.List;
          import?javax.annotation.Resource;
          import?nx.bean.Student;
          import?nx.service.StudentService;
          import?org.springframework.web.bind.annotation.RequestMapping;
          import?org.springframework.web.bind.annotation.RestController;

          @RestController
          @RequestMapping("/student")
          public?class?StudentController?{

          ????//?注入StudentService
          ????@Resource
          ????private?StudentService?studentService;

          ????@RequestMapping("/save")
          ????public?String?save()?{
          ????????Student?s1?=?new?Student();
          ????????s1.setAddress("廣州");
          ????????s1.setName("帥帥");
          ????????s1.setAge(18);
          ????????s1.setSex('男');

          ????????Student?s2?=?new?Student();
          ????????s2.setAddress("廣州");
          ????????s2.setName("花花");
          ????????s2.setAge(17);
          ????????s2.setSex('女');

          ????????List?students?=?new?ArrayList<>();
          ????????students.add(s1);
          ????????students.add(s2);

          ????????studentService.saveAll(students);
          ????????return?"保存student對象成功";
          ????}

          ????@RequestMapping("/name")
          ????public?Student?getByName(String?name)?{
          ????????return?studentService.getStuByName(name);
          ????}

          ????@RequestMapping("/nameAndAddress")
          ????public?List?getByNameAndAddress(String?name,String?address)?{
          ????????return?studentService.getStusByNameAndAddress(name,?address);
          ????}

          ????@RequestMapping("/nameLike")
          ????public?List?getByNameLile(String?name)?{
          ????????return?studentService.getStusByNameLike(name);
          ????}???
          }
          7)訪問測試

          http://localhost:8080/student/nameLike?name=帥

          未完待續(xù),等我下一篇?~~~


          Java后端編程

          更多Java推文,關注公眾號

          瀏覽 28
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  天天色色插插综合视频 | 黄色操逼网 | 九一精品网站 | 爱搞搞就搞搞 | 青娱乐在线视频自拍好爽好舒服啊 |