SpringDataJpa的使用 -- 增删查改

SpringDataJpa的使用 – 增删查改

上一篇:SpringDataJpa的使用 – 一对一、一对多、多对多 关系映射

本文主要讲述 配置了关系映射的实体类基本的数据操作 增删查改。

简述 save、findAll、deleteById 等方法

方法分为三类:删除(deleteXXX())、查询(findXXX())、持久化及更改(saveXXX())。

以 JpaRepository 接口为例,简述一下 常用的方法。

持久化及更改

save()、saveAndFlush()、saveAll()、saveAllAndFlush() 方法。

方法主要是两种:单条数据操作、批量数据操作。

  • save() 和 saveAndFlush() 用于更新一条数据,区别在于是否在更新数据表后刷新数据(后一个刷新),功能没有其他区别了。

  • saveAll() 和 saveAllAndFlush() 方法用于更新多条数据,其他的与上面的方法无异。

  • insert(添加) 和 update(更改) 都是使用 save 方法的,以 save 为例,当插入的数据类的主键为空时,是 insert 操作;当插入的数据类的主键不为空时,是 update 操作。

删除

delete()、deleteById()、deleteAll()、deleteAllById()、deleteAllByIdInBatch()、deleteAllInBatch()、deleteInBatch() 方法。

方法主要是三种:单条数据删除、批量数据删除。

  • delete() 用于 根据 类实体 删除,InBatch 是 批量的意思,deleteInBatch() 的功能也是这样的。

  • deleteAll() 用于 根据 类实体集 批量删除,如果无参,应该是全部删。(没用过哦)deleteAllInBatch() 同理。

  • deleteById() 用于 根据 ID 删除

  • deleteAllById() 用于 根据 ID集 批量删除,deleteAllByIdInBatch() 同理。

查询

findAll()、findAllById()、findById()、findBy()、findOne() 方法。

方法主要是三种:单条数据查询、批量数据查询、分页查询(待添加)。

  • findById() 用于 根据 ID 查询

  • findAllById() 用于 根据 ID集 批量查询

  • findAll() 用于 批量查询,无参数时,全查,数据按照表数据排序;可以添加参数实现 筛选、排序 等等。

  • findOne() 用于 根据 Example 实体执行比较复杂的 查询

  • findBy() 在 findOne() 的基础上,再添加参数来执行复杂查询。

分页查询 和 查询排序

可以看我的下一篇文章,有比较详细的介绍。

首先,这两个功能都是直接用 findAll() 来实现的,JpaRepository 接口中内置了大量的同名方法,功能颇多。

  • 分页查询,设定 页码、页大小,分批地查询。
package com.example.demo.serve.impl;

import com.example.demo.entity.Student;
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.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl {
	
	public Page<Student> selectPaging (int currentPage, int pageSize) {
		if (currentPage < 0) {
			// 页码,是从 0 开始的,自己试试便知
			currentPage = 0;
		}
		if (pageSize <= 0) {
			// 页数据数(页大小)
			pageSize = 5;
		}
		// 解决 页大小大于表数据数 或 当前要查询的数据量超过表数据数 的问题
		int count = (int) countAll();
		System.out.println(currentPage + "," + pageSize);
		if (currentPage * pageSize == count) {
			currentPage--;
		} else if (currentPage * pageSize > count) {
			currentPage = count / pageSize;
		}
		Pageable pageable = PageRequest.of(currentPage, pageSize);
		return studentRepository.findAll(pageable);
	}
	
	/** 仅展示 相关 方法 和 导入 */
	
}
  • 查询排序,设置排序规则,将结果排序后输出。
package com.example.demo.serve.impl;

import com.example.demo.entity.Student;
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.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl {
	
	/**
	 * 查询并排序(默认升序)
	 *
	 * @param attributeName 属性名(如: studentName、studentAge、...)
	 * @return 结果集
	 */
	public List<Student> selectSort (String attributeName) {
		return studentRepository.findAll(Sort.by(attributeName));
	}
	
	/**
	 * 查询并排序
	 *
	 * @param attributeName 属性名(如: studentName、studentAge、...)
	 * @param isDesc 是否 降序
	 * @return 结果集
	 */
	public List<Student> selectSort (String attributeName, boolean isDesc) {
		// Sort 的 by() 的参数是一到多个,就像写 SQL 语句排序一样,在前者的排序基础上,在对后者排序,有先后之分。
		return isDesc ? studentRepository.findAll(Sort.by(attributeName).descending())
				: selectSort(attributeName);
	}
	
	/** 仅展示 相关 方法 和 导入 */
	
}
  • 分页查询后,再排序输出,即 前面两个的合并。
// 方法省略了,使用 分页 时的方法,更改 Pageable 即可。
// 仅分页时
Pageable pageable = PageRequest.of(currentPage, pageSize);

// 分页并排序时(降序)
Pageable pageable = PageRequest.of(currentPage, pageSize, Sort.by("studentAge").descending());
// 分页并排序时(升序)
Pageable pageable = PageRequest.of(currentPage, pageSize, Sort.by("studentAge"));

配置 并 测试

实现 JpaRepository 接口、添加 业务代码,最后测试。

第一步、配置 操作接口 必须

即,为每个实体类都配置一个 dao 接口,继承 JpaRepository<实体类.class, 对应的主键.class> 接口。

记得分开成一个一个的类。

package com.example.demo.repository;

import com.example.demo.entity.ClassRoom;
import com.example.demo.entity.Patriarch;
import com.example.demo.entity.Student;
import com.example.demo.entity.Teacher;
import org.springframework.data.jpa.repository.JpaRepository;

/**
 * 教师类 操作 接口
 *
 * @author LJM
 */
public interface ClassRoomRepository extends JpaRepository<ClassRoom, Long> {
}

/**
 * 家长类 操作 接口
 *
 * @author LJM
 */
public interface PatriarchRepository extends JpaRepository<Patriarch, Long> {
}

/**
 * 学生类 操作 接口
 *
 * @author LJM
 */
public interface StudentRepository extends JpaRepository<Student, Long> {
}

/**
 * 老师类 操作 接口
 *
 * @author LJM
 */
public interface TeacherRepository extends JpaRepository<Teacher, Long> {
}
第二步、为对应的实体类添加业务接口 非必要

业务接口不是必须的,这只是我的编码习惯。

  • 自定义业务基类

BaseService.java

package com.example.demo.base;

import java.util.List;

/**
 * 数据操作业务 基类
 *
 * @param <E>  实体类的类型
 * @param <PK> 实体类的主键的类型
 *
 * @author LJM
 */
