首页 > 数据库 >springboot+postgresql集成anyline试水

springboot+postgresql集成anyline试水

时间:2022-12-19 12:22:59浏览次数:60  
标签:postgresql springboot simple 试水 anyline org data id

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>都提示数据源未注册。
然后发现,两个数据库的查询不需要添加前缀,直接查询就都可以查到。

集成方法(springboot+postgresql)

  1. 在pom.xml中添加依赖(注意:此处是在你原有项目可以正常使用,默认该配的依赖已经配上的前提下):
    <dependencies>
        <dependency>
            <groupId>org.anyline</groupId>
            <artifactId>anyline-data-jdbc-postgresql</artifactId>
            <version>8.6.1-SNAPSHOT</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. 新建一个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

标签:postgresql,springboot,simple,试水,anyline,org,data,id
From: https://www.cnblogs.com/cosmicbison/p/16989355.html

相关文章

  • 5. MinIO与springboot的集成
    MinIO与springboot的集成搭建一个springboot的项目,集成MinIO实现文件的管理。一、搭建springboot环境IntelliJIDEAJDK17gradle-7.5.1springboot2.7.6项目地址:g......
  • springboot实现AOP切面编程
    概述AOP(AspectOrientedProgramming)即面向切面编程。面向切面是面向对象中的一种方式而已。在代码执行过程中,动态嵌入其他代码,叫做面向切面编程(将交叉业务逻辑封装成成......
  • 亲手实现一个springboot默认配置&起步加载
    实现一、默认配置1、创建springboot项目引入spring-boot-dependencies依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-......
  • 【java】(二) SpringBoot 源码解析——run启动加载过程——准备环境
    1.前言深入学习springboot笔记系列,可能会有错误还请指正,互相勉励,互相学习。上一章讲了SpringApplicaiton是如何初始化的,本章讲解后续的run方法的启动过程。本章涉及......
  • SpringBoot3.x中spring.factories功能被移除的解决方案
    背景笔者所在项目组在搭建一个全新项目的时候选用了​​SpringBoot3.x​​​,项目中应用了很多​​SpringBoot2.x​​​时代相关的第三方组件例如​​baomidou​​​出品的​......
  • springboot项目打包报错:Type org.springframework.boot.maven.RepackageMojo not pres
    折磨了好久的一个问题,换过spring-boot-maven-plugin版本,但不见效。今天参考这篇文章:springboot打包RepackageMojonotpresent,版本改成2.6.2,不行。就想,可能是我用的idea......
  • SpringBoot知识点
    自动装配原理SpringBoot特点优点:(1)创建独立Spring应用(2)内嵌web服务器(3)自动start依赖,简化构建配置(4)自动配置Spring以及第三方功能(5)提供生产级别的监控、健康检测以及......
  • Springboot使用CrosXssFilter防止sql注入xss攻击cros跨域等
    现在的web系统对安全性要求越来越高,常常需要通过第三方的渗透测试才能进行验收,其中就有关于sql注入、xss攻击相关的,此文记录如果在springbooot中进行非侵入的改造,达到能通......
  • SpringBoot 阅读源码之RandomValuePropertySource 是如何工作的
    最近在极客时间上面学习丁雪丰老师的《玩转Spring全家桶》,看到一个在application.properties里面生成随机数的例子,觉得很厉害,带着崇拜的心情去阅读了一下Spring的源码,总......
  • SpringBoot Actuator监控
    Actuator帮助我们监控和管理SpringBoot应用。1.集成Actuator1.1build.gradleimplementationgroup:'org.springframework.boot',name:'spring-boot-starter-actu......