SPL-在SpringBoot中的集成(三)

x33g5p2x  于2022-07-04 转载在 Spring  
字(10.0k)|赞(0)|评价(0)|浏览(1412)

需要的MAVEN依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.vaadin.external.google</groupId>
                    <artifactId>android-json</artifactId>
                </exclusion>
            </exclusions>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>com.scudata.esproc</groupId>
            <artifactId>esproc</artifactId>
            <version>20220601</version>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.2.8</version>
        </dependency>
        <dependency>
            <groupId>com.ibm.icu</groupId>
            <artifactId>icu4j</artifactId>
            <version>60.3</version>
        </dependency>

        <dependency>
            <groupId>org.lucee</groupId>
            <artifactId>jdom</artifactId>
            <version>1.1.3</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>5.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>5.1.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.6.RELEASE</version>
            </plugin>
        </plugins>
    </build>

SPL无配置文件版

这个版本不依赖raqsoftConfig.xml配置文件,数据库需要我们手动自己添加进去

application.yml

server:
  port: 9546

spring:
  datasource:
    ds1:
      driverclassName: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://192.168.118.129:3306/voidme?useCursorFetch=true&useSSL=false
      username: root
      password: root
      dbType: MYSQL
    ds2:
      driverclassName: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://192.168.118.129:3306/sso?useCursorFetch=true&useSSL=false
      username: root
      password: root
      dbType: MYSQL

JdbcConfig

@Configuration
public class JdbcConfig {
    @Bean("local_mysql_1")
    @ConfigurationProperties("spring.datasource.ds1")
    public DataSource datasourceMysql1() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean("local_mysql_2")
    @ConfigurationProperties("spring.datasource.ds2")
    public DataSource datasourceMysql2() {
        return DruidDataSourceBuilder.create().build();
    }

}

AppInitListener

@Component
public class AppInitListener extends ApplicationObjectSupport implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        Map<String, DataSource> beansOfType = getApplicationContext().getBeansOfType(DataSource.class);

        for (Map.Entry<String, DataSource> stringDataSourceEntry : beansOfType.entrySet()) {
            DruidDataSource value = (DruidDataSource)stringDataSourceEntry.getValue();
            int dbType = DBTypes.getDBType(value.getDbType());
            SpringDBSessionFactory.create(stringDataSourceEntry.getKey(), dbType);
        }
    }
}

SplAppliction

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, })
@ComponentScan({"com.scudata.*","com.splparer.*"})
public class SplAppliction {

    public static void main(String[] args) {
        SpringApplication.run(SplAppliction.class, args);
    }
}

SplTest

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SplAppliction.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class SplTest {
    @SneakyThrows
    @Test
    public void contextLoads() {
//        FileInputStream fileInputStream=new FileInputStream(new File("C:\\Users\\huanmin\\Desktop\\spl\\p.spl"));
//        PgmCellSet pgmCellSet = AppUtil.readSPL(fileInputStream);  //文本文件
//        PgmCellSet pgmCellSet = AppUtil.readSPL("C:\\Users\\huanmin\\Desktop\\spl\\p.spl");  //文本文件
        PgmCellSet pgmCellSet = AppUtil.readCellSet("C:\\Users\\huanmin\\Desktop\\spl\\p.dfx");  //dfx, sqlx 二进制文件
        Context context = new Context(); //上下文,参数..设置
        pgmCellSet.setContext(context);
        Object execute = pgmCellSet.execute();
        System.out.println(execute);
    }

}

p.splx

结果如下:

SPL配置文件版

这个版本支持的功能比较全,直接调用SPL驱动,需要搭配raqsoftConfig.xml配置文件进行

raqsoftConfig.xml 文件可在本地windows客户端中-选项-环境里自行配置配置好后,就会将内容同步到本地安装目录下esProc\config\raqsoftConfig.xml里 更全配置信息

<?xml version="1.0" encoding="UTF-8"?>
<Config Version="3">
    <Runtime>
        <DBList>
            <DB name="local_mysql_1">
                <property name="url"
                          value="jdbc:mysql://192.168.118.129:3306/voidme?useCursorFetch=true&amp;useSSL=false"/>
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="type" value="10"/>
                <property name="user" value="root"/>
                <property name="password" value="root"/>
                <property name="batchSize" value="0"/>
                <property name="autoConnect" value="true"/>
                <property name="useSchema" value="false"/>
                <property name="addTilde" value="false"/>
                <property name="needTransContent" value="false"/>
                <property name="needTransSentence" value="false"/>
                <property name="caseSentence" value="false"/>
            </DB>
        </DBList>
        <Esproc>
            <charSet>UTF-8</charSet>