public interface BaseService<E, PK> {
    /**
     * 添加
     *
     * @param e 实体类
     *
     * @return 成功返回 id,失败返回 0
     */
    long save (E e);
    /**
     * 删除
     *
     * @param id 实体类 ID
     *
     * @return 是否成功 1/0
     */
    int deleteById (PK id);
    /**
     * 更改
     *
     * @param e 实体类
     *
     * @return 是否成功 1/0
     */
    int update (E e);
    /**
     * 查询
     *
     * @param id 实体类 ID
     *
     * @return 实体类
     */
    E selectById (PK id);
    /**
     * 批量添加
     *
     * @param eList 实体类 列表
     *
     * @return 成功返回 id,失败返回 0 数组
     */
    long[] saveBatch (List<E> eList);
    /**
     * 批量删除
     *
     * @param ids 实体类 ID 列表
     *
     * @return 是否成功 1/0 数组
     */
    int[] deleteByIdBatch (List<PK> ids);
    /**
     * 批量更改
     *
     * @param eList 实体类 列表
     *
     * @return 是否成功 1/0 数组
     */
    int[] updateBatch (List<E> eList);
    /**
     * 批量查询
     *
     * @param ids 实体类 ID 列表
     *
     * @return 实体类 列表
     */
    List<E> selectByIdBatch (List<PK> ids);
    /**
     * 批量查询 ALL
     *
     * @return 实体类 列表
     */
    List<E> selectAllBatch ();
    
    /**
     * 数目统计
     *
     * @return 数目
     */
    long countAll ();
}
  • 添加业务类接口

记得分开成一个一个的类。

package com.example.demo.serve;

import com.example.demo.base.BaseService;
import com.example.demo.entity.ClassRoom;
import com.example.demo.entity.Patriarch;
import com.example.demo.entity.Student;
import com.example.demo.entity.Teacher;

/**
 * 班级 业务 接口
 *
 * @author LJM
 */
public interface ClassRoomService extends BaseService<ClassRoom, Long> {
}

/**
 * 家长 业务 接口
 *
 * @author LJM
 */
public interface PatriarchService extends BaseService<Patriarch, Long> {
}

/**
 * 学生 业务 接口
 *
 * @author LJM
 */
public interface StudentService extends BaseService<Student, Long> {
}

/**
 * 教师 业务 接口
 *
 * @author LJM
 */
public interface TeacherService extends BaseService<Teacher, Long> {
}
第三步、实现对应的实体类所添加的业务接口 必要

如果没有添加业务接口,那么除去实现即可,其他的几个实现类的方法类似,只需要更改 实现接口 和 导入的dao接口 即可。

package com.example.demo.serve.impl;

import com.example.demo.entity.Teacher;
import com.example.demo.repository.TeacherRepository;
import com.example.demo.serve.TeacherService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import javax.annotation.Resource;

/**
 * 教师 业务 实现
 *
 * @author LJM
 */
@Service
public class TeacherServiceImpl implements TeacherService {
	@Resource
	private TeacherRepository teacherRepository;
	@Override
	public long save (Teacher teacher) {
		teacher.setTeacherId(null);
		Teacher save = teacherRepository.save(teacher);
		return (save.getTeacherId() == null || save.getTeacherId() <= 0)
				? 0L : save.getTeacherId();
	}
	@Override
	public int deleteById (Long id) {
		if (id == null || id <= 0) {
			return 0;
		} else {
			teacherRepository.deleteById(id);
			return 1;
		}
	}
	@Override
	public int update (Teacher teacher) {
		if (teacher.getTeacherId() == null || teacher.getTeacherId() <= 0) {
			return 0;
		} else {
			teacherRepository.save(teacher);
			return 1;
		}
	}
	@Override
	public Teacher selectById (Long id) {
		if (id == null || id <= 0) {
			return null;
		} else {
			Optional<Teacher> byId = teacherRepository.findById(id);
			return byId.orElse(null);
		}
	}
	@Override
	public long[] saveBatch (List<Teacher> teachers) {
		for (Teacher t : teachers) {
			t.setTeacherId(null);
		}
		List<Teacher> teachers1 = teacherRepository.saveAll(teachers);
		long[] returnInts = new long[teachers1.size()];
		int i = 0;
		for (Teacher t : teachers1) {
			returnInts[i++] = (t.getTeacherId() == null || t.getTeacherId() <= 0)
					? 0L : t.getTeacherId();
		}
		return returnInts;
	}
	@Override
	public int[] deleteByIdBatch (List<Long> ids) {
		int[] returnInts = new int[ids.size()];
		int i = 0;
		for (Long id : ids) {
			returnInts[i++] = (id == null || id <= 0) ? 0 : 1;
		}
		teacherRepository.deleteAllById(ids);
		return returnInts;
	}
	@Override
	public int[] updateBatch (List<Teacher> teachers) {
		int[] returnInts = new int[teachers.size()];
		int i = 0;
		for (Teacher t : teachers) {
			returnInts[i++] = (t.getTeacherId() == null || t.getTeacherId() <= 0) ? 0 : 1;
		}
		teacherRepository.saveAll(teachers);
		return returnInts;
	}
	@Override
	public List<Teacher> selectByIdBatch (List<Long> ids) {
		return teacherRepository.findAllById(ids);
	}
	@Override
	public List<Teacher> selectAllBatch () {
		return teacherRepository.findAll();
	}
	@Override
	public long countAll () {
		return teacherRepository.count();
	}
}
第四步、测试 1 单元测试

完成这个步骤即可判断 增删查改 基本操作是否成功,controller 测试可以不看了。

测试类 TeacherServiceImplText.java

package com.example.demo.serve.impl;

import com.example.demo.entity.ClassRoom;
import com.example.demo.entity.Patriarch;
import com.example.demo.entity.Student;
import com.example.demo.entity.Teacher;
import com.example.demo.serve.StudentService;
import com.example.demo.serve.TeacherService;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;

