presto整合MyBatis

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 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值