美文网首页
SpringBoot之整合JPA

SpringBoot之整合JPA

作者: 赵柯里昂 | 来源:发表于2017-10-10 17:18 被阅读0次

    1.添加依赖

            <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
    

    2.在application.yml文件中配置数据源(ddl-auto:建表时使用create , 其他时候用update )

    spring:
      profiles:
        active: dev
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/dbstudent
        username: root
        password: root
      jpa:
        hibernate:
          ddl-auto: update
        show-sql: true
    

    3.创建Student实体类,id自动增长

    package com.zhizhuoedu.student.entity;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    /**
     * @Author: zhao.jw
     * @Description:
     * @Date: Created in 2017-10-10 15:08
     * @Modified By:
     */
    @Entity
    public class Student {
    
        @Id
        @GeneratedValue
        private Integer id;
    
        private String name;
    
        private Integer age;
    
        public Student() {
        }
    
        public Student(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    
        public Student(Integer id,String name, Integer age) {
            this.id=id;
            this.name = name;
            this.age = age;
        }
    
        public Integer getId() {
            return id;
        }
    
        public void setId(Integer id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public Integer getAge() {
            return age;
        }
    
        public void setAge(Integer age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    

    4.IStudentDao ,可自己增加根据某个属性查询的抽象方法,格式为findByXXX

    package com.zhizhuoedu.student.dao;
    
    import com.zhizhuoedu.student.entity.Student;
    import org.springframework.data.jpa.repository.JpaRepository;
    
    import java.util.List;
    
    /**
     * @Author: zhao.jw
     * @Description:
     * @Date: Created in 2017-10-10 15:55
     * @Modified By:
     */
    public interface IStudentDao  extends JpaRepository<Student,Integer>{
    
        //通过年龄来查询
        public List<Student> findByAge(Integer age);
    
    }
    

    5.StudentController

    package com.zhizhuoedu.student.controller;
    
    import com.zhizhuoedu.student.dao.IStudentDao;
    import com.zhizhuoedu.student.entity.Student;
    import com.zhizhuoedu.student.service.StudentService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    import java.util.Queue;
    
    /**
     * @Author: zhao.jw
     * @Description:RESTFUL规范对学生信息进行增删改查
     * @Date: Created in 2017-10-10 15:07
     * @Modified By:
     */
    @RestController
    public class StudentController {
    
        @Autowired
        private IStudentDao studentDao;
        @Autowired
        private StudentService studentService;
    
        /**
         * @Author: Zhao.jw
         * @Description:获取学生列表
         */
        @GetMapping("/students")
        public List<Student> getStudents(){
            return studentDao.findAll();
        }
    
        /**
         * @Author: Zhao.jw
         * @Description:根据学生ID获取学生信息
         */
        @GetMapping("/students/{id}")
        public Student getStudentById(@PathVariable("id") Integer id){
            return studentDao.findOne(id);
        }
    
        /**
         * @Author: Zhao.jw
         * @Description:根据学生年龄获取学生列表
         */
        @GetMapping("/students/age/{age}")
        public List<Student> getStudentsByAge(@PathVariable("age") Integer age){
            return studentDao.findByAge(age);
        }
    
        /**
         * @Author: Zhao.jw
         * @Description:添加一个学生
         */
        @PostMapping("/students")
        public String addStudent(String name,Integer age){
            Student entity=new Student(name,age);
            return studentDao.saveAndFlush(entity).toString();
        }
    
        /**
         * @Author: Zhao.jw
         * @Description:根据学生ID更新学生数据
         */
        @PutMapping("/students/{id}")
        public String updateStudent(@PathVariable("id") Integer id, String name,Integer age){
            Student entity=new Student(id,name,age);
            return studentDao.saveAndFlush(entity).toString();
        }
    
        /**
         * @Author: Zhao.jw
         * @Description:根据学生ID删除学生数据
         */
        @DeleteMapping("/students/{id}")
        public void delete(@PathVariable("id") Integer id){
            studentDao.delete(id);
        }
    
    
        @GetMapping("/students/two")
        public void studentsTwo(){
                studentService.insertTwo();
        }
    }
    

    6.StudentService

    package com.zhizhuoedu.student.service;
    
    import com.zhizhuoedu.student.dao.IStudentDao;
    import com.zhizhuoedu.student.entity.Student;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.transaction.annotation.Transactional;
    
    import javax.naming.Name;
    
    /**
     * @Author: zhao.jw
     * @Description:
     * @Date: Created in 2017-10-10 16:31
     * @Modified By:
     */
    @Service
    public class StudentService {
    
        @Autowired
        private IStudentDao studentDao;
    
        @Transactional
        public void insertTwo(){
            Student entityA=new Student("A",29);
            studentDao.save(entityA);
    
            Student entityB=new Student("BB",22);
            studentDao.save(entityB);
        }
    }
    

    相关文章

      网友评论

          本文标题:SpringBoot之整合JPA

          本文链接:https://www.haomeiwen.com/subject/tbcbyxtx.html