MyBatis查询Apache Druid

391 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 1 天,点击查看活动详情

每日英语:

Morning comes whether you set the alarm or not.

不管你定不定闹钟,早晨都会到来。 -厄休拉·克鲁伯·勒奎恩

前序

我们在java程序中可以通过JDBC查询ApacheDruid,但是需要引入avatica,Apache Calcite 是一个动态数据管理框架,用于 Hadoop 平台,1.10.0 增加了对 JDBC Array data, Docker, 和 JDK 9 的支持(同时支持JDK7、JDK8)。

druid.apache.org/docs/0.20.0…

Example code:

// Connect to /druid/v2/sql/avatica/ on your Broker. 链接地址
String url = "jdbc:avatica:remote:url=http://localhost:8082/druid/v2/sql/avatica/";

// Set any connection context parameters you need here (see "Connection context" below).
// Or leave empty for default behavior.
Properties connectionProperties = new Properties();

//创建链接
try (Connection connection = DriverManager.getConnection(url, connectionProperties)) {
  try (
      //获取Statement对象
      final Statement statement = connection.createStatement();
      //执行sql
      final ResultSet resultSet = statement.executeQuery(query)
  ) {
    while (resultSet.next()) {
      // process result set 解析结果集
    }
  }
}

我们项目中使用的是MyBatisPlus,我们需要做的是把JDBC操作Druid改成MyBatis操作ApacheDruid。关于JDBC的操作,我们就不在这里做过多讲解。

我们单独搭建一个工程mall-dw-service实现MyBatis集成ApacheDruid,所有Druid查询都用该工程操作。

1. API工程搭建

1)API

创建mall-dw-api,并创建查询Druid对应的JavaBean对象com.xz.mall.dw.model.HotGoods

@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@TableName(value = "msitemslog")
public class HotGoods {
    //IP
    private String ip;
    //访问的uri
    private String uri;
    //时间.
    @TableField("__time")
    private Date accesstime;
}

2. Service工程搭建

搭建操作ApacheDruid的工程mall-dw-service,并完善该工程。

1)pom.xml

<dependencies>
    <!--API-->
    <dependency>
        <groupId>com.xz</groupId>
        <artifactId>mall-dw-api</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
​
    <!--操作Apache Druid-->
    <dependency>
        <groupId>org.apache.calcite.avatica</groupId>
        <artifactId>avatica</artifactId>
        <version>1.17.0</version>
    </dependency>
​
    <dependency>
        <groupId>org.apache.calcite.avatica</groupId>
        <artifactId>avatica-core</artifactId>
        <version>1.17.0</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.2.4</version>
    </dependency>
</dependencies>

2)bootstrap.yml

server:
  port: 8093
spring:
  application:
    name: mall-dw
  datasource:
    driver-class-name: org.apache.calcite.avatica.remote.Driver
    url: jdbc:avatica:remote:url=http://192.168.xxx.xxx:8082/druid/v2/sql/avatica/
    type: com.alibaba.druid.pool.DruidDataSource
  cloud:
    nacos:
      config:
        file-extension: yaml
        server-addr: 192.168.xxx.xxx:8848
      discovery:
        #Nacos的注册地址
        server-addr: 192.168.xxx.xxx:8848
# ====================MybatisPlus====================
mybatis:
  mapper-locations: mapper/*.xml
  type-aliases-package: com.xz.mall.*.model
  configuration:
    map-underscore-to-camel-case: true
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

#日志配置
logging:
  pattern:
    console: "%msg%n"

此时需要注意,数据库连接地址和驱动和之前是存在差异,使用的地址是Apache druid地址,而驱动用的则是avatica驱动。

3)Dao

创建com.xz.mall.dw.mapper.HotGoodsMapper

public interface HotGoodsMapper extends BaseMapper<HotGoods> {}

4)Service

接口:创建com.xz.mall.dw.service.HotGoodsService

public interface HotGoodsService extends IService<HotGoods> {}

实现类:创建com.xz.mall.dw.service.impl.HotGoodsServiceImpl

@Service
public class HotGoodsServiceImpl extends ServiceImpl<HotGoodsMapper,HotGoods> implements HotGoodsService {}

5)Controller

创建com.xz.mall.dw.controller.HotGoodsController

@RestController
@RequestMapping(value = "/hot/goods")
public class HotGoodsController {}

6)启动类

创建启动类com.xz.mall.dw.DwApplication,代码如下:

@SpringBootApplication
@MapperScan(basePackages = "com.xz.mall.dw.mapper")
public class DwApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(DwApplication.class,args);
    }
}

总结

本篇主要介绍了MyBatisPlus查询的Apache Druid的具体依赖和yml配置,还有demo项目的搭建,下一篇主要实战一下具体的操作。