首先自己創建spring boot工程,做好基本引用;下面只介紹滿足presto的引用
引用pom文件:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-jdbc</artifactId>
<version>0.203</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<!-- <version>5.1.1.RELEASE</version> -->
</dependency>
<!-- 處理No supported DataSource type found錯誤 -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
application.properties文件:
#
server.port=8000
spring.datasource.presto.name=presto
spring.datasource.presto.type=org.apache.tomcat.jdbc.pool.DataSource
spring.datasource.presto.driver-class-name=com.facebook.presto.jdbc.PrestoDriver
spring.datasource.presto.jdbc-url=jdbc:presto://192.168.227.3:8001/system/runtime //url改成jdbc-url,不然可能會報jdbcUrl is required with driverClassName
spring.datasource.presto.username=root
數據源配置類:
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
/**
* <p>創建數據源配置類</p>
* 2019年7月4日上午8:52:44
* @author lvjie
*/
@Configuration
public class GlobalDataSourceConfiguration {
private static Logger LOG = LoggerFactory.getLogger(GlobalDataSourceConfiguration.class);
@Bean(name = "prestoDataSource")
@ConfigurationProperties(prefix = "spring.datasource.presto")
public DataSource prestoDataSource() {
LOG.info("-------------------- presto init ---------------------");
return DataSourceBuilder.create().build();
}
@Bean(name = "prestoTemplate")
public JdbcTemplate prestoJdbcTemplate(@Qualifier("prestoDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
訪問使用類:
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.hww.boot.service.ITestService;
@Service
public class TestServiceImpl implements ITestService{
@Autowired
@Qualifier("prestoTemplate")
private JdbcTemplate prestoTemplate;
@Override
public List<Map<String, Object>> findList() {
// TODO Auto-generated method stub
return prestoTemplate.queryForList("SELECT a.id,a.name,b.id,b.name FROM postgresql.public.fd_area a,mongodb.test.user b where a.id = b.id");
}
}