@SpringBootTest
public class TeacherServiceImplTest {
	@Resource
	private StudentService studentService;
	@Resource
	private TeacherService teacherService;
	@Test
	void save () {
		Teacher teacher = new Teacher("黄芳", 34, "女", "英语");
		teacherService.save(teacher);
	}
	@Test
	void save1 () {
		Patriarch patriarch1 = new Patriarch(3L, "家长1", 40, "女", "14712345666", "中国");
		Patriarch patriarch2 = new Patriarch(4L, "家长1", 40, "女", "14712345666", "中国");
		ClassRoom classRoom1 = new ClassRoom(3L, "高二四班1", "A栋三楼", 45, "高二");
		ClassRoom classRoom2 = new ClassRoom(4L, "高二四班2", "A栋三楼", 45, "高二");
		Student student1 = new Student(3L, "11", 15, "男", patriarch1, classRoom1);
		Student student2 = new Student(4L, "11", 15, "男", patriarch2, classRoom2);
		
		List<Student> studentArrayList = new ArrayList<Student>() {{
			add(student1);
			add(student2);
		}};
		Teacher teacher = new Teacher("黄芳15", 34, "女", "英语");
		teacher.setStudentList(studentArrayList);
		teacherService.save(teacher);
	}
	@Test
	void deleteById () {
		int i = teacherService.deleteById(3L);
		System.out.println("成功与否: " + (i == 1));
	}
	@Test
	void update () {
		Teacher teacher = new Teacher(2L, "黄芳", 37, "女", "英语");
		teacherService.update(teacher);
	}
	@Test
	void selectById () {
		System.out.println(teacherService.selectById(1L));
	}
	@Test
	void saveBatch () {
	}
	@Test
	void deleteByIdBatch () {
		List<Long> longs = new ArrayList<Long>() {{
			add(3L);
			add(4L);
		}};
		int[] ints = teacherService.deleteByIdBatch(longs);
		Arrays.stream(ints).forEach(System.out::println);
	}
	@Test
	void updateBatch () {
		List<Teacher> teacherArrayList = new ArrayList<Teacher>() {{
			add(new Teacher(3L, "黄芳11", 37, "女", "英语"));
			add(new Teacher(4L, "黄芳21", 37, "女", "英语"));
		}};
		int[] ints = teacherService.updateBatch(teacherArrayList);
		Arrays.stream(ints).forEach(System.out::println);
	}
	@Test
	void selectByIdBatch () {
		List<Long> longList = new ArrayList<Long>() {{
			add(1L);
			add(2L);
		}};
		List<Teacher> teachers = teacherService.selectByIdBatch(longList);
		teachers.stream().map(Object::toString).forEach(System.out::println);
	}
	@Test
	void selectAllBatch () {
		List<Teacher> teachers = teacherService.selectAllBatch();
		teachers.stream().map(Object::toString).forEach(System.out::println);
	}
	@Test
	void countAll () {
		System.out.println(teacherService.countAll());
	}
}

结果参考:

  • 添加(直接添加,没有额外操作)
Hibernate: insert into teacher (teacher_age, teacher_course, teacher_name, teacher_sex) values (?, ?, ?, ?)
  • 更改(先查,再改,如果数据没有变,即:要更新的数据与表数据一致时,不更改数据)
Hibernate: select teacher0_.teacher_id as teacher_1_4_0_, teacher0_.teacher_age as teacher_2_4_0_, teacher0_.teacher_course as teacher_3_4_0_, teacher0_.teacher_name as teacher_4_4_0_, teacher0_.teacher_sex as teacher_5_4_0_ from teacher teacher0_ where teacher0_.teacher_id=?
Hibernate: select studentlis0_.teacher_id as teacher_2_3_0_, studentlis0_.student_id as student_1_3_0_, student1_.student_id as student_1_2_1_, student1_.class_room_id as class_ro5_2_1_, student1_.patriarch_id as patriarc6_2_1_, student1_.student_age as student_2_2_1_, student1_.student_name as student_3_2_1_, student1_.student_sex as student_4_2_1_, classroom2_.class_room_id as class_ro1_0_2_, classroom2_.class_room_capacity as class_ro2_0_2_, classroom2_.class_room_grade as class_ro3_0_2_, classroom2_.class_room_location as class_ro4_0_2_, classroom2_.class_room_name as class_ro5_0_2_, patriarch3_.patriarch_id as patriarc1_1_3_, patriarch3_.patriarch_address as patriarc2_1_3_, patriarch3_.patriarch_age as patriarc3_1_3_, patriarch3_.patriarch_name as patriarc4_1_3_, patriarch3_.patriarch_phone as patriarc5_1_3_, patriarch3_.patriarch_sex as patriarc6_1_3_ from student_teacher studentlis0_ inner join student student1_ on studentlis0_.student_id=student1_.student_id inner join classroom classroom2_ on student1_.class_room_id=classroom2_.class_room_id inner join patriarch patriarch3_ on student1_.patriarch_id=patriarch3_.patriarch_id where studentlis0_.teacher_id=?
Hibernate: select studentlis0_.class_room_id as class_ro5_2_0_, studentlis0_.student_id as student_1_2_0_, studentlis0_.student_id as student_1_2_1_, studentlis0_.class_room_id as class_ro5_2_1_, studentlis0_.patriarch_id as patriarc6_2_1_, studentlis0_.student_age as student_2_2_1_, studentlis0_.student_name as student_3_2_1_, studentlis0_.student_sex as student_4_2_1_, patriarch1_.patriarch_id as patriarc1_1_2_, patriarch1_.patriarch_address as patriarc2_1_2_, patriarch1_.patriarch_age as patriarc3_1_2_, patriarch1_.patriarch_name as patriarc4_1_2_, patriarch1_.patriarch_phone as patriarc5_1_2_, patriarch1_.patriarch_sex as patriarc6_1_2_ from student studentlis0_ inner join patriarch patriarch1_ on studentlis0_.patriarch_id=patriarch1_.patriarch_id where studentlis0_.class_room_id=?
Hibernate: select teacherlis0_.student_id as student_1_3_0_, teacherlis0_.teacher_id as teacher_2_3_0_, teacher1_.teacher_id as teacher_1_4_1_, teacher1_.teacher_age as teacher_2_4_1_, teacher1_.teacher_course as teacher_3_4_1_, teacher1_.teacher_name as teacher_4_4_1_, teacher1_.teacher_sex as teacher_5_4_1_ from student_teacher teacherlis0_ inner join teacher teacher1_ on teacherlis0_.teacher_id=teacher1_.teacher_id where teacherlis0_.student_id=?
Hibernate: select teacherlis0_.student_id as student_1_3_0_, teacherlis0_.teacher_id as teacher_2_3_0_, teacher1_.teacher_id as teacher_1_4_1_, teacher1_.teacher_age as teacher_2_4_1_, teacher1_.teacher_course as teacher_3_4_1_, teacher1_.teacher_name as teacher_4_4_1_, teacher1_.teacher_sex as teacher_5_4_1_ from student_teacher teacherlis0_ inner join teacher teacher1_ on teacherlis0_.teacher_id=teacher1_.teacher_id where teacherlis0_.student_id=?
Hibernate: select studentlis0_.teacher_id as teacher_2_3_0_, studentlis0_.student_id as student_1_3_0_, student1_.student_id as student_1_2_1_, student1_.class_room_id as class_ro5_2_1_, student1_.patriarch_id as patriarc6_2_1_, student1_.student_age as student_2_2_1_, student1_.student_name as student_3_2_1_, student1_.student_sex as student_4_2_1_, classroom2_.class_room_id as class_ro1_0_2_, classroom2_.class_room_capacity as class_ro2_0_2_, classroom2_.class_room_grade as class_ro3_0_2_, classroom2_.class_room_location as class_ro4_0_2_, classroom2_.class_room_name as class_ro5_0_2_, patriarch3_.patriarch_id as patriarc1_1_3_, patriarch3_.patriarch_address as patriarc2_1_3_, patriarch3_.patriarch_age as patriarc3_1_3_, patriarch3_.patriarch_name as patriarc4_1_3_, patriarch3_.patriarch_phone as patriarc5_1_3_, patriarch3_.patriarch_sex as patriarc6_1_3_ from student_teacher studentlis0_ inner join student student1_ on studentlis0_.student_id=student1_.student_id inner join classroom classroom2_ on student1_.class_room_id=classroom2_.class_room_id inner join patriarch patriarch3_ on student1_.patriarch_id=patriarch3_.patriarch_id where studentlis0_.teacher_id=?
Hibernate: update teacher set teacher_age=?, teacher_course=?, teacher_name=?, teacher_sex=? where teacher_id=?
  • 删除(先查,再删)
