背景
若依(RuoYi)开源框架是一个功能强大的Java开发框架,专注于快速构建企业级后台管理系统。它提供了一套丰富的功能和模块,可以帮助开发人员快速搭建稳定、高效的管理系统。本篇博客将大家了解若依框架的基本概念和使用方法,帮助您快速上手。
系统需求
- JDK >= 1.8
MySQL >= 5.7
Maven >= 3.0
Node >= 12
Redis >= 3
这里是若依官网,还有在线演示
具体的若依介绍,大家可以看官网或者这篇文章:
https://blog.csdn.net/weixin_49185262/article/details/131448994
我主要说一下基于若依快速二次开发的方法,给自己做个记录,若有不对的地方欢迎指出批评,过程中的相关的代码都是示例代码,大家仅供参考!
想基于若依快速开发,但是若依有一些能力规范还不够全面,所以需要稍微处理一下,接下来把需要修改的几点罗列一下
一、修改配置
1、mybatis-plus 适配
关于Mybatis-plus的使用,大家可以参考我的这篇文章:
使用Mybatis-plus清空表数据_mybatisplus删除表所有内容_Alex_81D的博客-CSDN博客
1.1 添加pom
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version>
</dependency>
1.2 修改配置类
增加 MybatisPlusConfig.java,并注释掉 MyBatisConfig
内容如下:
package com.ruoyi.framework.config;import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;/*** Mybatis Plus 配置*/
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig
{@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor(){MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();// 分页插件interceptor.addInnerInterceptor(paginationInnerInterceptor());// 乐观锁插件interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());// 阻断插件interceptor.addInnerInterceptor(blockAttackInnerInterceptor());return interceptor;}/*** 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html*/public PaginationInnerInterceptor paginationInnerInterceptor(){PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();// 设置数据库类型为mysqlpaginationInnerInterceptor.setDbType(DbType.MYSQL);// 设置最大单页限制数量,默认 500 条,-1 不受限制paginationInnerInterceptor.setMaxLimit(-1L);return paginationInnerInterceptor;}/*** 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html*/public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor(){return new OptimisticLockerInnerInterceptor();}/*** 如果是对全表的删除或更新操作,就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html*/public BlockAttackInnerInterceptor blockAttackInnerInterceptor(){return new BlockAttackInnerInterceptor();}
}
1.3 修改配置文件
增加如下配置,并注释掉原来的mybatis的配置信息
# MyBatis Plus配置
mybatis-plus:configuration:map-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.stdout.StdOutImpl# 搜索指定包别名typeAliasesPackage: com.ruoyi.**.domain# 配置mapper的扫描,找到所有的mapper.xml映射文件mapperLocations: classpath*:mapper/**/*Mapper.xml
至此,mybatis-plus改造完成
2.增加 lombok
pom文件内容如下:
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.24</version>
</dependency>
3.mysql版本调整
这步有需要调整,没需要可以不调整:在ruoyi-admin/pom.xml
<!-- Mysql驱动包 -->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.16</version><scope>runtime</scope>
</dependency>
4.增加Knife4j
Knife4j的前身是swagger-bootstrap-ui,可以让swagger显示更加优美
4.1 修改pom
<!-- knife4j -->
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.3</version>
</dependency>
4.2 修改配置
在SwaggerConfig中修改内容
/*** 创建API*/
@Bean
public Docket createRestApi()
{return new Docket(DocumentationType.OAS_30)// 是否启用Swagger.enable(enabled)// 用来创建该API的基本信息,展示在文档的页面中(自定义展示的信息).apiInfo(apiInfo())// 设置哪些接口暴露给Swagger展示.select()// 扫描所有有注解的api,用这种方式更灵活//.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))// 扫描指定包中的swagger注解// .apis(RequestHandlerSelectors.basePackage("com.ruoyi.project.tool.swagger"))// 扫描所有.apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build()/* 设置安全模式,swagger可以设置访问token */.securitySchemes(securitySchemes()).securityContexts(securityContexts()).pathMapping(pathMapping);
}
4.3 前端改法
来看效果:
是不是比一开始的时候好一点
对比一下:
修改的先写这么多,基本上够用了
二、开发说明
1.XxxController
/*** @Author* @Description * @Version 1.0*/
@RestController
@RequestMapping("/freight/freight")
public class FreightBillController extends BaseController {@Autowiredprivate FreightBillService freightBillService;@PostMapping("/selectFreightBillList")public TableDataInfo selectFreightBillList(@RequestBody FreightBillDTO freightBill){startPage();List<FreightBill> areaList = freightBillService.selectFreightBillList(freightBill);return getDataTable(areaList);}@GetMapping("/selectFreightBillById/{id}")public AjaxResult selectFreightBillById(@PathVariable("id") Long id){FreightBill freightBill = freightBillService.selectFreightBillById(id);return AjaxResult.success(freightBill);}/*** 导出【请填写功能名称】列表*/@Log(title = "导出", businessType = BusinessType.EXPORT)@PostMapping("/export")@ResponseBodypublic AjaxResult export(@RequestBody FreightBillDTO freightBill){List<FreightBill> list = freightBillService.selectFreightBillList(freightBill);ExcelUtil<FreightBill> util = new ExcelUtil<FreightBill>(FreightBill.class);return util.exportExcel(list, "XX数据");}/*** 新增保存*/@Log(title = "XX新增", businessType = BusinessType.INSERT)@PostMapping("/add")@ResponseBodypublic AjaxResult addSave(@RequestBody FreightBillDTO freightBill){freightBillService.insertFreightBill(freightBill);return success();}/*** 修改保存*/@Log(title = "修改XX", businessType = BusinessType.UPDATE)@PostMapping("/updateFreightBill")@ResponseBodypublic AjaxResult updateFreightBill(@RequestBody FreightBillDTO freightBill){freightBillService.updateFreightBill(freightBill);return success();}/*** 删除*/@Log(title = "删除XX", businessType = BusinessType.DELETE)@PostMapping( "/deleteFreightBillByIds")@ResponseBodypublic AjaxResult deleteFreightBillByIds(String ids){freightBillService.deleteFreightBillByIds(ids);return success();}/*** 删除*/@Log(title = "删除XX", businessType = BusinessType.DELETE)@PostMapping( "/deleteFreightBillById")@ResponseBodypublic AjaxResult deleteFreightBillById(Long id){freightBillService.deleteFreightBillById(id);return success();}}
2.XxService
/*** @Author * @Description TODO* @Version 1.0*/
public interface FreightBillService extends IService<FreightBill> {/*** 根据id查询数据** @param id* @return*/public FreightBill selectFreightBillById(Long id);/*** 查询接口* @param freightBill* @return*/List<FreightBill> selectFreightBillList(FreightBillDTO freightBill);/*** 新增【请填写功能名称】** @param freightBill 【请填写功能名称】* @return 结果*/void insertFreightBill(FreightBillDTO freightBill);/*** 修改【请填写功能名称】** @param freightBill 【请填写功能名称】* @return 结果*/void updateFreightBill(FreightBillDTO freightBill);/*** 批量删除【请填写功能名称】** @param ids 需要删除的【请填写功能名称】主键集合* @return 结果*/void deleteFreightBillByIds(String ids);/*** 删除【请填写功能名称】信息** @param id 【请填写功能名称】主键* @return 结果*/boolean deleteFreightBillById(Long id);
}
3.XxServiceImpl
@Service
public class FreightBillServiceImpl extends ServiceImpl<FreightBillMapper, FreightBill> implements FreightBillService {/*** 查询接口** @param freightBill* @return*/@Overridepublic List<FreightBill> selectFreightBillList(FreightBillDTO freightBill) {//根据条件查询数据信息LambdaQueryWrapper<FreightBill> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(FreightBill::getEnableFlag, "0");List<FreightBill> freightBillList = this.list(queryWrapper);return freightBillList;}/*** 查询*/@Overridepublic FreightBill selectFreightBillById(Long id) {LambdaQueryWrapper<FreightBill> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(FreightBill::getEnableFlag, "0");queryWrapper.eq(FreightBill::getId, id);FreightBill freightBill = this.getOne(queryWrapper);return freightBill;}/*** 新增** @return 结果*/@Override@Transactional(rollbackFor = Exception.class)public void insertFreightBill(FreightBillDTO freightBillDTO) {FreightBill freightBill = new FreightBill();BeanUtils.copyProperties(freightBillDTO, freightBill);freightBill.setEnableFlag("0");//将当前用户插入表中SysUser useId = ShiroUtils.getSysUser();freightBill.setUserId(useId.getUserId());freightBill.setUserName(useId.getUserName());this.save(freightBill);}/*** 修改** @return 结果*/@Override@Transactional(rollbackFor = Exception.class)public void updateFreightBill(FreightBillDTO freightBillDTO) {Long id = freightBillDTO.getId();FreightBill freightBill = this.selectFreightBillById(id);if (!ObjectUtils.isEmpty(freightBill)) {BeanUtils.copyProperties(freightBillDTO, freightBill);updateById(freightBill);} else {throw new ServiceException("数据不存在!");}}/*** 批量删除** @return 结果*/@Override@Transactional(rollbackFor = Exception.class)public void deleteFreightBillByIds(String ids) {Long[] freightBillids = Convert.toLongArray(ids);for (Long freightBillid : freightBillids) {this.deleteFreightBillById(freightBillid);}}/*** 删除** @return 结果*/@Override@Transactional(rollbackFor = Exception.class)public boolean deleteFreightBillById(Long id) {boolean deleteFlag = false;FreightBill freightBill = this.selectFreightBillById(id);if (!ObjectUtils.isEmpty(freightBill)) {freightBill.setEnableFlag("2");deleteFlag = updateById(freightBill);} else {throw new ServiceException("数据不存在!");}return deleteFlag;}
}
4. XxMapper
/*** @Author * @Description TODO* @Version 1.0*/
public interface FreightBillMapper extends BaseMapper<FreightBill> {
}
这里有需要复杂sql操作了,可以在这里写,然后service中引用,我这里给的是极简的示例,仅供参考!
5.XxVO
/*** @Author * @Description TODO* @Version 1.0*/
@Data
@TableName("freight_bill")
public class FreightBill {private static final long serialVersionUID = 1L;/** 主键 */@TableField("id")@TableId(type= IdType.AUTO)private Long id;/** XX */@Excel(name = "车辆类型")@TableField("cartype")private String cartype;}
基本上就是这么用的,仅供参考啊
三、PostMan调用示例
备注:我用的单机版做的示例,按照自己的版本进行调整
把页面上的cookie拿过来放到headers中通过权限校验
调用完成!