diff --git a/README.md b/README.md index e1bd9f6..076c56d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,16 @@ # demo-simple-dao -简单DAO调用方法示例 \ No newline at end of file +简单DAO调用方法示例\ +需要依赖 tool-simple-dao-1.0.jar\ +调用方法:\ +1.在finedb中注册新的表实体\ +SimpleDatabaseCaller.getInstance().register(Class... 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即可 \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..5231b5d --- /dev/null +++ b/build.gradle @@ -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']) +} + + diff --git a/encrypt.xml b/encrypt.xml new file mode 100644 index 0000000..1401cd1 --- /dev/null +++ b/encrypt.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/lib/tool-simple-dao-1.0.jar b/lib/tool-simple-dao-1.0.jar new file mode 100644 index 0000000..918e1f5 Binary files /dev/null and b/lib/tool-simple-dao-1.0.jar differ diff --git a/plugin.xml b/plugin.xml new file mode 100644 index 0000000..99a0a9c --- /dev/null +++ b/plugin.xml @@ -0,0 +1,16 @@ + + com.tptj.demo.hg.simple.dao + + yes + 1.0 + 10.0 + 2020-01-01 + tptj + + + com.tptj.demo.hg.simple.dao + + + + + \ No newline at end of file diff --git a/src/main/java/com/tptj/demo/hg/simple/dao/DemoEntity.java b/src/main/java/com/tptj/demo/hg/simple/dao/DemoEntity.java new file mode 100644 index 0000000..cbe881f --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/simple/dao/DemoEntity.java @@ -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; + } + + +} diff --git a/src/main/java/com/tptj/demo/hg/simple/dao/DemoResource.java b/src/main/java/com/tptj/demo/hg/simple/dao/DemoResource.java new file mode 100644 index 0000000..1856544 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/simple/dao/DemoResource.java @@ -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)); + } +} \ No newline at end of file diff --git a/src/main/java/com/tptj/demo/hg/simple/dao/DemoServer.java b/src/main/java/com/tptj/demo/hg/simple/dao/DemoServer.java new file mode 100644 index 0000000..89bf4f7 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/simple/dao/DemoServer.java @@ -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 + }; + } +} diff --git a/src/main/java/com/tptj/demo/hg/simple/dao/DemoService.java b/src/main/java/com/tptj/demo/hg/simple/dao/DemoService.java new file mode 100644 index 0000000..97cf219 --- /dev/null +++ b/src/main/java/com/tptj/demo/hg/simple/dao/DemoService.java @@ -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 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 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 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(); + } + + public List 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(); + } +}