Hibernate: select teacher0_.teacher_id as teacher_1_4_0_, teacher0_.teacher_age as teacher_2_4_0_, teacher0_.teacher_course as teacher_3_4_0_, teacher0_.teacher_name as teacher_4_4_0_, teacher0_.teacher_sex as teacher_5_4_0_, studentlis1_.teacher_id as teacher_2_3_1_, student2_.student_id as student_1_3_1_, student2_.student_id as student_1_2_2_, student2_.class_room_id as class_ro5_2_2_, student2_.patriarch_id as patriarc6_2_2_, student2_.student_age as student_2_2_2_, student2_.student_name as student_3_2_2_, student2_.student_sex as student_4_2_2_, classroom3_.class_room_id as class_ro1_0_3_, classroom3_.class_room_capacity as class_ro2_0_3_, classroom3_.class_room_grade as class_ro3_0_3_, classroom3_.class_room_location as class_ro4_0_3_, classroom3_.class_room_name as class_ro5_0_3_, patriarch4_.patriarch_id as patriarc1_1_4_, patriarch4_.patriarch_address as patriarc2_1_4_, patriarch4_.patriarch_age as patriarc3_1_4_, patriarch4_.patriarch_name as patriarc4_1_4_, patriarch4_.patriarch_phone as patriarc5_1_4_, patriarch4_.patriarch_sex as patriarc6_1_4_ from teacher teacher0_ left outer join student_teacher studentlis1_ on teacher0_.teacher_id=studentlis1_.teacher_id left outer join student student2_ on studentlis1_.student_id=student2_.student_id left outer join classroom classroom3_ on student2_.class_room_id=classroom3_.class_room_id left outer join patriarch patriarch4_ on student2_.patriarch_id=patriarch4_.patriarch_id where teacher0_.teacher_id=?
Hibernate: delete from teacher where teacher_id=?
  • 查询(直接查询)
Hibernate: select teacher0_.teacher_id as teacher_1_4_0_, teacher0_.teacher_age as teacher_2_4_0_, teacher0_.teacher_course as teacher_3_4_0_, teacher0_.teacher_name as teacher_4_4_0_, teacher0_.teacher_sex as teacher_5_4_0_, studentlis1_.teacher_id as teacher_2_3_1_, student2_.student_id as student_1_3_1_, student2_.student_id as student_1_2_2_, student2_.class_room_id as class_ro5_2_2_, student2_.patriarch_id as patriarc6_2_2_, student2_.student_age as student_2_2_2_, student2_.student_name as student_3_2_2_, student2_.student_sex as student_4_2_2_, classroom3_.class_room_id as class_ro1_0_3_, classroom3_.class_room_capacity as class_ro2_0_3_, classroom3_.class_room_grade as class_ro3_0_3_, classroom3_.class_room_location as class_ro4_0_3_, classroom3_.class_room_name as class_ro5_0_3_, patriarch4_.patriarch_id as patriarc1_1_4_, patriarch4_.patriarch_address as patriarc2_1_4_, patriarch4_.patriarch_age as patriarc3_1_4_, patriarch4_.patriarch_name as patriarc4_1_4_, patriarch4_.patriarch_phone as patriarc5_1_4_, patriarch4_.patriarch_sex as patriarc6_1_4_ from teacher teacher0_ left outer join student_teacher studentlis1_ on teacher0_.teacher_id=studentlis1_.teacher_id left outer join student student2_ on studentlis1_.student_id=student2_.student_id left outer join classroom classroom3_ on student2_.class_room_id=classroom3_.class_room_id left outer join patriarch patriarch4_ on student2_.patriarch_id=patriarch4_.patriarch_id where teacher0_.teacher_id=?
Hibernate: select studentlis0_.class_room_id as class_ro5_2_0_, studentlis0_.student_id as student_1_2_0_, studentlis0_.student_id as student_1_2_1_, studentlis0_.class_room_id as class_ro5_2_1_, studentlis0_.patriarch_id as patriarc6_2_1_, studentlis0_.student_age as student_2_2_1_, studentlis0_.student_name as student_3_2_1_, studentlis0_.student_sex as student_4_2_1_, patriarch1_.patriarch_id as patriarc1_1_2_, patriarch1_.patriarch_address as patriarc2_1_2_, patriarch1_.patriarch_age as patriarc3_1_2_, patriarch1_.patriarch_name as patriarc4_1_2_, patriarch1_.patriarch_phone as patriarc5_1_2_, patriarch1_.patriarch_sex as patriarc6_1_2_ from student studentlis0_ inner join patriarch patriarch1_ on studentlis0_.patriarch_id=patriarch1_.patriarch_id where studentlis0_.class_room_id=?
Hibernate: select teacherlis0_.student_id as student_1_3_0_, teacherlis0_.teacher_id as teacher_2_3_0_, teacher1_.teacher_id as teacher_1_4_1_, teacher1_.teacher_age as teacher_2_4_1_, teacher1_.teacher_course as teacher_3_4_1_, teacher1_.teacher_name as teacher_4_4_1_, teacher1_.teacher_sex as teacher_5_4_1_ from student_teacher teacherlis0_ inner join teacher teacher1_ on teacherlis0_.teacher_id=teacher1_.teacher_id where teacherlis0_.student_id=?
Hibernate: select teacherlis0_.student_id as student_1_3_0_, teacherlis0_.teacher_id as teacher_2_3_0_, teacher1_.teacher_id as teacher_1_4_1_, teacher1_.teacher_age as teacher_2_4_1_, teacher1_.teacher_course as teacher_3_4_1_, teacher1_.teacher_name as teacher_4_4_1_, teacher1_.teacher_sex as teacher_5_4_1_ from student_teacher teacherlis0_ inner join teacher teacher1_ on teacherlis0_.teacher_id=teacher1_.teacher_id where teacherlis0_.student_id=?
Hibernate: select studentlis0_.teacher_id as teacher_2_3_0_, studentlis0_.student_id as student_1_3_0_, student1_.student_id as student_1_2_1_, student1_.class_room_id as class_ro5_2_1_, student1_.patriarch_id as patriarc6_2_1_, student1_.student_age as student_2_2_1_, student1_.student_name as student_3_2_1_, student1_.student_sex as student_4_2_1_, classroom2_.class_room_id as class_ro1_0_2_, classroom2_.class_room_capacity as class_ro2_0_2_, classroom2_.class_room_grade as class_ro3_0_2_, classroom2_.class_room_location as class_ro4_0_2_, classroom2_.class_room_name as class_ro5_0_2_, patriarch3_.patriarch_id as patriarc1_1_3_, patriarch3_.patriarch_address as patriarc2_1_3_, patriarch3_.patriarch_age as patriarc3_1_3_, patriarch3_.patriarch_name as patriarc4_1_3_, patriarch3_.patriarch_phone as patriarc5_1_3_, patriarch3_.patriarch_sex as patriarc6_1_3_ from student_teacher studentlis0_ inner join student student1_ on studentlis0_.student_id=student1_.student_id inner join classroom classroom2_ on student1_.class_room_id=classroom2_.class_room_id inner join patriarch patriarch3_ on student1_.patriarch_id=patriarch3_.patriarch_id where studentlis0_.teacher_id=?

