按用户洞察 (jxcj 端到端跑通后沉淀): - 4 个 Java 模板更新 (entity/mapper/service-impl/controller) - SKILL.md 加 § 端到端实战教训段 (6 个关键点) - v3.0 § 11.3 backend-dev 行加 'jxcj 2026-06-29 端到端验证 200' 6 个关键点 (jxcj 实战踩坑): 1. Entity 必 extends jnpf SuperBaseEntity (主键 f_id 映射, 9 系统列继承) 2. Mapper 放 jnpf.mapper (jnpf 自带 @MapperScan 扫到) 3. @DS 在 Entity/Service/Controller 三处都加 (jnpf 拦截器分别走三个代理) 4. dacli app clear 后需 dacli app create 重建 (清空 app 关联) 5. 物理表需 dacli dm create 在新 app 关联库建 (dbLinkId 改变) 6. mvn package 不重打时需 rm jar 强制重打 (mtime 检查跳过) 模板更新: - entity.java: extends SuperBaseEntity + 9 系统列继承, 去掉重复 id 字段 - controller.java: @DS + 反射设 id (避免 setId 编译错) - service.java: status Integer → String - service-impl.java: 已有 @DS + SuperServiceImpl, 加 v0.12+ 实战注释
94 lines
3.2 KiB
Java
94 lines
3.2 KiB
Java
package ssw.{app_code}.service.impl;
|
|
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import jnpf.base.service.SuperServiceImpl;
|
|
import jnpf.mapper.XxxMapper;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.stereotype.Service;
|
|
import ssw.{app_code}.entity.XxxEntity;
|
|
import ssw.{app_code}.service.XxxService;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* {业务描述} Service 实现
|
|
*
|
|
* ⭐ FIX-TEMPLATE-V0.12+ (jxcj 实战验证, 端到端跑通 200):
|
|
* - extends SuperServiceImpl<XxxMapper, XxxEntity> — jnpf 基类
|
|
* - @DS("{app_code}") 在 Service 类上
|
|
* - 配套: Entity extends SuperBaseEntity, Mapper 放 jnpf.mapper, Controller 也加 @DS
|
|
*
|
|
* ⚠️ FIX-TEMPLATE-2/3: 关键 2 修复
|
|
* 1. create() 必须设 fSortCode=0 默认值 (PLT-NOT-NULL 根因)
|
|
* 2. update() 用反射合并非空字段 (PUT 时未传字段保留原值)
|
|
*
|
|
* ⚠️ FIX-TEMPLATE-1: 必须用 @DS("{app_code}") 注解指定业务库
|
|
* 不加 @DS 会使用 master 主库, 导致找不到业务表
|
|
*/
|
|
@Service
|
|
@DS("{app_code}") // 对应 application-dev.yml 里 spring.datasource.dynamic.datasource.{app_code}
|
|
@RequiredArgsConstructor
|
|
public class XxxServiceImpl
|
|
extends SuperServiceImpl<XxxMapper, XxxEntity>
|
|
implements XxxService {
|
|
|
|
@Override
|
|
public List<XxxEntity> getList(String keyword, String status) {
|
|
QueryWrapper<XxxEntity> qw = new QueryWrapper<>();
|
|
if (keyword != null && !keyword.isEmpty()) {
|
|
qw.and(w -> w.like("f_xxx_name", keyword).or().like("f_xxx_code", keyword));
|
|
}
|
|
if (status != null) qw.eq("f_status", status);
|
|
qw.orderByDesc("f_create_time");
|
|
return list(qw);
|
|
}
|
|
|
|
@Override
|
|
public XxxEntity getInfo(String id) {
|
|
return getById(id);
|
|
}
|
|
|
|
@Override
|
|
public void create(XxxEntity entity) {
|
|
// fId 由父类 SuperBaseEntity.@TableId 自动生成雪花 ID, 无需手动 set
|
|
// ⭐ FIX-TEMPLATE-2: fSortCode 默认值 (避免 INSERT 触发 f_sort_code NOT NULL 违规)
|
|
if (entity.getSortCode() == null) entity.setSortCode(0L);
|
|
save(entity);
|
|
}
|
|
|
|
@Override
|
|
public boolean update(XxxEntity entity) {
|
|
// ⭐ FIX-TEMPLATE-3: 反射合并非空字段 (避免 updateById 带 null 字段触发 NOT NULL)
|
|
XxxEntity db = this.getById(entity.getId());
|
|
if (db == null) return false;
|
|
try {
|
|
java.lang.reflect.Field[] fields = XxxEntity.class.getDeclaredFields();
|
|
for (java.lang.reflect.Field fld : fields) {
|
|
fld.setAccessible(true);
|
|
Object v = fld.get(entity);
|
|
if (v != null) fld.set(db, v);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return false;
|
|
}
|
|
return updateById(db);
|
|
}
|
|
|
|
@Override
|
|
public boolean delete(String id) {
|
|
return removeById(id);
|
|
}
|
|
|
|
@Override
|
|
public Map<String, Object> getStats() {
|
|
Map<String, Object> stats = new HashMap<>();
|
|
List<XxxEntity> all = list();
|
|
stats.put("total", all.size());
|
|
return stats;
|
|
}
|
|
}
|