前言
springboot----vue
一、忽略字段不展示给前端
在字段上加 @JsonIgnore
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String username;
@JsonIgnore
private String password;
private String nickname;
private String email;
private String phone;
private String address;
}
二、不用插件的方式实现分页查询
每加一个查询条件在findall和selecttotal中都需要去加入这个查询条件
- 后端
controller
@RequestMapping("/page")
private Map<String,Object> findpage(@RequestParam Integer pagenum,@RequestParam Integer pagesize,
@RequestParam String username){
//实现模糊查询
//加条件则需要在findpage和selecttotal中加这个条件
username="%"+username+"%";
//pagenum 表示第几页
Integer page=(pagenum-1)*pagesize;
//这一页的数据
List<User> data=userService.findpage(page,pagesize,username);
//查询总条数
Integer total=userMapper.seletotal(username);
Map<String,Object> res=new HashMap<>();
res.put("data",data);
res.put("total",total);
return res ;
}
mapper
//分页查询
@Select("select * from user where username like #{username}limit #{pagenum},#{pagesize}")
List<User> findpage(Integer pagenum,Integer pagesize,String username);
//查询总条数
@Select("select count(*) from user where username like #{username}")
Integer seletotal(String username);
- vue中
绑定数据
data(){
return {
tableData: [],
total: 0,
pagenum: 1,
pagesize: 2,
username: ""
}
},
显示
条件查询
<el-input style="width: 200px" suffix-icon="el-icon-search" placeholder="请输入名称" v-model="username"></el-input>
<el-button class="ml-5" type="primary" @click="loadpage">搜索</el-button>
表格数据显示
<el-table :data="tableData" border stripe :header-cell-class-name="headerBg">
<el-table-column prop="id" label="ID" width="70">
</el-table-column>
<el-table-column prop="username" label="用户名" width="140">
</el-table-column>
<el-table-column label="操作" width="200" align="center">
<template slot-scope="scope">
<el-button type="success">编辑 <i class="el-icon-edit"></i></el-button>
<el-button type="danger">删除 <i class="el-icon-remove-outline"></i></el-button>
</template>
</el-table-column>
</el-table>
<div style="padding: 10px 0">
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="pagenum"
:page-sizes="[2, 5, 10, 15]"
:page-size="pagesize"
layout="total, sizes, prev, pager, next, jumper"
:total="total">
</el-pagination>
</div>
实现动态
loadpage(){
request.get("http://localhost:9090/user/page",{
params: {
pagenum: this.pagenum,
pagesize: this.pagesize,
username: this.username,
nickname: this.nickname,
address: this.address
}
} ).then(res=>
{
this.tableData=res.records
this.total=res.total
})
},
handleSizeChange(pageSize){
this.pagesize=pageSize
this.loadpage()
},
handleCurrentChange(pageNum){
this.pagenum=pageNum
this.loadpage()
}
},
created(){
//页面一加载完就查询数据挂载
this.loadpage()
}
三、使用mybaties-plus实现分页查询
- 引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
- 配置文件
原来的@mapper不需要了,改成@MapperScan(“com.xhq.sv.mapper”)这种形式的包扫描
原来mybatis的xml还是会生效
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.xhq.sv.mapper")
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
3.sql日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- 实体类中指定表名,并且指定主键
@TableName(value = "user")
//指定表
@TableId(type = IdType.AUTO)
//指定主键
private Integer id;
@TableField(value = "user_address")
//将数据库中的列名user_address与类中的属性名address对应
private String address;
5.mapper要继承 BaseMapper
User为要操作的实体类,对应表
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
- Service
继承ServiceImpl<UserMapper,User>
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhq.sv.entity.User;
import com.xhq.sv.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService extends ServiceImpl<UserMapper,User> {
}
- controller
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhq.sv.entity.User;
import com.xhq.sv.mapper.UserMapper;
import com.xhq.sv.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/findall")
public List<User> findall(){
return userService.list();
}
@RequestMapping("/insert")
private boolean save( @RequestBody User user){
return userService.insert(user);
}
@RequestMapping("/delete/{id}")
private boolean delete(@PathVariable Integer id){
return userService.removeById(id);
}
//records 与 total
//给默认值
@RequestMapping("/page")
private IPage<User> findpage(@RequestParam Integer pagenum,
@RequestParam Integer pagesize,
@RequestParam(defaultValue = "")String username,
@RequestParam (defaultValue = "")String nickname,
@RequestParam (defaultValue = "")String address){
IPage<User> page=new Page<>(pagenum,pagesize);
QueryWrapper<User> queryWrapper =new QueryWrapper<>();
//多条件进行and查询
if(!"".equals(username)){
//如果为空就不拼接
queryWrapper.like("username",username);//进行模糊查询
}
if(!"".equals(nickname)){
queryWrapper.like("nickname",nickname);
}
if(!"".equals(address)){
queryWrapper.like("address",address);
}
return userService.page(page,queryWrapper);
}
}
- 前端接收数据的地方改为records就行
this.tableData=res.records
四、swagger
- swagger配置类
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
- swaggerConfig配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
@EnableOpenApi
public class SwaggerConfig {
/**
* 创建API应用
* apiInfo() 增加API相关信息
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
*
* @return
*/
@Bean
public Docket restApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("标准接口")
.apiInfo(apiInfo("Spring Boot中使用Swagger2构建RESTful APIs", "1.0"))
.useDefaultResponseMessages(true)
.forCodeGeneration(false)
.select()
.apis(RequestHandlerSelectors.basePackage("com.xhq.sv.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:localhost:8080/swagger-ui/index.html
*
* @return
*/
private ApiInfo apiInfo(String title, String version) {
return new ApiInfoBuilder()
.title(title)
.version(version)
.build();
}
}
3.springboot与swagger不兼容时在application.yml中添加配置
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher