1.表格初始化
@Data
@EqualsAndHashCode(callSuper = true)
@Table("表名") // @Table("t_tab_${cid}") 动态表名
@TableMeta("{'mysql-charset':'utf8mb4'}")
//索引
@TableIndexes({
@Index(name = "IDX_DEVICE_HANDLER_CODE", fields = "code")
})
@Comment("注释")
@ApiModel(description = "Api注释")
public class 类名 extends BaseModel implements Serializable{
private static final long serialVersionUID = 1L;
@Column
@Name
@Comment("注释")
@ColDefine(type = ColType.VARCHAR, width = 32)
@PrevInsert(els = {@EL("snowflake()")})
@PrevInsert(uu32 = true)
@PrevInsert(els = {@EL("snowflake()",nullEffective = true)}) //上面的赋值规则要起效必须是在[当前字段==null]时才能生效
@ApiModelProperty(description = "id")
// @ApiModelProperty(description = "应用ID", required = true, check = true, validation = Validation.UPPER) 验证格式
private String id;
@Column
@Comment("注释")
@ColDefine(type = ColType.VARCHAR, width = 50)
@ApiModelProperty(name = "name", description = "名称")
private String name;
//时间戳 不设置长度和类型
@Column
@Comment("注释")
@ApiModelProperty(name = "time", description = "时间戳")
private Long time;
}
/**
接口
*/
public interface 接口名称 {
}
/**
实现类 dao
*/
@IocBean(args = {"refer:dao"})
public class --Impl extends BaseServiceImpl<--> implements --Service {
public DeviceInfoServiceImpl(Dao dao) {
super(dao);
}
}
/**
实现类 zMongoClient
*/
@IocBean(args = {"refer:zMongoClient"})
public class --Impl extends BaseServiceImpl<--> implements --Service {
private static final String COLLECTION_NAME = Device_cmd_record.class.getSimpleName().toLowerCase(Locale.ROOT);
private final ZMongoClient zMongoClient;
public DeviceCommandRecordHistoryServiceImpl(@Inject ZMongoClient zMongoClient) {
this.zMongoClient = zMongoClient;
}
}
2.控制类,增删改查
/**
控制类以及增删改查
*/
@IocBean
@At("/--/--")
@SLog(tag = "日志标签")
@ApiDefinition(tag = "Device_info")
@Slf4j
public class DeviceInfoController {
//列表
@At
@Ok("json")
//@Ok("json:full")
@POST
@ApiOperation(name = "分页查询")
@ApiFormParams(
{
@ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
@ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
@ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
@ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式"),
@ApiFormParam(name = "--", example = "--")
}
)
@ApiResponses(
implementation = Pagination.class
)
//权限标识
@SaCheckPermission("device.info")
//只有登录后才能进入此方法
@SaCheckLogin
public Result<?> list(@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
Pagination pagination = deviceInfoService.listPage(pageNo, pageSize, Cnd.NEW().asc("location"));
return Result.data(pagination);
}
//新增
@At
@Ok("json")
@POST
@ApiOperation(name = "--")
@ApiFormParams(
implementation = 类名.class
)
@ApiResponses
@SLog("新增Device_info:${deviceInfo.id}")
@SaCheckPermission("--")
//@SaCheckLogin
public Result<?> create(@Param("..") Device_info deviceInfo, HttpServletRequest req) {
deviceInfo.setCreatedBy(SecurityUtil.getUserId());
deviceInfoService.insert(deviceInfo);
return Result.success();
}
//编辑
@At
@Ok("json")
@POST
@ApiOperation(name = "--")
@ApiFormParams(
implementation = Device_info.class
)
@ApiResponses
@SLog("修改Device_info:${deviceInfo.name}")
@SaCheckPermission("device.info.update")
public Result<?> update(@Param("..") Device_info deviceInfo, HttpServletRequest req) {
deviceInfo.setUpdatedBy(SecurityUtil.getUserId());
deviceInfoService.updateIgnoreNull(deviceInfo);
return Result.success();
}
//详情
@At("/get/{id}")
@Ok("json")
@GET
@ApiOperation(name = "--")
@ApiImplicitParams(
{
@ApiImplicitParam(name = "id", in = ParamIn.PATH, required = true, check = true)
}
)
@ApiResponses
@SaCheckPermission("device.info")
public Result<?> getData(String id, HttpServletRequest req) { //因为id是拼接在URL上的不是参数,所以这里不用@Param
Device_info deviceInfo = deviceInfoService.fetch(id);
if (deviceInfo == null) {
return Result.error(ResultCode.NULL_DATA_ERROR);
}
return Result.data(deviceInfo);
}
//删除
@At("/delete/{id}")
@Ok("json")
@DELETE
@ApiOperation(name = "删除Device_info")
@ApiImplicitParams(
{
@ApiImplicitParam(name = "id", in = ParamIn.PATH, required = true, check = true)
}
)
@ApiResponses
@SLog("删除Device_info:")
@SaCheckPermission("device.info.delete")
public Result<?> delete(String id, HttpServletRequest req) {
Device_info deviceInfo = deviceInfoService.fetch(id);
if (deviceInfo == null) {
return Result.error(ResultCode.NULL_DATA_ERROR);
}
deviceInfoService.delete(id);
req.setAttribute("_slog_msg", deviceInfo.getId());
return Result.success();
}
//删除多个
@At("/deleteMore/{ids}")
@Ok("json")
@DELETE
@ApiOperation(name = "删除Device_info")
@ApiImplicitParams(
{
@ApiImplicitParam(name = "ids", in = ParamIn.PATH, required = true, check = true)
}
)
@ApiResponses
@SLog("删除Device_info:")
@SaCheckPermission("device.info.delete")
public Result<?> deleteMore(String ids, HttpServletRequest req) {
if(Strings.isBlank(ids)){
return Result.error("id为空");
}
String[] idList = ids.split(",");
List<String> list = Arrays.asList(idList);
for (String it : list) {
Device_info deviceInfo = deviceInfoService.fetch(it);
if (deviceInfo == null) {
return Result.error(ResultCode.NULL_DATA_ERROR);
}
}
deviceInfoService.delete(list);
return Result.success();
}
}
3.回调函数
//回调函数 返回指定类的集合 也可以是指定类
//前提是类声明了 @Table 并且每个字段上的 @Column 可以同你的 ResultSet 配置起来
sql.setCallback(Sqls.callback.entities());
sql.setEntity(this.dao().getEntity(Device_info.class));
this.dao().execute(sql);
List<Device_info> list = sql.getList(Device_info.class);
//自定义回调函数 这个返回值就靠自定义
//ResultSet rs 就是 select 后面的查询出来的东西
sql.setCallback(new SqlCallback() {
@Override
public Object invoke(Connection conn, ResultSet rs, Sql sql) throws SQLException {
List<String> strings = new ArrayList<>();
while (rs.next()) {
// 具体操作
}
return strings;
}
});
常用回调
名称 | 结果类型 | 备注 |
---|---|---|
bool | Boolean | |
bools | boolean[] |
1.r.62及之前的版本是LinkedArray |
doubleValue | Double | |
entities | List<Pojo> |
取决于Entity对应的Pojo类 |
entity | Pojo | 取决于Entity对应的Pojo类 |
floatValue | Float | |
integer | Integer | |
ints | int[] |
|
longValue | Long | |
longs | long[] |
|
map | NutMap | 与Record类型,但区分大小写 |
maps | List<NutMap> |
|
record | Record | 字段名均为小写 |
records | List<Record> |
|
str | String | |
strList | List<String> |
4.动态表名的生成
protected Map<String, Dao> ymDaoMap = new HashMap<>();
//只有动态yearMonth
public Dao ymdao(String key) {
String month = key;
key = key + this.getEntityClass().getSimpleName();
Dao dao = ymDaoMap.get(key);
if (dao == null) {
synchronized (this) {
dao = ymDaoMap.get(key);
if (dao == null) {
dao = Daos.ext(this.dao(), month);
//如果存在表是否强制移除
dao.create(this.getEntityClass(), false);
ymDaoMap.put(key, dao);
try {
Daos.migration(dao, this.getEntityClass(), true, false);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
}
return dao;
}
/**
动态yearMonth 和 动态类
使用返回的dao对象可以调用它的方法进行增删改查
*/
ymdao(format, this.getEntityClass());
public Dao ymdao(String key, Class clz) {
String month = key;
key = key + clz.getSimpleName();
Dao dao = ymDaoMap.get(key);
if (dao == null) {
synchronized (this) {
dao = ymDaoMap.get(key);
if (dao == null) {
dao = Daos.ext(this.dao(), month);
dao.create(clz, false);
ymDaoMap.put(key, dao);
try {
Daos.migration(dao, clz, true, false);
} catch (Throwable e) {
}
}
}
}
return dao;
}
/**
ymdao操作
classOfT 如果这个动态表名不存在即创建这张表
dropIfExists 如果这张表已经存在是否强制移除
*/
ymdao().create(Class<T> classOfT, boolean dropIfExists);
5.枚举类模板
@JsonShape(JsonShape.Type.OBJECT)
public enum ArrearsStatusEnum {
private int value;
private String text;
ArrearsStatusEnum(int value, String text) {
this.value = value;
this.text = text;
}
public static ArrearsStatusEnum from(int value) {
for (ArrearsStatusEnum c : ArrearsStatusEnum.values()) {
if (value == c.value) {
return c;
}
}
return null;
}
public static NutMap toMap() {
NutMap map = NutMap.NEW();
for (ArrearsStatusEnum v : ArrearsStatusEnum.values()) {
map.put(String.valueOf(v.getValue()), v.getText());
}
return map;
}
public int value() {
return value;
}
public String text() {
return text;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
6.发送url请求
1.构建发送请求
Request re = Request.create(url,Request.METHOD.POST);
2.参数
NutMap nut = NutMap.NEW(); nut.addv(); nut.addv();
3.头部
Header header = Header.create(); header.set()
4.组合
re.setParams(nut); re.setHeader(header);
5.发送
Response response = Sender.create(re).send(); if (response.isOK()) { String content = Strings.sNull(response.getContent()); } }
6.通过拦截器签名验证构建并发送请求
//签名验证的注解 @Filters({@By(type = ApiHeaderSignFilter.class)}) //这个注解是注释在主模块\子模块\入口函数上的 ApiHeaderSignFilter是V8里的签名验证类 //通过Test构建签名验证请求 @Test public void testSign(){ }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 为DeepSeek添加本地知识库
· 精选4款基于.NET开源、功能强大的通讯调试工具
· DeepSeek智能编程
· 大模型工具KTransformer的安装
· [计算机/硬件/GPU] 显卡