springboot-data-jpa+vue实现前后端分离增删改分页查询

springboot-data-jpa+vue实现前后端分离增删改分页查询

废话不多说直接上代码

一.后台

1.实体类(pojo)

package com.wentao.springbootvue.pojo;

import lombok.Data;

import javax.persistence.*;

/**
 * @BelongsProject: springdatajpaday1
 * @BelongsPackage: com.wentao.springdatajpaday1.pojo
 * @Author: 13274
 * @CreateTime: 2019-06-03 14:22
 * @Description: ${Description}
 */

@Entity
@Table(name = "student")
@Data
public class Student {
    //id表示主键 主键有生成策略GenerationType.IDENTITY
    //GenerationType.AUTO
    //Oracle中是没有自动增长的设置SEQUENCE 使用序列进行增长
    //GeneratedValue自动增长生成的values的值
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private  Integer id;//学生id

    // @Column可以写可以不写
    @Column(name = "name",columnDefinition = "varchar(25) comment '姓名'")
    private String name;//姓名

    @Column
    private  String sex;//性别

    @Column
    private Integer gradeId;//年级id

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getGradeId() {
        return gradeId;
    }

    public void setGradeId(Integer gradeId) {
        this.gradeId = gradeId;
    }
}

2.dao层(dao)

package com.wentao.springbootvue.dao;

import com.wentao.springbootvue.pojo.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
 * @BelongsProject: springdatajpaday1
 * @BelongsPackage: com.wentao.springdatajpaday1.dao
 * @Author: 13274
 * @CreateTime: 2019-06-03 14:33
 * @Description: ${Description}
 */
public interface StudentDao extends JpaRepository, JpaSpecificationExecutor {
}

3.业务层(service)

接口

package com.wentao.springbootvue.service;


import com.wentao.springbootvue.pojo.Student;
import org.springframework.data.domain.Page;

import java.util.List;

/**
 * @BelongsProject: springdatajpaday1
 * @BelongsPackage: com.wentao.springdatajpaday1.service
 * @Author: 13274
 * @CreateTime: 2019-06-03 14:36
 * @Description: ${Description}
 */
public interface StudentService {
    /**
     * 增加学生的方法
     * @param student 要增加学生的对象
     * @return 返回学生对象
     */
    Student add(Student student);

    /**
     * 修改学生的对象
     * @param student 要增加学生的对象
     * @return 返回学生对象
     */
    Student  upd(Student student);

    /**
     * 查询学生对象
     * @return 返回查询到集合
     */
    List seAll();

    /**
     * 删除学生的对象
     * @param id 要删除学生的id
     * @return 返回学生对象
     */
    void del(Integer id);

    /**
     * 根据id查询学生对象
     * @param id 要查询的id
     * @return 返回查询的对象
     */
    Student selById(Integer id);

    /**
     * 分页查询
     * @param pageNum
     * @param size
     * @return
     */
    Page findByPage(Integer pageNum, Integer size);
}

4.接口实现(impl)

package com.wentao.springbootvue.service.impl;


import com.wentao.springbootvue.dao.StudentDao;
import com.wentao.springbootvue.pojo.Student;
import com.wentao.springbootvue.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @BelongsProject: springdatajpaday1
 * @BelongsPackage: com.wentao.springdatajpaday1.service.impl
 * @Author: 13274
 * @CreateTime: 2019-06-03 14:41
 * @Description: ${Description}
 */
@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    StudentDao studentDao;
    /**
     * 增加学生对象
     * @param student 要增加学生的对象
     * @return 返回学生对象
     */
    @Override
    public Student add(Student student) {
        Student s=studentDao.save(student);
        return s;
    }

    /**
     * 修改学生对象
     * @param student 要修改学生的对象
     * @return返回学生对象
     */
    @Override
    public Student upd(Student student) {
        return studentDao.save(student);
    }

    /**
     * 查询学生
     * @return 返回查询到的集合
     */
    @Override
    public List seAll() {
        return studentDao.findAll();
    }

    /**
     * 删除学生
     * @param id 要删除学生的id
     */
    @Override
    public void del(Integer id) {

         studentDao.deleteById(id);
    }

    /**
     * 根据id查询学生
     * @param id 要查询的id
     * @return
     */
    @Override
    public Student selById(Integer id) {
        return studentDao.findById(id).get();
    }

    /**
     * 分页查询
     * @param pageNum
     * @param size
     * @return
     */
    @Override
    public Page findByPage(Integer pageNum, Integer size) {
        PageRequest pageRequest = PageRequest.of(pageNum - 1, size);
       Page page= studentDao.findAll(pageRequest);
        return page;
    }
}