Hibernate: select teacher0_.teacher_id as teacher_1_4_, teacher0_.teacher_age as teacher_2_4_, teacher0_.teacher_course as teacher_3_4_, teacher0_.teacher_name as teacher_4_4_, teacher0_.teacher_sex as teacher_5_4_ from teacher teacher0_
Hibernate: select studentlis0_.teacher_id as teacher_2_3_0_, studentlis0_.student_id as student_1_3_0_, student1_.student_id as student_1_2_1_, student1_.class_room_id as class_ro5_2_1_, student1_.patriarch_id as patriarc6_2_1_, student1_.student_age as student_2_2_1_, student1_.student_name as student_3_2_1_, student1_.student_sex as student_4_2_1_, classroom2_.class_room_id as class_ro1_0_2_, classroom2_.class_room_capacity as class_ro2_0_2_, classroom2_.class_room_grade as class_ro3_0_2_, classroom2_.class_room_location as class_ro4_0_2_, classroom2_.class_room_name as class_ro5_0_2_, patriarch3_.patriarch_id as patriarc1_1_3_, patriarch3_.patriarch_address as patriarc2_1_3_, patriarch3_.patriarch_age as patriarc3_1_3_, patriarch3_.patriarch_name as patriarc4_1_3_, patriarch3_.patriarch_phone as patriarc5_1_3_, patriarch3_.patriarch_sex as patriarc6_1_3_ from student_teacher studentlis0_ inner join student student1_ on studentlis0_.student_id=student1_.student_id inner join classroom classroom2_ on student1_.class_room_id=classroom2_.class_room_id inner join patriarch patriarch3_ on student1_.patriarch_id=patriarch3_.patriarch_id where studentlis0_.teacher_id=?
Hibernate: select studentlis0_.teacher_id as teacher_2_3_0_, studentlis0_.student_id as student_1_3_0_, student1_.student_id as student_1_2_1_, student1_.class_room_id as class_ro5_2_1_, student1_.patriarch_id as patriarc6_2_1_, student1_.student_age as student_2_2_1_, student1_.student_name as student_3_2_1_, student1_.student_sex as student_4_2_1_, classroom2_.class_room_id as class_ro1_0_2_, classroom2_.class_room_capacity as class_ro2_0_2_, classroom2_.class_room_grade as class_ro3_0_2_, classroom2_.class_room_location as class_ro4_0_2_, classroom2_.class_room_name as class_ro5_0_2_, patriarch3_.patriarch_id as patriarc1_1_3_, patriarch3_.patriarch_address as patriarc2_1_3_, patriarch3_.patriarch_age as patriarc3_1_3_, patriarch3_.patriarch_name as patriarc4_1_3_, patriarch3_.patriarch_phone as patriarc5_1_3_, patriarch3_.patriarch_sex as patriarc6_1_3_ from student_teacher studentlis0_ inner join student student1_ on studentlis0_.student_id=student1_.student_id inner join classroom classroom2_ on student1_.class_room_id=classroom2_.class_room_id inner join patriarch patriarch3_ on student1_.patriarch_id=patriarch3_.patriarch_id where studentlis0_.teacher_id=?
Hibernate: select studentlis0_.teacher_id as teacher_2_3_0_, studentlis0_.student_id as student_1_3_0_, student1_.student_id as student_1_2_1_, student1_.class_room_id as class_ro5_2_1_, student1_.patriarch_id as patriarc6_2_1_, student1_.student_age as student_2_2_1_, student1_.student_name as student_3_2_1_, student1_.student_sex as student_4_2_1_, classroom2_.class_room_id as class_ro1_0_2_, classroom2_.class_room_capacity as class_ro2_0_2_, classroom2_.class_room_grade as class_ro3_0_2_, classroom2_.class_room_location as class_ro4_0_2_, classroom2_.class_room_name as class_ro5_0_2_, patriarch3_.patriarch_id as patriarc1_1_3_, patriarch3_.patriarch_address as patriarc2_1_3_, patriarch3_.patriarch_age as patriarc3_1_3_, patriarch3_.patriarch_name as patriarc4_1_3_, patriarch3_.patriarch_phone as patriarc5_1_3_, patriarch3_.patriarch_sex as patriarc6_1_3_ from student_teacher studentlis0_ inner join student student1_ on studentlis0_.student_id=student1_.student_id inner join classroom classroom2_ on student1_.class_room_id=classroom2_.class_room_id inner join patriarch patriarch3_ on student1_.patriarch_id=patriarch3_.patriarch_id where studentlis0_.teacher_id=?
Hibernate: select studentlis0_.teacher_id as teacher_2_3_0_, studentlis0_.student_id as student_1_3_0_, student1_.student_id as student_1_2_1_, student1_.class_room_id as class_ro5_2_1_, student1_.patriarch_id as patriarc6_2_1_, student1_.student_age as student_2_2_1_, student1_.student_name as student_3_2_1_, student1_.student_sex as student_4_2_1_, classroom2_.class_room_id as class_ro1_0_2_, classroom2_.class_room_capacity as class_ro2_0_2_, classroom2_.class_room_grade as class_ro3_0_2_, classroom2_.class_room_location as class_ro4_0_2_, classroom2_.class_room_name as class_ro5_0_2_, patriarch3_.patriarch_id as patriarc1_1_3_, patriarch3_.patriarch_address as patriarc2_1_3_, patriarch3_.patriarch_age as patriarc3_1_3_, patriarch3_.patriarch_name as patriarc4_1_3_, patriarch3_.patriarch_phone as patriarc5_1_3_, patriarch3_.patriarch_sex as patriarc6_1_3_ from student_teacher studentlis0_ inner join student student1_ on studentlis0_.student_id=student1_.student_id inner join classroom classroom2_ on student1_.class_room_id=classroom2_.class_room_id inner join patriarch patriarch3_ on student1_.patriarch_id=patriarch3_.patriarch_id where studentlis0_.teacher_id=?
Hibernate: select studentlis0_.teacher_id as teacher_2_3_0_, studentlis0_.student_id as student_1_3_0_, student1_.student_id as student_1_2_1_, student1_.class_room_id as class_ro5_2_1_, student1_.patriarch_id as patriarc6_2_1_, student1_.student_age as student_2_2_1_, student1_.student_name as student_3_2_1_, student1_.student_sex as student_4_2_1_, classroom2_.class_room_id as class_ro1_0_2_, classroom2_.class_room_capacity as class_ro2_0_2_, classroom2_.class_room_grade as class_ro3_0_2_, classroom2_.class_room_location as class_ro4_0_2_, classroom2_.class_room_name as class_ro5_0_2_, patriarch3_.patriarch_id as patriarc1_1_3_, patriarch3_.patriarch_address as patriarc2_1_3_, patriarch3_.patriarch_age as patriarc3_1_3_, patriarch3_.patriarch_name as patriarc4_1_3_, patriarch3_.patriarch_phone as patriarc5_1_3_, patriarch3_.patriarch_sex as patriarc6_1_3_ from student_teacher studentlis0_ inner join student student1_ on studentlis0_.student_id=student1_.student_id inner join classroom classroom2_ on student1_.class_room_id=classroom2_.class_room_id inner join patriarch patriarch3_ on student1_.patriarch_id=patriarch3_.patriarch_id where studentlis0_.teacher_id=?
Hibernate: select studentlis0_.teacher_id as teacher_2_3_0_, studentlis0_.student_id as student_1_3_0_, student1_.student_id as student_1_2_1_, student1_.class_room_id as class_ro5_2_1_, student1_.patriarch_id as patriarc6_2_1_, student1_.student_age as student_2_2_1_, student1_.student_name as student_3_2_1_, student1_.student_sex as student_4_2_1_, classroom2_.class_room_id as class_ro1_0_2_, classroom2_.class_room_capacity as class_ro2_0_2_, classroom2_.class_room_grade as class_ro3_0_2_, classroom2_.class_room_location as class_ro4_0_2_, classroom2_.class_room_name as class_ro5_0_2_, patriarch3_.patriarch_id as patriarc1_1_3_, patriarch3_.patriarch_address as patriarc2_1_3_, patriarch3_.patriarch_age as patriarc3_1_3_, patriarch3_.patriarch_name as patriarc4_1_3_, patriarch3_.patriarch_phone as patriarc5_1_3_, patriarch3_.patriarch_sex as patriarc6_1_3_ from student_teacher studentlis0_ inner join student student1_ on studentlis0_.student_id=student1_.student_id inner join classroom classroom2_ on student1_.class_room_id=classroom2_.class_room_id inner join patriarch patriarch3_ on student1_.patriarch_id=patriarch3_.patriarch_id where studentlis0_.teacher_id=?
Hibernate: select studentlis0_.teacher_id as teacher_2_3_0_, studentlis0_.student_id as student_1_3_0_, student1_.student_id as student_1_2_1_, student1_.class_room_id as class_ro5_2_1_, student1_.patriarch_id as patriarc6_2_1_, student1_.student_age as student_2_2_1_, student1_.student_name as student_3_2_1_, student1_.student_sex as student_4_2_1_, classroom2_.class_room_id as class_ro1_0_2_, classroom2_.class_room_capacity as class_ro2_0_2_, classroom2_.class_room_grade as class_ro3_0_2_, classroom2_.class_room_location as class_ro4_0_2_, classroom2_.class_room_name as class_ro5_0_2_, patriarch3_.patriarch_id as patriarc1_1_3_, patriarch3_.patriarch_address as patriarc2_1_3_, patriarch3_.patriarch_age as patriarc3_1_3_, patriarch3_.patriarch_name as patriarc4_1_3_, patriarch3_.patriarch_phone as patriarc5_1_3_, patriarch3_.patriarch_sex as patriarc6_1_3_ from student_teacher studentlis0_ inner join student student1_ on studentlis0_.student_id=student1_.student_id inner join classroom classroom2_ on student1_.class_room_id=classroom2_.class_room_id inner join patriarch patriarch3_ on student1_.patriarch_id=patriarch3_.patriarch_id where studentlis0_.teacher_id=?
Hibernate: select studentlis0_.class_room_id as class_ro5_2_0_, studentlis0_.student_id as student_1_2_0_, studentlis0_.student_id as student_1_2_1_, studentlis0_.class_room_id as class_ro5_2_1_, studentlis0_.patriarch_id as patriarc6_2_1_, studentlis0_.student_age as student_2_2_1_, studentlis0_.student_name as student_3_2_1_, studentlis0_.student_sex as student_4_2_1_, patriarch1_.patriarch_id as patriarc1_1_2_, patriarch1_.patriarch_address as patriarc2_1_2_, patriarch1_.patriarch_age as patriarc3_1_2_, patriarch1_.patriarch_name as patriarc4_1_2_, patriarch1_.patriarch_phone as patriarc5_1_2_, patriarch1_.patriarch_sex as patriarc6_1_2_ from student studentlis0_ inner join patriarch patriarch1_ on studentlis0_.patriarch_id=patriarch1_.patriarch_id where studentlis0_.class_room_id=?
Hibernate: select teacherlis0_.student_id as student_1_3_0_, teacherlis0_.teacher_id as teacher_2_3_0_, teacher1_.teacher_id as teacher_1_4_1_, teacher1_.teacher_age as teacher_2_4_1_, teacher1_.teacher_course as teacher_3_4_1_, teacher1_.teacher_name as teacher_4_4_1_, teacher1_.teacher_sex as teacher_5_4_1_ from student_teacher teacherlis0_ inner join teacher teacher1_ on teacherlis0_.teacher_id=teacher1_.teacher_id where teacherlis0_.student_id=?
Hibernate: select teacherlis0_.student_id as student_1_3_0_, teacherlis0_.teacher_id as teacher_2_3_0_, teacher1_.teacher_id as teacher_1_4_1_, teacher1_.teacher_age as teacher_2_4_1_, teacher1_.teacher_course as teacher_3_4_1_, teacher1_.teacher_name as teacher_4_4_1_, teacher1_.teacher_sex as teacher_5_4_1_ from student_teacher teacherlis0_ inner join teacher teacher1_ on teacherlis0_.teacher_id=teacher1_.teacher_id where teacherlis0_.student_id=?
Hibernate: select studentlis0_.teacher_id as teacher_2_3_0_, studentlis0_.student_id as student_1_3_0_, student1_.student_id as student_1_2_1_, student1_.class_room_id as class_ro5_2_1_, student1_.patriarch_id as patriarc6_2_1_, student1_.student_age as student_2_2_1_, student1_.student_name as student_3_2_1_, student1_.student_sex as student_4_2_1_, classroom2_.class_room_id as class_ro1_0_2_, classroom2_.class_room_capacity as class_ro2_0_2_, classroom2_.class_room_grade as class_ro3_0_2_, classroom2_.class_room_location as class_ro4_0_2_, classroom2_.class_room_name as class_ro5_0_2_, patriarch3_.patriarch_id as patriarc1_1_3_, patriarch3_.patriarch_address as patriarc2_1_3_, patriarch3_.patriarch_age as patriarc3_1_3_, patriarch3_.patriarch_name as patriarc4_1_3_, patriarch3_.patriarch_phone as patriarc5_1_3_, patriarch3_.patriarch_sex as patriarc6_1_3_ from student_teacher studentlis0_ inner join student student1_ on studentlis0_.student_id=student1_.student_id inner join classroom classroom2_ on student1_.class_room_id=classroom2_.class_room_id inner join patriarch patriarch3_ on student1_.patriarch_id=patriarch3_.patriarch_id where studentlis0_.teacher_id=?
第四步、测试 2 controller 测试

