分类实体类`
只有id和类别名称
package com.wzh.springbootproject.entity;
import java.io.Serializable;
/**
* 博客分类
*/
public class Category {
/** ID */
private Integer id;
/** 分类名称 */
private String name;
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;
}
}
还是基础的增删改查,
CategoryController
package com.wzh.springbootproject.controller;
import com.wzh.springbootproject.common.Result;
import com.wzh.springbootproject.entity.Category;
import com.wzh.springbootproject.service.CategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 博客分类前端操作接口
**/
@RestController
@RequestMapping("/category")
public class CategoryController {
@Resource
private CategoryService categoryService;
/**
* 新增
*/
@PostMapping("/add")
public Result add(@RequestBody Category category) {
categoryService.add(category);
return Result.success();
}
/**
* 删除
*/
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable Integer id) {
categoryService.deleteById(id);
return Result.success();
}
/**
* 批量删除
*/
@DeleteMapping("/delete/batch")
public Result deleteBatch(@RequestBody List<Integer> ids) {
categoryService.deleteBatch(ids);
return Result.success();
}
/**
* 修改
*/
@PutMapping("/update")
public Result updateById(@RequestBody Category category) {
categoryService.updateById(category);
return Result.success();
}
/**
* 根据ID查询
*/
@GetMapping("/selectById/{id}")
public Result selectById(@PathVariable Integer id) {
Category category = categoryService.selectById(id);
return Result.success(category);
}
/**
* 查询所有
*/
@GetMapping("/selectAll")
public Result selectAll(Category category) {
List<Category> list = categoryService.selectAll(category);
return Result.success(list);
}
/**
* 分页查询
*/
@GetMapping("/selectPage")
public Result selectPage(Category category,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
PageInfo<Category> page = categoryService.selectPage(category, pageNum, pageSize);
return Result.success(page);
}
}
CategoryMapper
package com.wzh.springbootproject.mapper;
import com.wzh.springbootproject.entity.Category;
import java.util.List;
/**
* 操作category相关数据接口
*/
public interface CategoryMapper {
/**
* 新增
*/
int insert(Category category);
/**
* 删除
*/
int deleteById(Integer id);
/**
* 修改
*/
int updateById(Category category);
/**
* 根据ID查询
*/
Category selectById(Integer id);
/**
* 查询所有
*/
List<Category> selectAll(Category category);
}
在resource文件夹中实现它
CategoryMapper.xml
<?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.wzh.springbootproject.mapper.CategoryMapper">
<sql id="Base_Column_List">
id,name
</sql>
<select id="selectAll" resultType="com.wzh.springbootproject.entity.Category">
select
<include refid="Base_Column_List" />
from category
<where>
<if test="id != null"> and id = #{id}</if>
<if test="name != null"> and name like concat('%', #{name}, '%')</if>
</where>
-- order by id desc
</select>
<select id="selectById" resultType="com.wzh.springbootproject.entity.Category">
select
<include refid="Base_Column_List" />
from category
where id = #{id}
</select>
<delete id="deleteById">
delete from category
where id = #{id}
</delete>
<insert id="insert" parameterType="com.wzh.springbootproject.entity.Category" useGeneratedKeys="true" keyProperty="id">
insert into category
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="name != null">name,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="name != null">#{name},</if>
</trim>
</insert>
<update id="updateById" parameterType="com.wzh.springbootproject.entity.Category">
update category
<set>
<if test="name != null">
name = #{name},
</if>
</set>
where id = #{id}
</update>
</mapper>
但是还是有点问题如上篇博客,第一个属性会报错,没有彻底解决,和掩耳盗铃一样
CategoryService
package com.wzh.springbootproject.service;
import com.wzh.springbootproject.entity.Category;
import com.wzh.springbootproject.mapper.CategoryMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* 博客分类业务处理
**/
@Service
public class CategoryService {
@Resource
private CategoryMapper categoryMapper;
/**
* 新增
*/
public void add(Category category) {
categoryMapper.insert(category);
}
/**
* 删除
*/
public void deleteById(Integer id) {
categoryMapper.deleteById(id);
}
/**
* 批量删除
*/
public void deleteBatch(List<Integer> ids) {
for (Integer id : ids) {
categoryMapper.deleteById(id);
}
}
/**
* 修改
*/
public void updateById(Category category) {
categoryMapper.updateById(category);
}
/**
* 根据ID查询
*/
public Category selectById(Integer id) {
return categoryMapper.selectById(id);
}
/**
* 查询所有
*/
public List<Category> selectAll(Category category) {
return categoryMapper.selectAll(category);
}
/**
* 分页查询
*/
public PageInfo<Category> selectPage(Category category, Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<Category> list = categoryMapper.selectAll(category);
return PageInfo.of(list);
}
}