(三)分类相关功能设计(上)
实体/数据库定义
- 类名:Category
- 表名:blog_category
- 属性
| 名称 | 中文名 | 类型 |
|---|---|---|
| id | 主键 | 整数 |
| name | 名称 | 字符串 |
| summary | 介绍 | 字符串 |
JPA注解说明
| 名称 | 位置 | 说明 |
|---|---|---|
| @Entity | 类 | 表示被注解的类是一个可以被JPA管理的实体类 |
| @Table | 类 | 当实体类与其映射的数据库表名不同名时需要使用 |
| @Column | 属性 | 当实体的属性与其映射的数据库表的列不同名时需要使用该标注说明,Column 标注的常用属性是 name,用于设置映射数据库表的列名。此外,该标注还包含其它多个属性,如:unique 、nullable、length 等 |
| @Id | 属性 | 标注用于声明一个实体类的属性映射为数据库的主键列 |
| @GeneratedValue | 属性 | 主要就是为一个实体生成一个唯一标识的主键、提供了主键的生成策略,比如IDENTITY就是自增主键,这个也是用的最多的 |
分实体类定义完整代码
package cn.kevinlu98.pojo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* Author: Mr丶冷文
* Date: 2022/10/6 21:56
* Email: kevinlu98@qq.com
* Description:
*/
@Data
@Builder
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "blog_category")
public class Category {
// 设置主键自增
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(unique = true)
private String name;
private String summary;
}
JPA增删查改操作
在jpa中只需要定义一个接口继承下JpaRepository接口就可以拥有对该实体类对应的数据库表最基本的增删查改操作
package cn.kevinlu98.mapper;
import cn.kevinlu98.pojo.Category;
import org.springframework.data.jpa.repository.JpaRepository;
/**
* Author: Mr丶冷文
* Date: 2022/10/6 22:04
* Email: kevinlu98@qq.com
* Description:
*/
public interface CategoryMapper extends JpaRepository<Category, Integer> {
}
SpringBootTest类进行测试
package cn.kevinlu98.test;
import cn.kevinlu98.Application;
import cn.kevinlu98.mapper.CategoryMapper;
import cn.kevinlu98.pojo.Category;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Author: Mr丶冷文
* Date: 2022/10/6 22:02
* Email: kevinlu98@qq.com
* Description:
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class CategoryTest {
@Autowired
private CategoryMapper mapper;
@Test
public void newEntity() {
Category c1 = Category.builder().id(1).name("xx").summary("Qqq").build();
System.out.println(c1);
}
@Test
public void testMapper() {
System.out.println(mapper);
}
@Test
public void testUpdate() {
Category category = mapper.findById(1).orElse(null);
System.out.println(category);
category.setName("新分类");
mapper.save(category);
System.out.println(mapper.findById(1));
}
@Test
public void testAdd() {
//save 如果这个对象的id不为null,它就是一个update,反之就是一个insert
for (int i = 10; i < 20; i++) {
mapper.save(Category.builder().name("测试分类" + i).summary("我是一个描述字段" + i + "...").build());
}
}
@Test
public void testFind() {
System.out.println(mapper.findAll());
System.out.println(mapper.findById(1));
}
}
完成service层增删查改操作
- 查询所有
- 查询总数
- 查询单个对象
- 保存(新增/更新)
- 删除
package cn.kevinlu98.service;
import cn.kevinlu98.mapper.CategoryMapper;
import cn.kevinlu98.pojo.Category;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Author: Mr丶冷文
* Date: 2022/10/6 22:20
* Email: kevinlu98@qq.com
* Description:
*/
@Service
public class CategoryService {
private final CategoryMapper mapper;
public CategoryService(CategoryMapper mapper) {
this.mapper = mapper;
}
/**
* 查询所有的分类
*
* @return 分类列表
*/
public List<Category> list() {
return mapper.findAll();
}
/**
* 查询分类总数
*
* @return 分类总数
*/
public long count() {
return mapper.count();
}
/**
* id查询资源详情
*
* @param id 主键
* @return 分类详情
*/
public Category detail(Integer id) {
return mapper.findById(id).orElse(null);
}
/**
* 保存操作
*
* @param category 要保存的对象
*/
public void save(Category category) {
mapper.save(category);
}
/**
* 根据id从数据库表中删除数据
*
* @param id 主键
*/
public void delete(Integer id) {
mapper.deleteById(id);
}
}
Controller层开发
公共返回实体
| 属性 | 类型 | 说明 |
|---|---|---|
| code | 整数 | 返回的状态,200表示成功,其它表示失败 |
| message | 字符串 | 返回的消息 |
| data | 对象 | 返回的具体数据 |
package cn.kevinlu98.common;
import cn.kevinlu98.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* Author: Mr丶冷文
* Date: 2022/10/6 22:26
* Email: kevinlu98@qq.com
* Description:
*/
@Data
@AllArgsConstructor
public class Result<T> {
private Integer code;
private String message;
private T data;
private Result(ResultEnum resultEnum) {
this.code = resultEnum.getCode();
this.message = resultEnum.getMessage();
}
private Result(ResultEnum resultEnum, T data) {
this.code = resultEnum.getCode();
this.message = resultEnum.getMessage();
this.data = data;
}
}返回枚举定义
定义一个有两个属性的枚举,code与message,然后定义成功与失败的枚举常量
package cn.kevinlu98.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* Author: Mr丶冷文
* Date: 2022/10/6 22:27
* Email: kevinlu98@qq.com
* Description:
*/
@Getter
@AllArgsConstructor
public enum ResultEnum {
RESULT_SUCCESS(200, "操作成功"),
RESULT_ERROR(500, "系统错误");
private final Integer code;
private final String message;
}
定义公共返回类的静态方法
定义成功及失败的静态方法
public static <T> Result<T> success() {
return new Result<>(ResultEnum.RESULT_SUCCESS);
}
public static <T> Result<T> success(T data) {
return new Result<>(ResultEnum.RESULT_SUCCESS, data);
}
public static <T> Result<T> error() {
return new Result<>(ResultEnum.RESULT_ERROR);
}
public static <T> Result<T> error(ResultEnum resultEnum) {
return new Result<>(resultEnum);
}
public static <T> Result<T> error(int code, String message) {
return new Result<>(code, message, null);
}
完成分类列表接口的开发
接口定义:
url: /admin/category/
method: get
Params: none
Result:
{
code:200,
message:"成功",
data:[
{
id:1,
name:"分类1",
summary:"我是描述"
},
{
id:2,
name:"分类2",
summary:"我是描述"
},
]
}@GetMapping("/")
public Result<List<Category>> list() {
return Result.success(service.list());
}
下载看看
I drop a comment whenever I like a article on a site or I have something to valuable to contribute to the discussion. Usually it is a result of the fire displayed in the article I looked at. And on this article Freewind主题音乐播放器(老王重制版) - 冷文学习者. I was actually excited enough to post a comment :-) I actually do have a few questions for you if it's okay. Could it be just me or do some of these responses appear like left by brain dead people? :-P And, if you are posting at other places, I would like to keep up with everything new you have to post. Could you list the complete urls of your communal sites like your linkedin profile, Facebook page or twitter feed? Feel free to surf to my blog post :: https://bonusstats.com/en/bonus-7signs
Thanks for finally talking about >Freewind 1.5 更新 - 冷文学习者
下载看看
我要回复一下课件