这个测试主要是用来 数据操作是否成功,数据能不能成功 发出/接受。

BaseController.java 我自定义的 控制器类基类 接口,删了就行(记得把 @Override 删了)。

其他的 controller 基本一样,省略了,如果不构建前端页面就只能测试 Get 方法。

其中,ResultVO 是我自定义的 数据返回集,将 ResultVO 换成 String,ResultVO.XX(“xxx”) 换成 “xxx” 即可。

我在 Gitee 上 有前端代码,可以直接 下载,在 CSDN 上也有博文,欢迎 大佬 指点。

gitee:《vue 的简单构建》

博文:《Vue的使用 – 基于Vue搭建前端页面》

package com.example.demo.controller;

import com.example.demo.base.BaseController;
import com.example.demo.entity.Teacher;
import com.example.demo.serve.TeacherService;
import com.example.demo.utils.ResultVO;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.annotation.Resource;

/**
 * 教师 接口
 *
 * @author LJM
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController implements BaseController<Teacher, Long> {
	
	@Resource
	private TeacherService teacherService;
	
	@Override
	@GetMapping("/select")
	public ResultVO selectAll() {
		List<Teacher> teachers = teacherService.selectAllBatch();
		return teachers.isEmpty() ? ResultVO.error("表数据为空") : ResultVO.success(teachers);
	}
	
	@Override
	@GetMapping("/select/{id}")
	public ResultVO selectById(@PathVariable Long id) {
		Teacher teacher = teacherService.selectById(id);
		return teacher == null ? ResultVO.error("id 不正确") : ResultVO.success(teacher);
	}
	
	@Override
	@DeleteMapping("/delete/{id}")
	public ResultVO deleteById(@PathVariable Long id) {
		int i = teacherService.deleteById(id);
		return i == 0 ? ResultVO.error("id 不正确") : ResultVO.ok();
	}
	
	@Override
	@PostMapping("/save")
	public ResultVO insert(@RequestBody Teacher teacher) {
		long i = teacherService.save(teacher);
		return i == 0L ? ResultVO.error("添加失败") : ResultVO.success(i);
	}
	
	@Override
	@PostMapping("/update")
	public ResultVO update(@RequestBody Teacher teacher) {
		if (teacher.getTeacherId() == null) {
			return ResultVO.error();
		}
		int i = teacherService.update(teacher);
		return i == 0L ? ResultVO.error("更改失败") : ResultVO.success(i);
	}
	
	@Override
	public ResultVO count() {
		return ResultVO.success(teacherService.countAll());
	}
}

可以看一下我自定义的 ResultVO 类

package com.example.demo.utils;

import com.example.demo.utils.enumUtils.GlobalStatusEnum;
import io.swagger.annotations.Api;
import lombok.Data;

/**
 * 返回结果集封装类
 *
 * @author LJM
 */
