网站建设制作周期,drupal个人门户网站开发,青岛媒体网地址,wordpress网络报名系统目录 一#xff0c;mybatis-plus常见注解二#xff0c;创建一个工具类和启动类三#xff0c;创建实体类四#xff0c;创建mapper接口五#xff0c;创建service接口和impl类六#xff0c;创建配置类七#xff0c;创建controller八#xff0c;使用测试工具测试增删改查和… 目录 一mybatis-plus常见注解二创建一个工具类和启动类三创建实体类四创建mapper接口五创建service接口和impl类六创建配置类七创建controller八使用测试工具测试增删改查和分页8.1测试全部查询8.2测试根据id查8.3测试模糊查询8.4测试新增8.5测试修改8.6测试删除8.7测试分页 九QueryWrapper介绍 一mybatis-plus常见注解 TableName 用于定义表名 TableId 用于定义表的主键 属性 value 用于定义主键字段名 type 用于定义主键类型主键策略 IdType具体策略如下
IdType.AUTO 主键自增系统分配不需要手动输入
IdType.NONE 未设置主键
IdType.INPUT 需要自己输入 主键值
IdType.ASSIGN_ID 系统分配 ID用于数值型数据Long对应 mysql 中 BIGINT 类型
IdType.ASSIGN_UUID 系统分配 UUID用于字符串型数据String对应 mysql 中 varchar(32) 类型统一配置主键策略 配置全局默认主键类型,实体类就不用加 TableId(value id, type IdType.AUTO)
mybatis-plus.global-config.db-config.id-typeautoTableField 用于定义表的非主键字段 属性 value 用于定义非主键字段名,用于别名匹配假如java对象属性和数据库属性不一样 exist 用于指明是否为数据表的字段 true 表示是false 为不是假如某个java属性在数据库没对应的字段则要标记为faslse fill 用于指定字段填充策略FieldFill用的不多 字段填充策略一般用于填充 创建时间、修改时间等字段FieldFill.DEFAULT 默认不填充FieldFill.INSERT 插入时填充FieldFill.UPDATE 更新时填充FieldFill.INSERT_UPDATE 插入、更新时填充。二创建一个工具类和启动类 util包 JsonData类 DemoApplication启动类 启动类
package com.demo;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;SpringBootApplication
MapperScan(com.demo.mapper) //public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class,args);}}
工具类
package com.demo.util;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;Data
AllArgsConstructor //会生成一个包含所有变量
NoArgsConstructor //生成一个无参数的构造方法
public class JsonData {/*** 状态码 0 表示成功1表示处理中-1表示失败*/private Integer code;/*** 数据*/private Object data;/*** 描述*/private String msg;// 成功传入数据public static JsonData buildSuccess() {return new JsonData(0, null, null);}// 成功传入数据public static JsonData buildSuccess(Object data) {return new JsonData(0, data, null);}// 失败传入描述信息public static JsonData buildError(String msg) {return new JsonData(-1, null, msg);}// 失败传入描述信息,状态码public static JsonData buildError(String msg, Integer code) {return new JsonData(code, null, msg);}
}
三创建实体类 Bean包 Lapop类 package com.demo.bean;import lombok.Data;Data
public class Lapop {/** 键盘id */private Integer id ;/** 键盘名称 */private String name ;/** 键盘尺寸 */private String size ;/** 键盘重量 */private String weight ;/** 电压 */private String voltage ;/** 电流 */private String current ;/** 键盘接口 */private String interfacepass ;/** 按键个数 */private String number ;
}四创建mapper接口 mapper包 LapopMapper接口 package com.demo.mapper;import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.bean.Lapop;public interface LapopMapper extends BaseMapperLapop {
}五创建service接口和impl类 service包 LapopService接口 impl包 LapopServiceImpl类 LapopService接口
package com.demo.service;import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.bean.Lapop;import java.util.List;
import java.util.Map;public interface LapopService {// 查询全部ListLapop getLapop();// 根据id查Lapop getByIdLapop(int id);//模糊查ListLapop getLapopBylist(Lapop lapop);// 新增int addLapop(Lapop lapop);// 修改int updateLapop(Lapop lapop);// 删除int deleteLapop(int id);// 分页IPageLapop selectPageVO(Integer LapopIPage, Integer queryWrapper);
}LapopServiceImpl类
package com.demo.service.impl;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.demo.bean.Lapop;
import com.demo.mapper.LapopMapper;
import com.demo.service.LapopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;
import java.util.Map;Service
public class LapopServiceImpl implements LapopService {Autowiredprivate LapopMapper lapopMapper;Overridepublic ListLapop getLapop() {// 查询全部ListLapop getLapopList lapopMapper.selectList(new QueryWrapperLapop());return getLapopList;}Overridepublic Lapop getByIdLapop(int id) {// 根据id查return lapopMapper.selectById(id);}Overridepublic ListLapop getLapopBylist(Lapop lapop) {// 模糊查询QueryWrapper queryWrapper new QueryWrapperLapop();queryWrapper.like(name,lapop.getName());queryWrapper.gt(Number,lapop.getNumber());return lapopMapper.selectList(queryWrapper);}Overridepublic int addLapop(Lapop lapop) {// 新增return lapopMapper.insert(lapop);}Overridepublic int updateLapop(Lapop lapop) {// 修改return lapopMapper.updateById(lapop);}Overridepublic int deleteLapop(int id) {// 删除return lapopMapper.deleteById(id);}Overridepublic IPageLapop selectPageVO(Integer LapopIPage, Integer queryWrapper) {// 分页QueryWrapperLapop wrapper new QueryWrapper();//第1页每页2条PageLapop page new Page(LapopIPage, queryWrapper);IPageLapop LapopbyIPage lapopMapper.selectPage(page, wrapper);System.out.println(总条数LapopbyIPage.getTotal());System.out.println(总页数LapopbyIPage.getPages());//获取当前数据return LapopbyIPage;}
}
六创建配置类 config包 MybatisPlusPageConfig类 配置分页插件 package com.demo.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;Configuration
public class MybatisPlusPageConfig {/*** 新的分页插件*/Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));return interceptor;}}七创建controller controller包 LapopController类 package com.demo.controller;import com.demo.bean.Lapop;
import com.demo.service.LapopService;
import com.demo.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;RestController
RequestMapping(/lapopController)
public class LapopController {Autowiredprivate LapopService lapopService;RequestMapping(/LapopList)ResponseBodypublic JsonData LapopList(){// 查询全部return JsonData.buildSuccess(lapopService.getLapop());}RequestMapping(/LapopByIDDList)ResponseBodypublic JsonData LapopByIDDList(int id){// 根据id查return JsonData.buildSuccess(lapopService.getByIdLapop(id));}RequestMapping(/paLapopByList)ResponseBodypublic JsonData paLapopByList(Lapop lapop){// 模糊查return JsonData.buildSuccess(lapopService.getLapopBylist(lapop));}RequestMapping(/insertLapop)public Object insertLapop(RequestBody Lapop lapop){// 新增int restue lapopService.addLapop(lapop);return JsonData.buildSuccess(restue);}RequestMapping(/updateLapop)public Object updateLapop(RequestBody Lapop lapop){// 修改int request lapopService.updateLapop(lapop);return JsonData.buildSuccess(request);}RequestMapping(/deleteLapop)public Object deleteLapop(int id){// 删除return JsonData.buildSuccess(lapopService.deleteLapop(id));}RequestMapping(/PageLapop)public Object PageLapop(Integer passerIPage, Integer queryWrapper ){// 分页return JsonData.buildSuccess(lapopService.selectPageVO(passerIPage,queryWrapper));}
}八使用测试工具测试增删改查和分页
8.1测试全部查询 8.2测试根据id查 8.3测试模糊查询 8.4测试新增 8.5测试修改 8.6测试删除 8.7测试分页 九QueryWrapper介绍
QueryWrapper介绍 可以封装sql对象包括where条件order by排序select哪些字段等等 查询包装类可以封装多数查询条件泛型指定返回的实体类
ListBannerDO list bannerMapper.selectList(new QueryWrapperBannerDO());核心API
- eq 等于
- ne 不等于
- gt 大于
- ge 大于等于
- lt 小于
- le 小于等于
- or 拼接or
- between 两个值中间
- notBetween 不在两个值中间
- like 模糊匹配
- notLike 不像
- likeLeft 左匹配
- likeRight 右边匹配
- isNull 字段为空
- in in查询
- groupBy 分组
- orderByAsc 升序
- orderByDesc 降序
- having having查询