251 lines
6.7 KiB
Markdown
251 lines
6.7 KiB
Markdown
# 快速开发模板
|
|
|
|
> 基于 JNPF 扩展开发的完整代码模板
|
|
> 整理时间:2026-04-01
|
|
|
|
---
|
|
|
|
## 一、后端快速模板
|
|
|
|
### 1.1 Entity
|
|
|
|
```java
|
|
package jnpf.entity;
|
|
|
|
import com.baomidou.mybatisplus.annotation.TableField;
|
|
import com.baomidou.mybatisplus.annotation.TableName;
|
|
import jnpf.base.entity.SuperExtendEntity;
|
|
import lombok.Data;
|
|
import lombok.EqualsAndHashCode;
|
|
import java.math.BigDecimal;
|
|
|
|
@EqualsAndHashCode(callSuper = true)
|
|
@Data
|
|
@TableName("Xxx")
|
|
public class XxxEntity extends SuperExtendEntity<String> {
|
|
|
|
@TableField("f_name")
|
|
private String name;
|
|
|
|
@TableField("f_amount")
|
|
private BigDecimal amount;
|
|
}
|
|
```
|
|
|
|
### 1.2 Mapper
|
|
|
|
```java
|
|
package jnpf.mapper;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import jnpf.base.mapper.SuperMapper;
|
|
import jnpf.entity.XxxEntity;
|
|
import jnpf.util.RandomUtil;
|
|
import jnpf.util.UserProvider;
|
|
import org.apache.ibatis.annotations.Mapper;
|
|
import java.util.Date;
|
|
|
|
@Mapper
|
|
public interface XxxMapper extends SuperMapper<XxxEntity> {
|
|
|
|
default XxxEntity getInfo(String id) {
|
|
return this.selectById(id);
|
|
}
|
|
|
|
default void create(XxxEntity entity) {
|
|
entity.setId(RandomUtil.uuId());
|
|
entity.setSortCode(RandomUtil.parses());
|
|
entity.setCreatorTime(new Date());
|
|
entity.setCreatorUserId(UserProvider.getUser().getUserId());
|
|
this.insert(entity);
|
|
}
|
|
|
|
default boolean update(String id, XxxEntity entity) {
|
|
entity.setId(id);
|
|
entity.setLastModifyTime(new Date());
|
|
entity.setLastModifyUserId(UserProvider.getUser().getUserId());
|
|
return this.updateById(entity) > 0;
|
|
}
|
|
|
|
default void delete(XxxEntity entity) {
|
|
this.deleteById(entity.getId());
|
|
}
|
|
}
|
|
```
|
|
|
|
### 1.3 Service
|
|
|
|
```java
|
|
// 接口
|
|
package jnpf.service;
|
|
public interface XxxService {}
|
|
// 实现
|
|
package jnpf.service.impl;
|
|
@Service
|
|
public class XxxServiceImpl extends SuperServiceImpl<XxxMapper, XxxEntity> implements XxxService {}
|
|
```
|
|
|
|
### 1.4 Controller
|
|
|
|
```java
|
|
package jnpf.controller;
|
|
|
|
@Tag(name = "功能名称", description = "Xxx")
|
|
@RestController
|
|
@RequestMapping("/api/extend/Xxx")
|
|
@RequiredArgsConstructor
|
|
public class XxxController extends SuperController<XxxService, XxxEntity> {
|
|
|
|
private final XxxService xxxService;
|
|
|
|
@Operation(summary = "列表")
|
|
@GetMapping
|
|
@SaCheckPermission("extend.xxx.list")
|
|
public ActionResult<PageListVO<XxxListVO>> list(PaginationXxx pagination) {
|
|
List<XxxEntity> data = xxxService.getList(pagination);
|
|
return ActionResult.page(JsonUtil.getJsonToList(data, XxxListVO.class),
|
|
JsonUtil.getJsonToBean(pagination, PaginationVO.class));
|
|
}
|
|
|
|
@Operation(summary = "详情")
|
|
@GetMapping("/{id}")
|
|
public ActionResult<XxxInfoVO> info(@PathVariable("id") String id) {
|
|
return ActionResult.success(JsonUtil.getJsonToBean(xxxService.getInfo(id), XxxInfoVO.class));
|
|
}
|
|
|
|
@Operation(summary = "新建")
|
|
@PostMapping
|
|
public ActionResult create(@RequestBody @Valid XxxCrForm form) {
|
|
xxxService.create(JsonUtil.getJsonToBean(form, XxxEntity.class));
|
|
return ActionResult.success(MsgCode.SU001.get());
|
|
}
|
|
|
|
@Operation(summary = "更新")
|
|
@PutMapping("/{id}")
|
|
public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid XxxUpForm form) {
|
|
boolean flag = xxxService.update(id, JsonUtil.getJsonToBean(form, XxxEntity.class));
|
|
return flag ? ActionResult.success(MsgCode.SU004.get()) : ActionResult.fail(MsgCode.FA002.get());
|
|
}
|
|
|
|
@Operation(summary = "删除")
|
|
@DeleteMapping("/{id}")
|
|
public ActionResult delete(@PathVariable("id") String id) {
|
|
XxxEntity entity = xxxService.getInfo(id);
|
|
if (entity != null) {
|
|
xxxService.delete(entity);
|
|
return ActionResult.success(MsgCode.SU003.get());
|
|
}
|
|
return ActionResult.fail(MsgCode.FA003.get());
|
|
}
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## 二、前端快速模板
|
|
|
|
### 2.1 API
|
|
|
|
```typescript
|
|
// #/api/extend/xxx.ts
|
|
import { defHttp } from '#/api/request';
|
|
|
|
enum Api { Prefix = '/api/extend/Xxx' }
|
|
|
|
export const getXxxList = (data) => defHttp.get({ url: Api.Prefix, data });
|
|
export const getInfo = (id) => defHttp.get({ url: `${Api.Prefix}/${id}` });
|
|
export const createXxx = (data) => defHttp.post({ url: Api.Prefix, data });
|
|
export const updateXxx = (data) => defHttp.put({ url: `${Api.Prefix}/${data.id}`, data });
|
|
export const delXxx = (id) => defHttp.delete({ url: `${Api.Prefix}/${id}` });
|
|
```
|
|
|
|
### 2.2 列表页面
|
|
|
|
```vue
|
|
<script lang="ts" setup>
|
|
import { useVxeTable } from '@jnpf/ui/vxeTable';
|
|
import { getXxxList, delXxx } from '#/api/extend/xxx';
|
|
|
|
const columns = [
|
|
{ title: '名称', dataIndex: 'name', width: 150 },
|
|
{ title: '金额', dataIndex: 'amount', width: 120 },
|
|
{ title: '时间', dataIndex: 'creatorTime', width: 150, format: 'date|YYYY-MM-DD' },
|
|
];
|
|
|
|
const [registerTable, { reload }] = useVxeTable({
|
|
api: getXxxList,
|
|
columns,
|
|
useSearchForm: true,
|
|
formConfig: {
|
|
schemas: [{ field: 'keyword', label: '关键词', component: 'Input' }]
|
|
}
|
|
});
|
|
|
|
const { createMessage, createConfirm } = useMessage();
|
|
|
|
function handleDel(id) {
|
|
createConfirm({
|
|
iconType: 'warning', title: '提示', content: '确认删除?',
|
|
onOk: () => { delXxx(id).then(res => { createMessage.success(res.msg); reload(); }); }
|
|
});
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<BasicVxeTable @register="registerTable">
|
|
<template #tableTitle>
|
|
<a-button type="primary" @click="handleAdd">新增</a-button>
|
|
</template>
|
|
<template #action="{ record }">
|
|
<TableAction :actions="[
|
|
{ label: '编辑', onClick: handleEdit.bind(null, record) },
|
|
{ label: '删除', color: 'error', modelConfirm: { onOk: handleDel.bind(null, record.id) } }
|
|
]" />
|
|
</template>
|
|
</BasicVxeTable>
|
|
</template>
|
|
```
|
|
|
|
---
|
|
|
|
## 三、数据库表结构
|
|
|
|
```sql
|
|
CREATE TABLE Xxx (
|
|
f_id VARCHAR(50) PRIMARY KEY,
|
|
f_name VARCHAR(100) COMMENT '名称',
|
|
f_amount DECIMAL(18,2) COMMENT '金额',
|
|
f_sortCode INT COMMENT '排序码',
|
|
f_creatorTime DATETIME COMMENT '创建时间',
|
|
f_creatorUser VARCHAR(50) COMMENT '创建人',
|
|
f_modifyTime DATETIME COMMENT '修改时间',
|
|
f_modifyUser VARCHAR(50) COMMENT '修改人',
|
|
f_deleteMark INT DEFAULT 0 COMMENT '删除标记'
|
|
);
|
|
```
|
|
|
|
---
|
|
|
|
## 四、关键配置清单
|
|
|
|
| 项目 | 配置 |
|
|
|------|------|
|
|
| 表名 | `Xxx` |
|
|
| 字段前缀 | `f_` |
|
|
| 主键 | `f_id` VARCHAR(50) |
|
|
| Controller 路径 | `/api/extend/Xxx` |
|
|
| 权限标识 | `extend.xxx.list` |
|
|
| 主键生成 | `RandomUtil.uuId()` |
|
|
| 审计字段 | 自动继承 |
|
|
|
|
---
|
|
|
|
## 五、参考示例
|
|
|
|
| 示例 | 路径 | 用途 |
|
|
|------|------|------|
|
|
| Employee | `EmployeeController` | 导入导出 |
|
|
| TableExample | `TableExampleController` | CRUD |
|
|
| saleOrder | `saleOrder/` | 主子表 |
|
|
| projectGantt | `projectGantt/` | 甘特图 |
|