@Data
@Api(tags = "返回结果集封装类")
public class ResultVO {

	/**
	 * 状态码
	 */
	private int code;

	/**
	 * 信息
	 */
	private String msg;

	/**
	 * 数据
	 */
	private Object data;
	
	
	/**
	 * 返回成功的结果(200)
	 *
	 * @param code 状态码
	 * @param msg  信息
	 * @param data 数据
	 * @return Result
	 */
	private static ResultVO success(int code, String msg, Object data) {
		ResultVO result = new ResultVO();
		result.setCode(code);
		result.setMsg(msg);
		result.setData(data);
		return result;
	}
	/**
	 * 成功(200)
	 *
	 * @param data 数据
	 * @return Result
	 */
	public static ResultVO success(Object data) {
		return success(GlobalStatusEnum.OK.getErrCode(), GlobalStatusEnum.OK.getErrMsg(), data);
	}
	/**
	 * 成功(200)
	 *
	 * @return Result
	 */
	public static ResultVO ok() {
		return success("成功");
	}
	
	
	/**
	 * 返回插入成功的结果(201)
	 *
	 * @param code 状态码
	 * @param msg  信息
	 * @param data 数据
	 * @return Result
	 */
	private static ResultVO create(int code, String msg, Object data) {
		ResultVO result = new ResultVO();
		result.setCode(code);
		result.setMsg(msg);
		result.setData(data);
		return result;
	}
	/**
	 * 插入成功(201)
	 *
	 * @param data 数据
	 * @return Result
	 */
	public static ResultVO create(Object data) {
		return create(GlobalStatusEnum.CREATED.getErrCode(),
					  GlobalStatusEnum.CREATED.getErrMsg(), data);
	}
	
	
	/**
	 * 返回失败的结果(400)
	 *
	 * @param code 状态码
	 * @param msg  信息
	 * @param data 数据
	 * @return Result
	 */
	private static ResultVO error(int code, String msg, Object data) {
		ResultVO result = new ResultVO();
		result.setCode(code);
		result.setMsg(msg);
		result.setData(data);
		return result;
	}
	/**
	 * 失败(400)
	 *
	 * @param data 数据
	 * @return Result
	 */
	public static ResultVO error(Object data) {
		return error(GlobalStatusEnum.BAD_REQUEST.getErrCode(),
					 GlobalStatusEnum.BAD_REQUEST.getErrMsg(), data);
	}
	/**
	 * 失败(400)
	 *
	 * @return Result
	 */
	public static ResultVO error() {
		return error("错误的请求");
	}
	
	
	/**
	 * 返回未登录的结果(401)
	 *
	 * @param code 状态码
	 * @param msg  信息
	 * @param data 数据
	 * @return Result
	 */
	private static ResultVO notLogIn(int code, String msg, Object data) {
		ResultVO result = new ResultVO();
		result.setCode(code);
		result.setMsg(msg);
		result.setData(data);
		return result;
	}
	/**
	 * 未登录(401)
	 *
	 * @param data 数据
	 * @return Result
	 */
	public static ResultVO notLogIn(Object data) {
		return notLogIn(GlobalStatusEnum.UNAUTHORIZED.getErrCode(),
						GlobalStatusEnum.UNAUTHORIZED.getErrMsg(), data);
	}
	/**
	 * 未登录(401)
	 *
	 * @return Result
	 */
	public static ResultVO notLogIn() {
		return notLogIn("登录信息已过期");
	}
	
	
	/**
	 * 返回禁止访问的结果(403)
	 *
	 * @param code 状态码
	 * @param msg  信息
	 * @param data 数据
	 * @return Result
	 */
	private static ResultVO forbidden(int code, String msg, Object data) {
		ResultVO result = new ResultVO();
		result.setCode(code);
		result.setMsg(msg);
		result.setData(data);
		return result;
	}
	/**
	 * 禁止访问(403)
	 *
	 * @param data 数据
	 * @return Result
	 */
	public static ResultVO forbidden(Object data) {
		return forbidden(GlobalStatusEnum.FORBIDDEN.getErrCode(),
						 GlobalStatusEnum.FORBIDDEN.getErrMsg(), data);
	}
	/**
	 * 禁止访问(403)
	 *
	 * @return Result
	 */
	public static ResultVO forbidden() {
		return forbidden("禁止访问");
	}
	
	
	/**
	 * 返回错误的URL的结果(404)
	 *
	 * @param code 状态码
	 * @param msg  信息
	 * @param data 数据
	 * @return Result
	 */
	private static ResultVO notFound(int code, String msg, Object data) {
		ResultVO result = new ResultVO();
		result.setCode(code);
		result.setMsg(msg);
		result.setData(data);
		return result;
	}
	/**
	 * 错误的URL(404)
	 *
	 * @param data 数据
	 * @return Result
	 */
	public static ResultVO notFound(Object data) {
		return notFound(GlobalStatusEnum.NOT_FOUND.getErrCode(),
						GlobalStatusEnum.NOT_FOUND.getErrMsg(), data);
	}
	/**
	 * 错误的URL(404)
	 *
	 * @return Result
	 */
	public static ResultVO notFound() {
		return notFound("URL信息错误");
	}
	
	
	/**
	 * 返回服务器内部错误的结果(500)
	 *
	 * @param code 状态码
	 * @param msg  信息
	 * @param data 数据
	 * @return Result
	 */
	private static ResultVO serverError(int code, String msg, Object data) {
		ResultVO result = new ResultVO();
		result.setCode(code);
		result.setMsg(msg);
		result.setData(data);
		return result;
	}
	/**
	 * 服务器内部错误(500)
	 *
	 * @param data 数据
	 * @return Result
	 */
	public static ResultVO serverError(Object data) {
		return serverError(GlobalStatusEnum.INTERNAL_SERVER_ERROR.getErrCode(),
						   GlobalStatusEnum.INTERNAL_SERVER_ERROR.getErrMsg(), data);
	}
	/**
	 * 服务器内部错误(500)
	 *
	 * @return Result
	 */
	public static ResultVO serverError() {
		return serverError("服务器内部错误");
	}
	
