# 电子书表
drop table if exists `ebook`;
create table `ebook` (
`id` bigint not null comment 'id',
`name` varchar(50) comment '名称',
`category1_id` bigint comment '分类1',
`category2_id` bigint comment '分类2',
`description` varchar(200) comment '描述',
`cover` varchar(200) comment '封面',
`doc_count` int comment '文档数',
`view_count` int comment '阅读数',
`vote_count` int comment '点赞数',
primary key (`id`)
) engine=innodb default charset=utf8mb4 comment='电子书';
insert into `ebook` (id, name, description) values (1, 'Spring Boot 入门教程', '零基础入门 Java 开发,企业级应用开发最佳首选框架');
insert into `ebook` (id, name, description) values (2, 'Vue 入门教程', '零基础入门 Vue 开发,企业级应用开发最佳首选框架');
insert into `ebook` (id, name, description) values (3, 'Python 入门教程', '零基础入门 Python 开发,企业级应用开发最佳首选框架');
insert into `ebook` (id, name, description) values (4, 'Mysql 入门教程', '零基础入门 Mysql 开发,企业级应用开发最佳首选框架');
insert into `ebook` (id, name, description) values (5, 'Oracle 入门教程', '零基础入门 Oracle 开发,企业级应用开发最佳首选框架');
(1)修改generator-config.xml对象
电子书列表对应数据库表ebook,因此持久层的对象应指定为ebook。
(2)mybatis-generator生成对象
@Service
public class EbookService {
//注入EbookMapper
@Resource//JDK自带
//@Autowired//spring自带
private EbookMapper ebookMapper;
public List<Ebook> list(){
// 查询所有数据
return ebookMapper.selectByExample(null);
}
}
@RestController
@RequestMapping("/ebook")
public class EbookController {
@Resource
private EbookService ebookService;
@RequestMapping("/list")
public List<Ebook> list(){
return ebookService.list();
}
}
GET http://localhost:8880/ebook/list
接口测试结果:
package com.dengfei.wiki.resp;
//通用返回类
public class CommonResp<T> {
/**
* 业务上的成功或失败
*/
private boolean success = true;
/**
* 返回信息
*/
private String message;
/**
* 返回泛型数据,自定义类型
*/
private T content;
public boolean getSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public T getContent() {
return content;
}
public void setContent(T content) {
this.content = content;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("ResponseDto{");
sb.append("success=").append(success);
sb.append(", message='").append(message).append('\'');
sb.append(", content=").append(content);
sb.append('}');
return sb.toString();
}
}
改造前:
@RestController
@RequestMapping("/ebook")
public class EbookController {
@Resource
private EbookService ebookService;
@RequestMapping("/list")
public List<Ebook> list(){
return ebookService.list();
}
}
改造后:
@RestController
@RequestMapping("/ebook")
public class EbookController {
@Resource
private EbookService ebookService;
@RequestMapping("/list")
public CommonResp list(){
CommonResp<List<Ebook>> resp = new CommonResp<>();
List<Ebook> list=ebookService.list();
//将list放入content当中
resp.setContent(list);
resp.setMessage("测试");
return resp;
}
}
使用请求类封装请求参数,有利于根据类中参数进行数据查询,而不需要函数中添加String name,Int id等参数(因为这些全部体现在类中):
package com.dengfei.wiki.req;
public class EbookReq {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", name=").append(name);
sb.append("]");
return sb.toString();
}
}
服务层如下:
@Service
public class EbookService {
//注入EbookMapper
@Resource//JDK自带
//@Autowired//spring自带
private EbookMapper ebookMapper;
//无条件查询,即查询全部数据
public List<Ebook> list(){
// 查询所有数据
return ebookMapper.selectByExample(null);
}
//单条件查询,一次查询只能输入name;若要增加查询条件,需要增加请求参数
public List<Ebook> list(String name){
EbookExample ebookExample=new EbookExample();
//创建where条件
EbookExample.Criteria criteria= ebookExample.createCriteria();
//查询条件为包含name的数据项
criteria.andNameLike("%"+name+"%");
// 查询name数据
return ebookMapper.selectByExample(ebookExample);
}
//不定条件查询,即请求参数是一个请求类,具体传入参数看传入类
public List<EbookResp> list(EbookReq req){
EbookExample ebookExample=new EbookExample();
//创建where条件
EbookExample.Criteria criteria= ebookExample.createCriteria();
//查询条件为包含name的数据项
criteria.andNameLike("%"+req.getName()+"%");
List<Ebook>ebookList=ebookMapper.selectByExample(ebookExample);
List<EbookResp>respList=new ArrayList<>();
//实体类复制
for(Ebook ebook:ebookList){
EbookResp ebookResp=new EbookResp();
BeanUtils.copyProperties(ebook,ebookResp);
respList.add(ebookResp);
}
return respList;
}
}
使用返回类封装返回结果:
package com.dengfei.wiki.resp;
public class EbookResp {
private Long id;
private String name;
private Long category1Id;
private Long category2Id;
private String description;
private String cover;
private Integer docCount;
private Integer viewCount;
private Integer voteCount;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getCategory1Id() {
return category1Id;
}
public void setCategory1Id(Long category1Id) {
this.category1Id = category1Id;
}
public Long getCategory2Id() {
return category2Id;
}
public void setCategory2Id(Long category2Id) {
this.category2Id = category2Id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getCover() {
return cover;
}
public void setCover(String cover) {
this.cover = cover;
}
public Integer getDocCount() {
return docCount;
}
public void setDocCount(Integer docCount) {
this.docCount = docCount;
}
public Integer getViewCount() {
return viewCount;
}
public void setViewCount(Integer viewCount) {
this.viewCount = viewCount;
}
public Integer getVoteCount() {
return voteCount;
}
public void setVoteCount(Integer voteCount) {
this.voteCount = voteCount;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(getClass().getSimpleName());
sb.append(" [");
sb.append("Hash = ").append(hashCode());
sb.append(", id=").append(id);
sb.append(", name=").append(name);
sb.append(", category1Id=").append(category1Id);
sb.append(", category2Id=").append(category2Id);
sb.append(", description=").append(description);
sb.append(", cover=").append(cover);
sb.append(", docCount=").append(docCount);
sb.append(", viewCount=").append(viewCount);
sb.append(", voteCount=").append(voteCount);
sb.append("]");
return sb.toString();
}
}
封装前(将ebookList复制到respList当中):
//实体类复制
List<EbookResp>respList=new ArrayList<>();
for(Ebook ebook:ebookList){
EbookResp ebookResp=new EbookResp();
//单个实体复制
BeanUtils.copyProperties(ebook,ebookResp);
respList.add(ebookResp);
}
封装后(整个列表对象进行复制):
List<EbookResp>respList= CopyUtil.copyList(ebookList,EbookResp.class);
或者单个对象逐一进行复制:
List<EbookResp>respList=new ArrayList<>();
for(Ebook ebook:ebookList){
//单个实体复制
EbookResp ebookResp=CopyUtil.copy(ebook,EbookResp.class);
respList.add(ebookResp);
}
封装的对象复制工具:
package com.dengfei.wiki.util;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName CopyUtil
* @Description 实体复制
* @Author dengfei
* @Date 2022/10/4 22:13
* @Version 1.0
**/
public class CopyUtil {
/**
* 单体复制
* 输入:Object source-源对象;Class clazz-目标类(不是目标对象)
*/
public static <T> T copy(Object source, Class<T> clazz) {
if (source == null) {
return null;
}
T obj = null;
try {
//new 目标对象
obj = clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
return null;
}
BeanUtils.copyProperties(source, obj);
return obj;
}
/**
* 列表复制
*/
public static <T> List<T> copyList(List source, Class<T> clazz) {
List<T> target = new ArrayList<>();
if (!CollectionUtils.isEmpty(source)){
for (Object c: source) {
T obj = copy(c, clazz);
target.add(obj);
}
}
return target;
}
}