本文档基于文档《 接口开发》,所以部分内容不再赘述。
数据库准备
- Navicate中创建数据库、表和专有账户
数据库名 | test-database |
表名 | ebook |
专有用户名及密码 | test-user/test@123 |
- IDEA中使用Database插件执行以下SQL语句初始化表
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(20) comment '封面',
`doc_count` int comment '文档数',
`view_count` int comment '阅读数',
`vote_count` int comment '点赞数',
primary key(`id`)
);
insert into `ebook` (id,name,description) values (1,'SpringBoot入门教程','零基础入门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,企业级应用开发最佳首选框架');
select * from ebook;
注意哈:一定要提交,这样才能真正更新数据库中的数据。
源码准备
- generateConfig.xml
<commentGenerator></commentGenerator>
,suppressAllComments
属性值默认是false
,即默认生成注释。本例中,将suppressAllComments
置为true
,禁止生成注释。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="Mysql" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test-database"
userId="test-user"
password="test@123">
</jdbcConnection>
<javaModelGenerator targetPackage="com.jepcc.test.model" targetProject="src\main\java">
</javaModelGenerator>
<sqlMapGenerator targetPackage="mapper" targetProject="src\main\resources">
</sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.jepcc.test.mapper" targetProject="src\main\java">
</javaClientGenerator>
<table tableName="ebook" domainObjectName="Ebook" >
</table>
</context>
</generatorConfiguration>
- 运行命令
mybatis-generator:generate -e
,生成相应代码,包括- Model实体类
Ebook
、EbookExample
- Mapper接口类
EbookMapper
- Mapper XML配置文件
EbookMapper.xml
- Model实体类
- 添加controller层(
EbookController
)和service层(EbookService
)
package com.jepcc.test.service;
import com.jepcc.test.mapper.EbookMapper;
import com.jepcc.test.model.Ebook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EbookService {
@Autowired
private EbookMapper ebookMapper;
public List<Ebook> list(){
return ebookMapper.selectByExample(null);
}
}
package com.jepcc.test.service;
import com.jepcc.test.mapper.EbookMapper;
import com.jepcc.test.model.Ebook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EbookService {
@Autowired
private EbookMapper ebookMapper;
public List<Ebook> list(){
return ebookMapper.selectByExample(null);
}
}
模糊查询
修改controller层代码、service层代码
package com.jepcc.test.controller;
import com.jepcc.test.model.Ebook;
import com.jepcc.test.resp.CommonResp;
import com.jepcc.test.service.EbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/ebook")
public class EbookController {
@Autowired
private EbookService ebookService;
@GetMapping("/list")
public CommonResp list(String name){
CommonResp resp = new CommonResp();
List<Ebook> list = ebookService.list(name);
resp.setContent(list);
return resp;
}
}
package com.jepcc.test.service;
import com.jepcc.test.mapper.EbookMapper;
import com.jepcc.test.model.Ebook;
import com.jepcc.test.model.EbookExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EbookService {
@Autowired
private EbookMapper ebookMapper;
public List<Ebook> list(String name){
EbookExample ebookExample = new EbookExample();
// EbookExample.Criteria criteria = ebookExample.createCriteria(name);
EbookExample.Criteria criteria = ebookExample.or();
criteria.andNameLike("%"+name+"%");
return ebookMapper.selectByExample(ebookExample);
}
}
假设浏览器访问http://localhost:8080/ebook/list?name=Spring
,那么以上模糊查询相当于select * from ebook where name like '%Spring%'
。
Example类
Example
类用来构建动态的where子句。
Example
类包含一个内部静态类Criteria
,可以通过以下方法创建Criteria
对象,
- 第一种方法:
createCriteria()
- 第二种方法:
or()
封装请求参数和返回参数
封装请求参数
- 新建包req,并在该包下新建类
EbookReq
package com.jepcc.test.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;
}
}
- 修改controller层
EbookController
package com.jepcc.test.controller;
import com.jepcc.test.model.Ebook;
import com.jepcc.test.req.EbookReq;
import com.jepcc.test.resp.CommonResp;
import com.jepcc.test.service.EbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/ebook")
public class EbookController {
@Autowired
private EbookService ebookService;
@GetMapping("/list")
public CommonResp list(EbookReq req){
CommonResp resp = new CommonResp();
List<Ebook> list = ebookService.list(req);
resp.setContent(list);
return resp;
}
}
- 修改service层
EbookService
package com.jepcc.test.service;
import com.jepcc.test.mapper.EbookMapper;
import com.jepcc.test.model.Ebook;
import com.jepcc.test.model.EbookExample;
import com.jepcc.test.req.EbookReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EbookService {
@Autowired
private EbookMapper ebookMapper;
public List<Ebook> list(EbookReq req){
EbookExample ebookExample = new EbookExample();
EbookExample.Criteria criteria = ebookExample.or();
criteria.andNameLike("%"+req.getName()+"%");
return ebookMapper.selectByExample(ebookExample);
}
}
- 验证下
封装返回参数
- 在resp包下新建类
EbookResp
package com.jepcc.test.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;
}
}
- 修改service层
EbookService
package com.jepcc.test.service;
import com.jepcc.test.mapper.EbookMapper;
import com.jepcc.test.model.Ebook;
import com.jepcc.test.model.EbookExample;
import com.jepcc.test.req.EbookReq;
import com.jepcc.test.resp.EbookResp;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class EbookService {
@Autowired
private EbookMapper ebookMapper;
public List<EbookResp> list(EbookReq req){
EbookExample ebookExample = new EbookExample();
EbookExample.Criteria criteria = ebookExample.or();
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;
}
}
- 修改controller层
EbookController
package com.jepcc.test.controller;
import com.jepcc.test.req.EbookReq;
import com.jepcc.test.resp.CommonResp;
import com.jepcc.test.resp.EbookResp;
import com.jepcc.test.service.EbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/ebook")
public class EbookController {
@Autowired
private EbookService ebookService;
@GetMapping("/list")
public CommonResp list(EbookReq req){
CommonResp resp = new CommonResp();
List<EbookResp> list = ebookService.list(req);
resp.setContent(list);
return resp;
}
}
- 验证下
封装BeanUtils,制作自己的工具类CopyUtil
新建包util,并在该包下新建工具类CopyUtil
,用来封装BeanUtils
以实现便捷的单体复制和列表复制。
这里使用了泛型,请注意public static <T> T copy(Object source,Class<T> clazz)
,第一个<T>
是泛型标识,后面的T
则是返回值类型。
package com.jepcc.test.util;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CopyUtil {
public static <T> T copy(Object source, Class<T> clazz){
if(source == null){
return null;
}
T obj = null;
try {
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;
}
}
接下来,对service层EbookService
进行修改。
- 第一种,调用
CopyUtil.copy()
package com.jepcc.test.service;
import com.jepcc.test.mapper.EbookMapper;
import com.jepcc.test.model.Ebook;
import com.jepcc.test.model.EbookExample;
import com.jepcc.test.req.EbookReq;
import com.jepcc.test.resp.EbookResp;
import com.jepcc.test.util.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class EbookService {
@Autowired
private EbookMapper ebookMapper;
public List<EbookResp> list(EbookReq req){
EbookExample ebookExample = new EbookExample();
EbookExample.Criteria criteria = ebookExample.or();
criteria.andNameLike("%"+req.getName()+"%");
List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);
List<EbookResp> respList = new ArrayList<>();
for(Ebook ebook:ebookList){
respList.add(CopyUtil.copy(ebook,EbookResp.class));
}
return respList;
}
}
- 第二种,调用
CopyUtil.copyList()
package com.jepcc.test.service;
import com.jepcc.test.mapper.EbookMapper;
import com.jepcc.test.model.Ebook;
import com.jepcc.test.model.EbookExample;
import com.jepcc.test.req.EbookReq;
import com.jepcc.test.resp.EbookResp;
import com.jepcc.test.util.CopyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class EbookService {
@Autowired
private EbookMapper ebookMapper;
public List<EbookResp> list(EbookReq req){
EbookExample ebookExample = new EbookExample();
EbookExample.Criteria criteria = ebookExample.or();
criteria.andNameLike("%"+req.getName()+"%");
List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);
return CopyUtil.copyList(ebookList,EbookResp.class);
}
}