基本思路:EasyCode 根据students表生成第一版代码,然后补充基本逻辑
[Ref] SpringBoot 初次配置数据库 并 进行第一次业务查询
[Ref] IDEA+EasyCode实现代码生成(傻瓜式教程)
Model
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String username;
private String password;
private Integer age;
private Integer height;
private String gender;
private Integer classId;
private Boolean isDelete;
}
Repository
package com.zhangziwa.practisesvr.mapper;
import com.zhangziwa.practisesvr.model.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* (Students)表数据库访问层
*
* @author makejava
* @since 2024-01-16 12:22:58
*/
public interface StudentsMapper {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
Student queryById(Integer id);
/**
* 查询指定行数据
*
* @param student 查询条件
* @return 对象列表
*/
List<Student> queryAllByLimit(Student student);
/**
* 统计总行数
*
* @param student 查询条件
* @return 总行数
*/
long count(Student student);
/**
* 新增数据
*
* @param student 实例对象
* @return 影响行数
*/
int insert(Student student);
/**
* 批量新增数据(MyBatis原生foreach方法)
*
* @param entities List<Students> 实例对象列表
* @return 影响行数
*/
int insertBatch(@Param("entities") List<Student> entities);
/**
* 批量新增或按主键更新数据(MyBatis原生foreach方法)
*
* @param entities List<Students> 实例对象列表
* @return 影响行数
* @throws org.springframework.jdbc.BadSqlGrammarException 入参是空List的时候会抛SQL语句错误的异常,请自行校验入参
*/
int insertOrUpdateBatch(@Param("entities") List<Student> entities);
/**
* 修改数据
*
* @param student 实例对象
* @return 影响行数
*/
int update(Student student);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 影响行数
*/
int deleteById(Integer id);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zhangziwa.practisesvr.mapper.StudentsMapper">
<resultMap type="com.zhangziwa.practisesvr.model.Student" id="StudentsMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="username" column="username" jdbcType="VARCHAR"/>
<result property="password" column="password" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<result property="height" column="height" jdbcType="NUMERIC"/>
<result property="gender" column="gender" jdbcType="VARCHAR"/>
<result property="classId" column="class_id" jdbcType="INTEGER"/>
<result property="isDelete" column="is_delete" jdbcType="VARCHAR"/>
</resultMap>
<!--查询单个-->
<select id="queryById" resultMap="StudentsMap">
select id,
username,
password,
age,
height,
gender,
class_id,
is_delete
from students
where id = #{id}
</select>
<!--查询指定行数据-->
<select id="queryAllByLimit" resultMap="StudentsMap">
select
id, username, password, age, height, gender, class_id, is_delete
from students
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="password != null and password != ''">
and password = #{password}
</if>
<if test="age != null">
and age = #{age}
</if>
<if test="height != null">
and height = #{height}
</if>
<if test="gender != null and gender != ''">
and gender = #{gender}
</if>
<if test="classId != null">
and class_id = #{classId}
</if>
<if test="isDelete != null">
and is_delete = #{isDelete}
</if>
</where>
</select>
<!--统计总行数-->
<select id="count" resultType="java.lang.Long">
select count(1)
from students
<where>
<if test="id != null">
and id = #{id}
</if>
<if test="username != null and username != ''">
and username = #{username}
</if>
<if test="password != null and password != ''">
and password = #{password}
</if>
<if test="age != null">
and age = #{age}
</if>
<if test="height != null">
and height = #{height}
</if>
<if test="gender != null and gender != ''">
and gender = #{gender}
</if>
<if test="classId != null">
and class_id = #{classId}
</if>
<if test="isDelete != null">
and is_delete = #{isDelete}
</if>
</where>
</select>
<!--新增所有列-->
<insert id="insert" keyProperty="id" useGeneratedKeys="true">
insert into students(username, password, age, height, gender, class_id, is_delete)
values (#{username}, #{password}, #{age}, #{height}, #{gender}, #{classId}, #{isDelete})
</insert>
<insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
insert into students(username, password, age, height, gender, class_id, is_delete)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.username}, #{entity.password}, #{entity.age}, #{entity.height}, #{entity.gender},
#{entity.classId}, #{entity.isDelete})
</foreach>
</insert>
<insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
insert into students(username, password, age, height, gender, class_id, is_delete)
values
<foreach collection="entities" item="entity" separator=",">
(#{entity.username}, #{entity.password}, #{entity.age}, #{entity.height}, #{entity.gender},
#{entity.classId}, #{entity.isDelete})
</foreach>
on duplicate key update
username = values(username),
password = values(password),
age = values(age),
height = values(height),
gender = values(gender),
class_id = values(class_id),
is_delete = values(is_delete)
</insert>
<!--通过主键修改数据-->
<update id="update">
update students
<set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="password != null and password != ''">
password = #{password},
</if>
<if test="age != null">
age = #{age},
</if>
<if test="height != null">
height = #{height},
</if>
<if test="gender != null and gender != ''">
gender = #{gender},
</if>
<if test="classId != null">
class_id = #{classId},
</if>
<if test="isDelete != null">
is_delete = #{isDelete},
</if>
</set>
where id = #{id}
</update>
<!--通过主键删除-->
<delete id="deleteById">
delete
from students
where id = #{id}
</delete>
</mapper>
Service
package com.zhangziwa.practisesvr.service;
import com.zhangziwa.practisesvr.model.Student;
import java.util.List;
/**
* (Students)表服务接口
*
* @author makejava
* @since 2024-01-16 12:23:01
*/
public interface StudentService {
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
Student queryById(Integer id);
/**
* 分页查询
*
* @param student 筛选条件
* @return 查询结果
*/
List<Student> queryByPage(Student student, Integer pageNum, Integer pageSize);
Long queryCount(Student student);
/**
* 新增数据
*
* @param student 实例对象
* @return 实例对象
*/
Student insert(Student student);
/**
* 修改数据
*
* @param student 实例对象
* @return 实例对象
*/
Student update(Student student);
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
boolean deleteById(Integer id);
}
package com.zhangziwa.practisesvr.service.serviceImpl;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;
import com.zhangziwa.practisesvr.mapper.StudentsMapper;
import com.zhangziwa.practisesvr.model.Student;
import com.zhangziwa.practisesvr.service.StudentService;
import com.zhangziwa.practisesvr.utils.pagehelper.PageHeaderUtils;
import com.zhangziwa.practisesvr.utils.pagehelper.PageUtils;
import com.zhangziwa.practisesvr.utils.response.ResponseContext;
import jakarta.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* (Students)表服务实现类
*
* @author makejava
* @since 2024-01-16 12:23:02
*/
@Service("studentsService")
public class StudentServiceImpl implements StudentService {
@Resource
private StudentsMapper studentsMapper;
/**
* 通过ID查询单条数据
*
* @param id 主键
* @return 实例对象
*/
@Override
public Student queryById(Integer id) {
return this.studentsMapper.queryById(id);
}
/**
* 分页查询
*
* @param student 筛选条件
* @return 查询结果
*/
@Override
public List<Student> queryByPage(Student student,Integer pageNum,Integer pageSize) {
PageMethod.startPage(PageUtils.getPageNum(pageNum), PageUtils.getPageSize(pageSize), PageUtils.isQueryTotalCount());
PageMethod.orderBy("id asc");
List<Student> students = studentsMapper.queryAllByLimit(student);
PageInfo<Student> studentPageInfo = PageInfo.of(students);
PageHeaderUtils.setPageHeader(studentPageInfo);
ResponseContext.setResponseCode(HttpStatus.OK);
return students;
}
@Override
public Long queryCount(Student student) {
return this.studentsMapper.count(student);
}
/**
* 新增数据
*
* @param student 实例对象
* @return 实例对象
*/
@Override
public Student insert(Student student) {
this.studentsMapper.insert(student);
return student;
}
/**
* 修改数据
*
* @param student 实例对象
* @return 实例对象
*/
@Override
public Student update(Student student) {
this.studentsMapper.update(student);
return this.queryById(student.getId());
}
/**
* 通过主键删除数据
*
* @param id 主键
* @return 是否成功
*/
@Override
public boolean deleteById(Integer id) {
return this.studentsMapper.deleteById(id) > 0;
}
}
Controller
package com.zhangziwa.practisesvr.controller;
import com.zhangziwa.practisesvr.model.Student;
import com.zhangziwa.practisesvr.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* (Students)表控制层
*
* @author makejava
* @since 2024-01-16 12:22:55
*/
@RestController
@RequestMapping("students")
public class StudentController {
/**
* 服务对象
*/
@Resource
private StudentService studentService;
/**
* 分页查询
*
* @param student 筛选条件
* @return 查询结果
*/
@GetMapping
public ResponseEntity<List<Student>> queryByPage(Student student,
@RequestParam(value = "pageNum") Integer pageNum,
@RequestParam(value = "PageSize") Integer pageSize) {
return ResponseEntity.ok(studentService.queryByPage(student, pageNum, pageSize));
}
@GetMapping("/count")
public ResponseEntity<Long> queryCount(Student student) {
return ResponseEntity.ok(studentService.queryCount(student));
}
/**
* 通过主键查询单条数据
*
* @param id 主键
* @return 单条数据
*/
@GetMapping("{id}")
public ResponseEntity<Student> queryById(@PathVariable("id") Integer id) {
return ResponseEntity.ok(studentService.queryById(id));
}
/**
* 新增数据
*
* @param student 实体
* @return 新增结果
*/
@PostMapping
public ResponseEntity<Student> add(@RequestBody Student student) {
return ResponseEntity.ok(studentService.insert(student));
}
/**
* 编辑数据
*
* @param student 实体
* @return 编辑结果
*/
@PutMapping
public ResponseEntity<Student> edit(@RequestBody Student student) {
return ResponseEntity.ok(studentService.update(student));
}
/**
* 删除数据
*
* @param id 主键
* @return 删除是否成功
*/
@DeleteMapping("{id}")
public ResponseEntity<String> deleteById(@PathVariable("id") Integer id) {
boolean body = studentService.deleteById(id);
if (body) {
return ResponseEntity.noContent().build();
} else {
return ResponseEntity.notFound().build();
}
}
}
增
查
改
删
分页查询