<!--            配置spl脚本文件寻址路径,可以设置多个路径,以“;”隔开。 spl文件的路径也可以放在应用项目的类路径中,加载文件的顺序高于寻址路径-->
            <splPathList>
                <splPath>C:\Users\huanmin\Desktop\spl</splPath>
            </splPathList>
            <dateFormat>yyyy-MM-dd</dateFormat>
            <timeFormat>HH:mm:ss</timeFormat>
            <dateTimeFormat>yyyy-MM-dd HH:mm:ss</dateTimeFormat>
            <mainPath/>
            <!-- 临时文件(dfx,txt...)存储路径,可以使用绝对路径,当设置路径为相对路径时,将设置在主路径下,相对路径不能以“/”或“\”开头-->
            <tempPath>resources/temp</tempPath>
            <bufSize>65536</bufSize>
            <localHost/>
            <localPort>0</localPort>
            <parallelNum/>
            <cursorParallelNum/>
            <simpleTableBlockSize>1048576</simpleTableBlockSize>
            <nullStrings>nan,null,n/a</nullStrings>
            <fetchCount/>
            <extLibsPath/>
        </Esproc>
        <Logger>
            <!--            <Level>DEBUG</Level>-->
            <Level>INFO</Level>
        </Logger>
    </Runtime>
    <JDBC>

<!--   在JDBC中配置了远程服务器之后,再用Java代码调用JDBC时,执行计算时会优先在本地处理,如果本地无法执行,如找不到数据文件等,才会在远程服务器端执行计算。如果希望将计算交给服务器执行,可以在连接串中添加参数?onlyServer=true 
详细说明:    http://d.raqsoft.com.cn:6999/esproc/tutorial/jdbcdyycfwq.html    
 -->
        <!--        <Units>-->
        <!--            <Unit>106.12.174.220:8281</Unit>-->
        <!--            <Unit>127.0.0.1:8281</Unit>-->
        <!--        </Units>-->

        <!-- 初始化使用的dfx文件,在系统初始化时先执行指定文件-->
        <Init>
            <!--            <SPL>C:\Users\huanmin\Desktop\spl\init.dfx</SPL>-->
        </Init>
    </JDBC>
</Config>

application.yml

server:
  port: 9546

spring:
  datasource:
      driverclassName: com.esproc.jdbc.InternalDriver
      url: jdbc:esproc:local://
      username: root
      password: root
      minIdle: 5
      maxActive: 100
      initialSize: 10

JdbcConfig

@Configuration
public class JdbcConfig {

    @Bean
    @ConfigurationProperties("spring.datasource")
    public DataSource datasource() {
        return DruidDataSourceBuilder.create().build();
    }

}

SplController

@RestController
@RequestMapping("/spl")
public class SplController {

    @Autowired
    private SplServiceImpl service;
    @GetMapping("")
    public void execute() {
        service.contextLoads();
    }
}

SplService

public interface    SplService {
    public void contextLoads() ;

}

SplServiceImpl

@Component
public class SplServiceImpl implements SplService {
    @Autowired
    private DataSource dataSource;

    @SneakyThrows
    @Override
    public void contextLoads() {
        long l = System.currentTimeMillis();
        //获得数据库连接
        Connection connection = dataSource.getConnection();
        CallableStatement st = connection.prepareCall("call p()");
        //执行存储过程
        st.execute();
        //获取结果集
        ResultSet rs = st.getResultSet();
        SplUtil.show(rs);
        connection.close();
        long l1 = System.currentTimeMillis();
        System.out.println("======================:"+String.valueOf(l1-l));
    }
}

SplUtil

public class SplUtil {

    //显示表格值内容
    public static void show(  ResultSet rs) throws SQLException {
        //简单处理结果集,将结果集中的字段名与数据输出
        ResultSetMetaData rsmd = rs.getMetaData();
        int colCount = rsmd.getColumnCount();
        for ( int  c = 1; c <= colCount;c++) {
            String title = rsmd.getColumnName(c);
            if( c > 1 ) {
                System.out.print("\t");
            }
            else {
                System.out.print("\n");
            }
            System.out.print(title);
        }

        while (rs.next()) {
            for(int c = 1; c<= colCount; c++) {
                if ( c > 1 ) {
                    System.out.print("\t");
                }
                else {
                    System.out.print("\n");
                }
                Object o = rs.getObject(c);
                System.out.print(o);
            }
        }

        System.out.println();
    }
}

SplAppliction

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, })
@ComponentScan({"com.scudata.*","com.spl.*"})
public class SplAppliction {

    public static void main(String[] args) {
        SpringApplication.run(SplAppliction.class, args);
    }
}

注意

  1. java调用SPL,网格中的代码都会被执行一遍, 但是return只会获取第一个
  2. 在spl中通过connect进行使用数据库连接,使用完毕后就要关闭,否则就会一直占用连接 ,当然还可以connect@x 方式来调用
  3. java 调用SPL第一次会加载类库等, 稍微有点慢大概需要300毫秒, 之后就快了也就几毫秒

点赞 -收藏-关注-便于以后复习和收到最新内容有其他问题在评论区讨论-或者私信我-收到会在第一时间回复感谢,配合,希望我的努力对你有帮助^_^

免责声明:本文部分素材来源于网络,版权归原创者所有,如存在文章/图片/音视频等使用不当的情况,请随时私信联系我。

相关文章