	@Override
	public String toString() {
		return "ResultVO{" +
				"code=" + code +
				", msg='" + msg + '\'' +
				", data=" + data +
				'}';
	}
}
package com.example.demo.utils.enumUtils;

/**
 * 访问 状态码
 *
 * @author LJM
 */
public enum GlobalStatusEnum {
	
	
	/**
	 * 成功
	 */
	OK(200, "请求成功"),
	CREATED(201, "请求已经被成功处理,并且创建了新的资源"),
	NO_CONTENT(204, "请求成功,但响应报文不含实体的主体部分"),
	
	/**
	 * 重定向
	 */
	MOVED_PERMANENTLY(301, "资源已被分配了新的 URL"),
	FOUND(302, "资源临时被分配了新的 URL"),
	SEE_OTHER(303, "资源存在着另一个 URL,应使用 GET 方法获取资源"),
	NOT_MODIFIED(304, "服务器允许访问资源,但请求未满足条件"),
	
	/**
	 * 失败
	 */
	BAD_REQUEST(400, "请求的地址不存在或者包含不支持的参数"),
	UNAUTHORIZED(401, "未授权,请登录"),
	FORBIDDEN(403, "禁止访问"),
	NOT_FOUND(404, "请求资源不存在"),
	UNPROCESSABLE_ENTITY(422, "[POST/PUT/PATCH] 当创建一个对象时,发生一个验证错误"),
	
	/**
	 * 错误
	 */
	INTERNAL_SERVER_ERROR(500, "内部错误"),
	NOT_IMPLEMENTED(501, "服务器不支持当前请求所需要的某个功能"),
	SERVICE_UNAVAILABLE(503, "服务器暂时处于超负载或正在停机维护,无法处理请求"),
	GATEWAY_TIMEOUT(504, "服务器连接超时");

	private final int code;

	private final String msg;


	GlobalStatusEnum(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public String getErrMsg() {
		return this.msg;
	}

	public int getErrCode() {
		return this.code;
	}

}

``如果对你有帮助,点赞可好!!```

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十⑧

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值