1.首先引入依赖,依赖的版本跟安装的presto版本保持一致
<dependency>
<groupId>com.facebook.presto</groupId>
<artifactId>presto-jdbc</artifactId>
<version>0.100</version>
</dependency>
2.application.properties配置
presto.datasource.name=presto
presto.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
presto.datasource.driver-class-name=com.facebook.presto.jdbc.PrestoDriver
presto.datasource.url=jdbc:presto://192.168.247.100:8082/hive/default
presto.datasource.username=root
url的配置可以参考JD的文档,下面给出示例截图。具体参考:http://prestodb.jd.com/docs/current/installation/jdbc.html
3.presto数据源配置类
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@Configuration
@MapperScan(basePackages = {"com.presto.dao.mapper"}, sqlSessionTemplateRef = "prestoJdbcSqlSessionTemplate")
public class PrestoConfig {
@Bean(name = "prestoJdbcDataSource")
@ConfigurationProperties(prefix = "presto.datasource")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "prestoJdbcSqlSessionFactory")
public SqlSessionFactory testSqlSessionFactory(@Qualifier("prestoJdbcDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/presto/dao/mapper/*.xml"));
return bean.getObject();
}
@Bean(name = "prestoJdbcTransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("prestoJdbcDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "prestoJdbcSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("prestoJdbcSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
com.presto.dao.mapper是你项目mapper所在包
4.一定要在mapper的xml文件中加statementType="STATEMENT"
<select id="" statementType="STATEMENT"></select>
不加会报下面的错误,这是因为presto-jdbc.jar没有实现sql预编译接口
com.facebook.presto.jdbc.NotImplementedException: Method Connection.prepareStatement is not yet implemented