You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
4.3 KiB
96 lines
4.3 KiB
package com.tptj.demo.hg.simple.dao; |
|
|
|
import com.fr.decision.authority.entity.UserEntity; |
|
import com.fr.log.FineLoggerFactory; |
|
import com.fr.stable.bridge.ObjectHolder; |
|
import com.fr.stable.db.session.DAOSession; |
|
import com.tptj.tool.hg.simple.dao.SimpleDatabaseCaller; |
|
|
|
import java.util.ArrayList; |
|
import java.util.List; |
|
|
|
|
|
/** |
|
* @author 秃破天际 |
|
* @version 10.0 |
|
* Created by 秃破天际 on 2021/8/27 |
|
* 不用DBAccessorProvider接口直接在finedb中注入表,并提供简单的dao调用。 |
|
**/ |
|
public class DemoService { |
|
public static final String PLUGIN_ID = "com.tptj.demo.hg.simple.dao"; |
|
private static volatile DemoService instance = null; |
|
private DemoService(){ |
|
init(); |
|
} |
|
private void init(){ |
|
try{ |
|
//注意,只能运行一次,否则会报XXX表已存在的异常,所以一般放到单例对象里面初始化 |
|
SimpleDatabaseCaller.getInstance().register(DemoEntity.class); |
|
}catch(Exception e){ |
|
FineLoggerFactory.getLogger().error(e,e.getMessage()); |
|
} |
|
} |
|
|
|
public static DemoService getInstance() { |
|
if( null == instance){ |
|
synchronized (DemoService.class){ |
|
if( null == instance){ |
|
instance = new DemoService(); |
|
} |
|
} |
|
} |
|
return instance; |
|
} |
|
|
|
public List<DemoEntity> test(String key,String value){ |
|
try{ |
|
DemoEntity test = new DemoEntity(key,value); |
|
ObjectHolder holder = new ObjectHolder(); |
|
//把原本产品面向对象的结构,转换为函数式的结构了,虽然减少了代码量,但是对于能处理的场景就变少了。 |
|
//仅能操作插件注册的表。决策平台本身的表目前是操作不到的 |
|
//通过createPluginWork可以调用所有插件申明的dao |
|
//注:一次Work提交可以涉及多个表的操作。 |
|
SimpleDatabaseCaller.getInstance().createPluginWork()//第一行固定写法不用动 |
|
//把test中所有不是null的字段值 按照and eq的方式进行查找,返回匹配的数组,放到holder里面 |
|
//如果传了相应的key列表,则只对指定的key字段进行筛选 |
|
.simpleFind(test,holder,DemoEntity.COLUMN_KEY) |
|
.addAction((DAOSession session)->{ |
|
List<DemoEntity> list = holder.get(List.class); |
|
if(list.size() > 1){ |
|
//抛出异常,后续的增删改就不会执行了 |
|
//主要用于操作数据前先查询本地数据,确认是否有条件对数据做增删改 |
|
throw new Exception("xxx不唯一!"); |
|
} |
|
}) |
|
.add(test)//增删改都是可以批量的 |
|
//.update(test)//基于ID修改数据,也就是ID不能改 |
|
//.delete(test)//基于ID删除 |
|
.simpleFind(test,holder,DemoEntity.COLUMN_KEY) |
|
.addAction((DAOSession session)->{ |
|
//事务处理完了还想做点什么确认之类的 |
|
List<DemoEntity> list = holder.get(List.class); |
|
System.out.println(list.size()); |
|
}) |
|
.commit();//commit的时候才会真正把前面的操作按照申明的顺序执行。最后一行固定写法不用动 |
|
//holder里面的数据就是最近一次find的数据 |
|
return holder.get(List.class); |
|
}catch(Exception e){ |
|
FineLoggerFactory.getLogger().error(e,e.getMessage()); |
|
} |
|
return new ArrayList<DemoEntity>(); |
|
} |
|
|
|
public List<UserEntity> user(String username){ |
|
try{ |
|
ObjectHolder holder = new ObjectHolder(); |
|
//通过createDecisionWork可以调用除定时调度以外的所有产品自身的dao |
|
SimpleDatabaseCaller.getInstance().createDecisionWork() |
|
.simpleFind(new UserEntity().userName(username),holder,UserEntity.COLUMN_USER_NAME) |
|
.commit(); |
|
return holder.get(List.class); |
|
}catch(Exception e){ |
|
FineLoggerFactory.getLogger().error(e,e.getMessage()); |
|
} |
|
return new ArrayList<UserEntity>(); |
|
} |
|
}
|
|
|