1.仿小程序「朕正有词」项目:翻页功能
一.列表词条
1.在网页端获取全部数据
xml文件:
<!--标签select代表里面的语句是select,id="select"是对词条sql的命名,方便理解即可,parameterType代表参数类型-->
<!--数据接下来会进入Mapper层-->
<select id="select" parameterType="com.example.test.domain.CitiaoExample" resultMap="BaseResultMap">
select * from citiao
</select>
通过xml文件里的sql语句获取citiao里的全部数据
Mapper文件:
//List<Citiao>意思是定义一个以Citiao为基本类型的对象
//Citiao是通过generator-config.xml自动创建出来的,generator-config.xml文件代码在最后
//这里的select指向的是xml文件里的id="select"
//数据接下来会进入Service层里
List<Citiao> select();
Service文件:
//由于Mapper文件使用的是List<Ctiao>所以此文件也需要用List<Citiao>
//return返回的是citiaoMapper里的select方法
//数据接下来会进入Controller层里
public List<Citiao> list(){
return citiaoMapper.select();
}
Controller文件:
//GetMapping后面的/citiao是网址路径
//示例:http://127.0.0.1:8888/citiao/list
//其中的8888是指我映射的端口
//中间的citiao是在类名上一行级里写入了@RequestMapping("/citiao")
@GetMapping("/list")
//由于其他文件使用的是List<Ctiao>所以此文件也需要用List<Citiao>
public List<Citiao> test(){
//return返回的是citiaoSerice里的list方法
return citiaoSerice.list();
}
在运行正常后用浏览器打开,网址:http://127.0.0.1:8888/citiao/list,即可获取到citiao表里的数据
在打开这个网址后,请求会依次传达 浏览器–>Controller文件–>Service文件–>Mapper文件–>xml文件
在xml文件获取数据后,再依次返回 浏览器<–Controller文件<–Service文件<–Mapper文件<–xml文件
Http文件:
//运行此文件只会获得后端发来的数据
//此文件没有指定放在哪个文件里,推荐先在跟目录里创建一个http文件夹,然后创建出的http文件放在这里
GET http://127.0.0.1:8888/citiao/list
2.分页功能
牵扯到分页,其中会涉及到一个插件,名称叫做PageHelper,开源网址: https://pagehelper.github.io/
首先要在maven里添加依赖,也就是pom.xml文件
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.6</version>
</dependency>
创建一个req文件夹,里面放入一个pageReq的类
package com.example.test.req;
public class PageReq {
private int page;
private int size;
}
右键选择Generate,或者快捷键command+N,win系统应该是control+N
跳出一个小弹窗后选择里面的Getter and Setter
注意:进入Getter and Setter后点击第一行后,需要一直按住shift,然后点击最下面的一行,再之后创建才可以,不然只会创建一个文件,toString同理
系统会自动在文件里创建出方法
应该会成为这个样子:
package com.example.test.req;
public class PageReq {
private int page;
private int size;
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
}
再创建出一个类,名称建议是[类名Req.java]
package com.example.test.req;
//注意,要继承PageReq
public class CitiaoReq extends PageReq{
private int id;
private String name;
}
使用Getter and Setter方法后再使用小弹窗里的toString方法
应该会变成这样:
package com.example.test.req;
public class CitiaoReq extends PageReq{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "CitiaoReq{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
还有封装类:
package com.example.test.util;
//R<T>是将这个类里面的值转换为泛类型
public class R<T> {
//创建三个字段,然后使用Getter and Setter方法,再使用toString
private int code = 0;
private String info = "success";
private T data;
//上面三个字段
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
@Override
public String toString() {
final StringBuffer sb = new StringBuffer("ResponseDto{");
sb.append("code=").append(code);
sb.append(", info='").append(info).append('\'');
sb.append(", data=").append(data);
sb.append('}');
return sb.toString();
}
}
更改Service文件:
import com.github.pagehelper.PageHelper;
import com.example.test.domain.Citiao;
import com.example.test.domain.CitiaoExample;
import org.springframework.stereotype.Service;
import com.example.test.mapper.CitiaoMapper;
import com.example.test.req.CitiaoReq;
import javax.annotation.Resource;
import java.util.*;
//此注解意思是此文件是Service层
@Service
public class CitiaoSerice {
//@Resource 默认按 byName 自动注入,上网查了一些,也没明白是什么意思,后面可能会单独建一个页面来讲
@Resource
//创建一个通过CitiaoMapper这个类创建一个citiaoMapper对象
private CitiaoMapper citiaoMapper;
//List<Citiao>是以Citiao创建一个list对象,后面的CitiaoReq req是以CitiaoReq声明一个req的对象
public List<Citiao> list(CitiaoReq req){
//这里就要使用PageHelper了,括号中放的是参数,分别是CitiaoReq里的getPage方法和getSize方法
//因为之前已经用CitiaoReq类型声明了一个名为req的对象,所以这里使用的是req.getPage而不是CitiaoReq
PageHelper.startPage(req.getPage(),req.getSize());
//使用CitiaoExample实例化一个citiaoExample
CitiaoExample citiaoExample = new CitiaoExample();
//返回citiaoMapper方法里的selectByExample方法,并且里面写入了citiaoExample
return citiaoMapper.selectByExample(citiaoExample);
}
}
更改Controller文件:
package com.example.test.controller;
import com.example.test.domain.Citiao;
import com.example.test.req.CitiaoReq;
import com.example.test.req.LikeReq;
import com.example.test.service.CitiaoSerice;
import com.example.test.util.R;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
//@RestController注解,相当于@Controller+@ResponseBody两个注解的结合
//返回json数据不需要在方法前面加@ResponseBody注解了
@RestController
//使用@RequestMapping即可在文件里使用@GetMapping和@PostMapping这两种请求方式
@RequestMapping("/citiao")
public class CitiaoController {
//后面可能会去写一篇专门讲注解的
@Resource
//使用CitiaoSerice类型声明citiaoSerice
private CitiaoSerice citiaoSerice;
//使用@GetMapping声明当前接口以get发起请求
@GetMapping("/list")
//使用泛类型R,圆括号里的是以CitiaoReq类型声明一个req
public R list(CitiaoReq req){
//用R实例化一个r
R r = new R<>();
//调用r,也就是R里面的setData方法,在里面是citiaoService也就是CitiaoSerice里的list方法
//里面的req就是还没有填写的值
r.setData(citiaoSerice.list(req));
//返回r
return r;
}
}
来到之前创建的http文件里
其中的page是第几页数,size是一页显示几条
GET http://127.0.0.1:8888/citiao/list?page=1&size=3
generator-config.xml文件:
<?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" defaultModelType="flat">
<!-- 自动检查关键字,为关键字增加反引号 -->
<property name="autoDelimitKeywords" value="true"/>
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!--覆盖生成XML文件-->
<plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin" />
<!-- 生成的实体类添加toString()方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 不生成注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--下面的com.mysql.cj.jdbc.Driver代表从哪个包里去寻找驱动-->
<!--cj是后加的,如果为老版本则不需要加cj-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3307/wiki2?serverTimezone=Asia/Shanghai"
userId="root"
password="123456">
<!--jdbc:mysql://为固定格式-->
<!--localhost:3307 其中localhost代表本机,而3307代表其对应的端口-->
<!--wiki2是想要连接的数据库-->
<!--serverTimezone=Asia/Shanghai 如果在亚洲,需要添加此段语句,不然时驱会发生错误-->
</jdbcConnection>
<!-- domain类的位置 -->
<javaModelGenerator targetProject="src/main/java"
targetPackage="com.example.test.domain"/>
<!-- mapper xml的位置 -->
<sqlMapGenerator targetProject="src/main/resources"
targetPackage="mapper"/>
<!-- mapper类的位置 -->
<javaClientGenerator targetProject="src/main/java"
targetPackage="com.example.test.mapper"
type="XMLMAPPER"/>
<!--底下tableName后的字符,代表用当前数据库里的哪一个表来创建 -->
<!--table tableName="demo" domainObjectName="Demo"/>-->
<!--<table tableName="ebook"/>-->
<!--<table tableName="category"/>-->
<!--<table tableName="doc"/>-->
<!--<table tableName="content"/>-->
<!--<table tableName="user"/>-->
<!--<table tableName="ebook_snapshot"/>-->
<!--<table tableName = "like"/>-->
</context>
</generatorConfiguration>
req在有些项目里叫dao,有些项目里叫bean,有些还会叫from
Warning: Undefined variable $aria_req in /www/wwwroot/l.lvovl.cn/wp-content/themes/JieStyle-Two-master/comments.php on line 26
Warning: Undefined variable $aria_req in /www/wwwroot/l.lvovl.cn/wp-content/themes/JieStyle-Two-master/comments.php on line 27
Warning: Undefined variable $aria_req in /www/wwwroot/l.lvovl.cn/wp-content/themes/JieStyle-Two-master/comments.php on line 28