5.控制层(controller)

package com.wentao.springbootvue.controller;

import com.wentao.springbootvue.pojo.Student;
import com.wentao.springbootvue.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: springdatajpaday1
 * @BelongsPackage: com.wentao.springdatajpaday1.controller
 * @Author: 13274
 * @CreateTime: 2019-06-03 14:55
 * @Description: ${Description}
 */
@RestController
@Api(value = "swagger ui 注释 api 级别")
public class StudentController {
    @Autowired
    StudentService studentService;
    Integer count;
   @GetMapping("sell")
   @ApiOperation(value = "查询所有学生",notes = "查询所有学生")
   public Map selAll(Integer pageNum){
       if (pageNum==null||pageNum==0){
           pageNum=1;
       }
       if (count!=null&&pageNum>=count){
            pageNum=count;
       }
       Page byPage = studentService.findByPage(pageNum, 2);
       count= byPage.getTotalPages();
       Map map =new HashMap();
       map.put("data",byPage);
       map.put("pageNum",pageNum);
       return map;
   }

   

    @DeleteMapping("del")
    public  int del(Integer id){
        try {
            studentService.del(id);
            return 1;
        } catch (Exception e) {
            return 0;
        }

    }

    @PostMapping("add")
    public Student add(@RequestBody Student student){
        return studentService.add(student);
    }
//    @RequestMapping("selByid")
//    public String selByid(Integer id,Model model){
//        Student student=studentService.selById(id);
//        model.addAttribute("student",student);
//        return "upd";
//    }
    @PutMapping("upd")
    public Student modifyStudent(Student student){
        return studentService.upd(student);
    }

}

6.备置层(config)

package com.wentao.springbootvue.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.wentao.springbootvue.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2实现前后端分离开发")
                .description("此项目只是练习如何实现前后端分离开发的小Demo")
                .termsOfServiceUrl("https://me.csdn.net/qq_42805685")
                .contact("吴文涛")
                .version("1.0")
                .build();
    }
}

package com.wentao.springbootvue.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    //跨域配置
    @Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            //重写父类提供的跨域请求处理的接口
            public void addCorsMappings(CorsRegistry registry) {
                //添加映射路径
                registry.addMapping("/**")
                        //放行哪些原始域
                        .allowedOrigins("*")
                        //是否发送Cookie信息
                        .allowCredentials(true)
                        //放行哪些原始域(请求方式)
                        .allowedMethods("GET", "POST", "PUT", "DELETE")
                        //放行哪些原始域(头部信息)
                        .allowedHeaders("*")
                        //暴露哪些头部信息(因为跨域访问默认不能获取全部头部信息)
                        .exposedHeaders("Header1", "Header2");
            }
        };
    }
}

7.yml配置

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/studentjpa?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
    password: root
    username: root
  jpa:
    database: mysql
    hibernate:
      ddl-auto: update
      naming:
        physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
        implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl
    show-sql: true

8.pom依赖包



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
         
    
    com.wentao
    springbootvue
    0.0.1-SNAPSHOT
    springbootvue
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            mysql
            mysql-connector-java
            runtime
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
        
            io.springfox
            springfox-swagger2
            2.7.0
        
        
            io.springfox
            springfox-swagger-ui
            2.7.0
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



上结构图

springboot-data-jpa+vue实现前后端分离增删改分页查询_第1张图片

二. 前台



	
		
		
		
		
		
		
	
	
		
编号 姓名 性别 年级 操作
首页 上一页 下一页 尾页

你可能感兴趣的:(springboot-data-jpa+vue实现前后端分离增删改分页查询)