springboot+postgresql集成anyline试水

anyline是什么

简单讲就是一个工具可以让你抛开常规的机械性建mapper、dao、sql,用通用的语句查询和操作数据库表。目前也在初步探索中,感受还不深。
官网文档:http://doc.anyline.org/
gitee:https://gitee.com/anyline/anyline-simple/tree/master

环境

在已有springboot框架基础上集成anyline,数据库是postgresql。
ps. 下面是踩坑过程,可以跳到集成方法章节直接看如何集成及简单使用。

踩坑过程

添加依赖

        <dependency>
            <groupId>org.anyboot</groupId>
            <artifactId>anyboot-mvc</artifactId>
            <version>8.3.8.206</version>
        </dependency>

测试controller

在自己的controller里继承AnylineController(AnylineController中已导入了AnylineService):

public class TableController extends AnylineController {
}

结果启动报错:
java.io.FileNotFoundException: class path resource [org/anyline/controller/impl/AnylineController.class] cannot be opened because it does not exist
或者
Unexpected exception during bean creation; nested exception is java.lang.UnsupportedOperationException
解决:折腾很多方法,忘记怎么解决了,大概是各种clean和重启。

新建查询方法测试

    public Response<DataSet> querys(String src, String ... conditions) {
        return Response.or(service.querys("test"));
    }

报错如下:

经过一步一步debug发现是SQLCreaterUtil的getCreater方法返回null。为什么呢,因为源码有一步是判断数据源类型:

				SQLCreater.DB_TYPE type = SQLCreater.DB_TYPE.MYSQL;
				if(name.contains("mysql")){
					type = SQLCreater.DB_TYPE.MYSQL;
				}else if(name.contains("mssql") || name.contains("sqlserver")){
					type = SQLCreater.DB_TYPE.MSSQL;
				}else if(name.contains("oracle")){
					type = SQLCreater.DB_TYPE.ORACLE;
				}else if(name.contains("db2")){
					type = SQLCreater.DB_TYPE.DB2;
				}else if(name.contains("hgdb") || name.contains("highgo")){
					type = SQLCreater.DB_TYPE.HighGo;
				}else if(name.contains("postgresql")){
					type = SQLCreater.DB_TYPE.PostgreSQL;
				}

我使用的driver是postgresql,但数据库名是highgo,导致把数据源类型判断成了HighGo。
于是我把数据库名改了,这回type终于判断正确。但是源码中creaters依然为空,后续依旧报空指针的错误。

此路不通,换条路

把思路调回开头。官网给的demo基本都是以mysql为例的,我使用pg,也应该找对应的demo作参考。
于是找到了gitee中关于pg的引用demo:https://gitee.com/anyline/anyline-simple/tree/master/anyline-simple-data-jdbc-dialect/anyline-simple-data-jdbc-postgresql
(如果你使用的是其他数据库,可以从 https://gitee.com/anyline/anyline-simple/tree/master/anyline-simple-data-jdbc-dialect 找到对应的示例)
把依赖换成了:

        <dependency>
            <groupId>org.anyline</groupId>
            <artifactId>anyline-data-jdbc-postgresql</artifactId>
            <version>8.6.1-SNAPSHOT</version>
        </dependency>

接着发现该包是没有AnylineController的,但是有AnylineService,在自己建的controller里引入就可以了。

    @Resource
    private AnylineService anylineService;

具体提供的查询、操作方法,可以看AnylineService源码,也可以对照官网给出的例子使用。

关于多源数据切换

我的工程使用了两个数据源,一个业务数据库一个系统数据库,原本参考两种方式切换数据源测试,结果加了<admin>或者<master>都提示数据源未注册。
参考此处:http://doc.anyline.org/a?id=p298pn6e9o1r5gv78ac1evic624c62387ffafcdadc1be1a411e5ca21d69177be00

集成方法(springboot+postgresql)

  1. 在pom.xml中添加依赖(注意:此处是在你原有项目可以正常使用,默认该配的依赖已经配上的前提下):
    <dependencies>
        <dependency>
            <groupId>org.anyline</groupId>
            <artifactId>anyline-data-jdbc-postgresql</artifactId>
            <version>8.6.1-SNAPSHOT</version>
        </dependency>
        <!-- 如果需要多源数据切换,还需要添加anyboot-data-jdbc依赖 -->
        <dependency>
            <groupId>org.anyboot</groupId>
            <artifactId>anyboot-data-jdbc</artifactId>
            <version>8.6.1.2612-20221008</version>
        </dependency>
    </dependencies>
  1. 上一步添加的依赖可能无法下载,需要添加aliyun和anyline仓库(如果原本项目中已经有阿里云仓库,就不需要重复添加第一个,只需添加anyline):
    <repositories>
        <repository>
            <id>aliyun</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        </repository>
        <repository>
            <id>anyline</id>
            <url>http://maven.anyline.org/repository/maven-snapshots/</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
  1. 数据源配置
    如果原本项目中已经配置了数据源,该步骤不需要,原本框架配置的数据源即可使用。
    否则(比如新建项目)参考此处:https://gitee.com/anyline/anyline-simple/blob/master/anyline-simple-data-jdbc-dialect/anyline-simple-data-jdbc-postgresql/src/main/resources/application.properties
  2. 修改启动类
    在启动类添加注解,扫描anyline。
@ComponentScan(value = {"org.anyline"})

如果需要多源数据切换,注解则需要额外扫描anyboot,以及引入DynamicDataSourceRegister.class

@ComponentScan(value = {"org.anyline", "org.anyboot" })
@Import(DynamicDataSourceRegister.class)
  1. 新建一个controller,或使用已有的controller,引入AnylineService:
@RestController
@RequestMapping("/xxx")
public class TableController {
    @Resource
    private AnylineService anylineService;
}
  1. 调用AnylineService实现简单查询:
anylineService.querys(src, conditions);

src可以是表名(如:"test"),可以是模式.表名(如:"public.test"),可以是表名+查询的列(如:"test(name, description)")。
condition是固定查询条件,如:"ID:>1"。
可以实现复杂的查询以及连表查询。

  1. 调用AnylineService实现简单新增/修改:
anylineService.save(tableName, data);

tableName为要操作的表名。
参数data接收前端传来的数据时可以写成@RequestBody DataRow data,DataRow类可以简单看成key-value形式的map,前端传json对象即可。
关于id:save方法默认传id为修改,不传id为新增,故建议数据库设为id自增,否则会出现试图新增一条记录时,不传id报id为空,而传id又因为识别为修改、无此id的记录而影响行数为0的情况。

可能有用的链接

官网文档:http://doc.anyline.org/
gitee:https://gitee.com/anyline/anyline-simple/tree/master
AnylineService:http://doc.anyline.org/s?id=p298pn6e9o1r5gv78acvic1e624c62387f2c45dd13bb112b342fda2aa3657afa61
查询例子:http://doc.anyline.org/s?id=p298pn6e9o1r5gv78acvic1e624c62387f2c45dd13bb112b3433b3ed7b7b64f9a9
条件约定格式:http://doc.anyline.org/s?id=p298pn6e9o1r5gv78acvic1e624c62387f2c45dd13bb112b34176fad5a868fa6a4

posted @ 2022-12-19 12:15  宇宙野牛  阅读(789)  评论(3编辑  收藏  举报