9 changed files with 363 additions and 1 deletions
@ -1,3 +1,16 @@ |
|||||||
# demo-simple-dao |
# demo-simple-dao |
||||||
|
|
||||||
简单DAO调用方法示例 |
简单DAO调用方法示例\ |
||||||
|
需要依赖 tool-simple-dao-1.0.jar\ |
||||||
|
调用方法:\ |
||||||
|
1.在finedb中注册新的表实体\ |
||||||
|
SimpleDatabaseCaller.getInstance().register(Class<? extends BaseEntity>... entities);\ |
||||||
|
注:此方法每个插件每次启动只能调用一次,调用多次会出现 xxx表已存在的异常!所以推荐放到任意单例对象初始化中调用\ |
||||||
|
|
||||||
|
2.调用任意插件申明的表实体\ |
||||||
|
SimpleDatabaseCaller.getInstance().createPluginWork().add/update/delete/find/simpleFind/count/simpleCount.commit();\ |
||||||
|
|
||||||
|
3.调用产品自身的表实体(注,当前只能调用定时调度以外的其他表实体)\ |
||||||
|
SimpleDatabaseCaller.getInstance().createDecisionWork().add/update/delete/find/simpleFind/count/simpleCount.commit();\ |
||||||
|
|
||||||
|
具体示例可以看demo中的DemoService即可 |
@ -0,0 +1,124 @@ |
|||||||
|
|
||||||
|
apply plugin: 'java' |
||||||
|
|
||||||
|
[compileJava,compileTestJava]*.options*.encoding = 'UTF-8' |
||||||
|
|
||||||
|
ext { |
||||||
|
/** |
||||||
|
* 项目中依赖的jar的路径 |
||||||
|
* 1.如果依赖的jar需要打包到zip中,放置在lib根目录下 |
||||||
|
* 2.如果依赖的jar仅仅是编译时需要,防止在lib下子目录下即可 |
||||||
|
*/ |
||||||
|
libPath = "$projectDir/../../webroot/WEB-INF/lib" |
||||||
|
|
||||||
|
/** |
||||||
|
* 是否对插件的class进行加密保护,防止反编译 |
||||||
|
*/ |
||||||
|
guard = false |
||||||
|
|
||||||
|
def pluginInfo = getPluginInfo() |
||||||
|
pluginPre = "fine-plugin" |
||||||
|
pluginName = pluginInfo.id |
||||||
|
pluginVersion = pluginInfo.version |
||||||
|
|
||||||
|
outputPath = "$projectDir/../../webroot/WEB-INF/plugins/plugin-" + pluginName + "-1.0/classes" |
||||||
|
} |
||||||
|
|
||||||
|
group = 'com.fr.plugin' |
||||||
|
version = '10.0' |
||||||
|
sourceCompatibility = '8' |
||||||
|
|
||||||
|
sourceSets { |
||||||
|
main { |
||||||
|
java.outputDir = file(outputPath) |
||||||
|
output.resourcesDir = file(outputPath) |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
ant.importBuild("encrypt.xml") |
||||||
|
//定义ant变量 |
||||||
|
ant.projectDir = projectDir |
||||||
|
ant.references["compile.classpath"] = ant.path { |
||||||
|
fileset(dir: libPath, includes: '**/*.jar') |
||||||
|
fileset(dir: ".",includes:"**/*.jar" ) |
||||||
|
} |
||||||
|
|
||||||
|
classes.dependsOn('clean') |
||||||
|
|
||||||
|
task copyFiles(type: Copy,dependsOn: 'classes'){ |
||||||
|
from outputPath |
||||||
|
into "$projectDir/classes" |
||||||
|
} |
||||||
|
|
||||||
|
task preJar(type:Copy,dependsOn: guard ? 'compile_encrypt_javas' : 'compile_plain_javas'){ |
||||||
|
from "$projectDir/classes" |
||||||
|
into "$projectDir/transform-classes" |
||||||
|
include "**/*.*" |
||||||
|
} |
||||||
|
jar.dependsOn("preJar") |
||||||
|
|
||||||
|
task makeJar(type: Jar,dependsOn: preJar){ |
||||||
|
from fileTree(dir: "$projectDir/transform-classes") |
||||||
|
baseName pluginPre |
||||||
|
appendix pluginName |
||||||
|
version pluginVersion |
||||||
|
destinationDir = file("$buildDir/libs") |
||||||
|
|
||||||
|
doLast(){ |
||||||
|
delete file("$projectDir/classes") |
||||||
|
delete file("$projectDir/transform-classes") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
task copyFile(type: Copy,dependsOn: ["makeJar"]){ |
||||||
|
from "$buildDir/libs" |
||||||
|
from("$projectDir/lib") { |
||||||
|
include "*.jar" |
||||||
|
} |
||||||
|
from "$projectDir/plugin.xml" |
||||||
|
into file("$buildDir/temp/plugin") |
||||||
|
} |
||||||
|
|
||||||
|
task zip(type:Zip,dependsOn:["copyFile"]){ |
||||||
|
from "$buildDir/temp/plugin" |
||||||
|
destinationDir file("$buildDir/install") |
||||||
|
baseName pluginPre |
||||||
|
appendix pluginName |
||||||
|
version pluginVersion |
||||||
|
} |
||||||
|
|
||||||
|
//控制build时包含哪些文件,排除哪些文件 |
||||||
|
processResources { |
||||||
|
// exclude everything |
||||||
|
// 用*.css没效果 |
||||||
|
// exclude '**/*.css' |
||||||
|
// except this file |
||||||
|
// include 'xx.xml' |
||||||
|
} |
||||||
|
|
||||||
|
/*读取plugin.xml中的version*/ |
||||||
|
def getPluginInfo(){ |
||||||
|
def xmlFile = file("plugin.xml") |
||||||
|
if (!xmlFile.exists()) { |
||||||
|
return ["id":"none", "version":"1.0.0"] |
||||||
|
} |
||||||
|
def plugin = new XmlParser().parse(xmlFile) |
||||||
|
def version = plugin.version[0].text() |
||||||
|
def id = plugin.id[0].text() |
||||||
|
return ["id":id,"version":version] |
||||||
|
} |
||||||
|
|
||||||
|
repositories { |
||||||
|
mavenLocal() |
||||||
|
maven { |
||||||
|
url = uri('http://mvn.finedevelop.com/repository/maven-public/') |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
dependencies { |
||||||
|
//使用本地jar |
||||||
|
implementation fileTree(dir: 'lib', include: ['**/*.jar']) |
||||||
|
implementation fileTree(dir: libPath, include: ['**/*.jar']) |
||||||
|
} |
||||||
|
|
||||||
|
|
@ -0,0 +1,13 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?> |
||||||
|
<project> |
||||||
|
<target name="compile_encrypt_javas" depends="copyFiles"> |
||||||
|
<echo message="加密文件"/> |
||||||
|
<echo message="${projectDir}"/> |
||||||
|
<taskdef name="pretreatment" classname="com.fr.plugin.pack.PluginPretreatmentTask"> |
||||||
|
<classpath refid="compile.classpath"/> |
||||||
|
</taskdef> |
||||||
|
<pretreatment baseDir="${projectDir}"/> |
||||||
|
</target> |
||||||
|
<target name="compile_plain_javas" depends="copyFiles"> |
||||||
|
</target> |
||||||
|
</project> |
Binary file not shown.
@ -0,0 +1,16 @@ |
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?><plugin> |
||||||
|
<id>com.tptj.demo.hg.simple.dao</id> |
||||||
|
<name><![CDATA[ SIMPLE DAO ]]></name> |
||||||
|
<active>yes</active> |
||||||
|
<version>1.0</version> |
||||||
|
<env-version>10.0</env-version> |
||||||
|
<jartime>2020-01-01</jartime> |
||||||
|
<vendor>tptj</vendor> |
||||||
|
<description/> |
||||||
|
<change-notes/> |
||||||
|
<main-package>com.tptj.demo.hg.simple.dao</main-package> |
||||||
|
<function-recorder class="com.tptj.demo.hg.simple.dao.DemoServer"/> |
||||||
|
<extra-decision> |
||||||
|
<ControllerRegisterProvider class="com.tptj.demo.hg.simple.dao.DemoServer"/> |
||||||
|
</extra-decision> |
||||||
|
</plugin> |
@ -0,0 +1,53 @@ |
|||||||
|
package com.tptj.demo.hg.simple.dao; |
||||||
|
|
||||||
|
import com.fr.decision.base.util.UUIDUtil; |
||||||
|
import com.fr.stable.db.entity.BaseEntity; |
||||||
|
import com.fr.third.javax.persistence.Column; |
||||||
|
import com.fr.third.javax.persistence.Entity; |
||||||
|
import com.fr.third.javax.persistence.Table; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author 秃破天际 |
||||||
|
* @version 10.0 |
||||||
|
* Created by 秃破天际 on 2021/8/27 |
||||||
|
**/ |
||||||
|
@Entity |
||||||
|
@Table(name = "fr_demo_simple") |
||||||
|
public class DemoEntity extends BaseEntity { |
||||||
|
|
||||||
|
public static final String COLUMN_KEY = "key"; |
||||||
|
public static final String COLUMN_VALUE = "value"; |
||||||
|
|
||||||
|
@Column(name = COLUMN_KEY,nullable = false) |
||||||
|
private String key; |
||||||
|
|
||||||
|
@Column(name = COLUMN_VALUE,nullable = false) |
||||||
|
private String value; |
||||||
|
|
||||||
|
public DemoEntity() { |
||||||
|
} |
||||||
|
|
||||||
|
public DemoEntity(String key, String value) { |
||||||
|
setId( UUIDUtil.generate() ); |
||||||
|
this.key = key; |
||||||
|
this.value = value; |
||||||
|
} |
||||||
|
|
||||||
|
public String getKey() { |
||||||
|
return key; |
||||||
|
} |
||||||
|
|
||||||
|
public void setKey(String key) { |
||||||
|
this.key = key; |
||||||
|
} |
||||||
|
|
||||||
|
public String getValue() { |
||||||
|
return value; |
||||||
|
} |
||||||
|
|
||||||
|
public void setValue(String value) { |
||||||
|
this.value = value; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,26 @@ |
|||||||
|
package com.tptj.demo.hg.simple.dao; |
||||||
|
|
||||||
|
import com.fr.decision.webservice.Response; |
||||||
|
import com.fr.decision.webservice.annotation.LoginStatusChecker; |
||||||
|
import com.fr.third.springframework.stereotype.Controller; |
||||||
|
import com.fr.third.springframework.web.bind.annotation.RequestMapping; |
||||||
|
import com.fr.third.springframework.web.bind.annotation.RequestMethod; |
||||||
|
import com.fr.third.springframework.web.bind.annotation.RequestParam; |
||||||
|
import com.fr.third.springframework.web.bind.annotation.ResponseBody; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author 秃破天际 |
||||||
|
* @version 10.0 |
||||||
|
* Created by 秃破天际 on 2021/8/27 |
||||||
|
**/ |
||||||
|
@Controller(DemoService.PLUGIN_ID) |
||||||
|
@RequestMapping(value = "/tptj/simple/dao") |
||||||
|
@LoginStatusChecker(required = false) |
||||||
|
public class DemoResource{ |
||||||
|
@RequestMapping(value = "/test",method = RequestMethod.GET) |
||||||
|
@ResponseBody |
||||||
|
public Response test( @RequestParam(value="username") String username )throws Exception{ |
||||||
|
Response.ok( DemoService.getInstance().test("a","b") ); |
||||||
|
return Response.ok(DemoService.getInstance().user(username)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,21 @@ |
|||||||
|
package com.tptj.demo.hg.simple.dao; |
||||||
|
|
||||||
|
import com.fr.decision.fun.impl.AbstractControllerRegisterProvider; |
||||||
|
import com.fr.intelli.record.Focus; |
||||||
|
import com.fr.record.analyzer.EnableMetrics; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author 秃破天际 |
||||||
|
* @version 10.0 |
||||||
|
* Created by 秃破天际 on 2021/8/27 |
||||||
|
**/ |
||||||
|
@EnableMetrics |
||||||
|
public class DemoServer extends AbstractControllerRegisterProvider { |
||||||
|
@Override |
||||||
|
@Focus(id = "com.tptj.demo.hg.simple.dao", text = "SIMPLE DAO") |
||||||
|
public Class<?>[] getControllers() { |
||||||
|
return new Class[]{ |
||||||
|
DemoResource.class |
||||||
|
}; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,96 @@ |
|||||||
|
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>(); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue