一、代码生成器
代码生成器--》jeecgOneGUI
配置文件:resource/jeecg/jeecg_config.properties,修改目标生成的路径和包名
数据库连接:resource/jeecg/jeecg_database.properties,改数据库连接
生成内容:
(1)、后端:生成数据库表对应的实体,生成实体的增删改查相关接口(主要利用mybatis-plus自带封装的方法)
(2)、前端:生成的vue文件,功能类似用户管理,有数据列表展示和记录的增删改查
一、mybatis-plus
1、类的封装
服务类继承了IService,
接口类继承了ServiceImpl,implements了服务类
2、mybatis-plus的方法(常用):
新增:save(),
更新:updateById(),
查询(+查询器):getById()、list();getOne();
删除:removeById()
分页:page(page, queryWrapper);
3、实体类,可以set或者get属性。(代码生成器生成的实体类,会对表中字段名称进行修改,会将下划线改成驼峰命名)
疑问:接口获取参数对象:OperatStaff operatStaff或者@RequestBody OperatStaff operatStaff区别?
4、接口请求方式:
查询:get
新增:post
编辑:put
删除:delete
5、查询器:
示例1:普通查询器-查询
public Result<?> queryPageQuery(OperatStaff operatStaff,HttpServletRequest req) { QueryWrapper<OperatStaff> queryWrapper = QueryGenerator.initQueryWrapper(operatStaff, req.getParameterMap());(参数1:实体和参数值,参数2:所有参数和值) List<OperatStaff> list = operatStaffService.list(queryWrapper); return Result.ok(list); }
示例2:普通查询器-分页
public Result<?> queryPageList(OperatStaff operatStaff,Integer pageNo,Integer pageSize,HttpServletRequest req) { QueryWrapper<OperatStaff> queryWrapper = QueryGenerator.initQueryWrapper(operatStaff, req.getParameterMap()); Page<OperatStaff> page = new Page<OperatStaff>(pageNo, pageSize);(参数1:第几页,参数2:每页数量) IPage<OperatStaff> pageList = operatStaffService.page(page, queryWrapper); return Result.ok(pageList); }
示例3:普通查询器-复杂查询+日期过滤+排序
public Result<?> queryPageQuery(String starttime,String endtime,String pscode,String outputcode,String monitorid) { QueryWrapper<OperatCollect> queryWrapper = new QueryWrapper<OperatCollect>(); queryWrapper.ge("start_time", DateUtil.strToDate(starttime,"yyyy-MM-dd HH:mm:ss")); queryWrapper.le("start_time",DateUtil.strToDate(endtime,"yyyy-MM-dd HH:mm:ss")); queryWrapper.eq(oConvertUtils.isNotEmpty(pscode),"pscode",pscode); queryWrapper.eq(oConvertUtils.isNotEmpty(outputcode),"outputcode",outputcode); queryWrapper.eq(oConvertUtils.isNotEmpty(monitorid),"monitorid",monitorid); queryWrapper.orderByAsc("start_time");//排序 //queryWrapper.apply(oConvertUtils.isNotEmpty(startdate)," start_time>='"+startdate+"'"); //queryWrapper.apply(oConvertUtils.isNotEmpty(endtime)," start_time<='{0}'",endtime); List<OperatCollect> pageList = operatCollectService.list(queryWrapper); return Result.ok(pageList); }
示例4:兰博查询器
LambdaQueryWrapper<SysRolePermission> query = new LambdaQueryWrapper<SysRolePermission>() eq(SysRolePermission::getPermissionId, permissionId) .eq(SysRolePermission::getRoleId,roleId); SysRolePermission sysRolePermission = sysRolePermissionService.getOne(query);
LambdaQueryWrapper<SysPermission> query = new LambdaQueryWrapper<SysPermission>(); query.eq(SysPermission::getDelFlag, CommonConstant.DEL_FLAG_0); query.orderByAsc(SysPermission::getSortNo); List<SysPermission> list = sysPermissionService.list(query);
LambdaQueryWrapper和QueryWrapper区别:
QueryWrapper 的列名匹配使用的是 “数据库中的字段名(一般是下划线规则)”
LambdaQueryWrapper 的列名匹配使用的是“Lambda的语法,偏向于对象”,兼容性更好
炫技示例:
OperatCompany oc=this.operatCompanyService.getOne(new LambdaQueryWrapper<OperatCompany>().eq(OperatCompany::getName, username)); if (oc == null) return Result.error("用户不存在!"); String passwordEncode = PasswordUtil.encrypt(oc.getId(), password, oc.getSalt()); operatCompanyService.update(new OperatCompany().setPassword(passwordEncode),new QueryWrapper<OperatCompany>().eq(oConvertUtils.isNotEmpty(username),"name",username));