JDBC融合Spring技术(连接Hive)--使用JDBC模板

15 篇文章 0 订阅
15 篇文章 0 订阅

1 简述JDBC模板

在实际编写JDBC程序的时候,我们需要负责处理与数据库访问相关的所有事情,这里包括管理数据库资源和处理异常。其中只有20%的代码是真正用于查询数据的,而80%代码都是样本代码,为了解决这一问题我们采用Spring的JDBC模板来简化工作,这里以连接Hive数据库为例。

2 JDBC融合Spring技术示例

基础环境

  • Hive
  • JDK1.8
  • Spring

程序清单

  1. HiveConfig.java – Spring配置文件
  2. HiveRepository.java – Hive仓库接口
  3. JdbcHiveRepository.java – Hive仓库接口的实现类
  4. Records.java – records表的实体类
  5. springContext.xml – Spring的配置文件,用于启用自动扫描功能
  6. TestHive2.java – 程序的测试类

HiveConfig.java

import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@Configuration
@ComponentScan
public class HiveConfig {
	@Bean
	public JdbcTemplate jdbcTemplate(DataSource dataSource){
		return new JdbcTemplate(dataSource);
	}
	
	@Bean
	public DataSource dataSource(){
		DriverManagerDataSource ds = new DriverManagerDataSource();
		ds.setDriverClassName("org.apache.hive.jdbc.HiveDriver");
		ds.setUrl("jdbc:hive2://192.168.225.100:10000/default");
		ds.setUsername("hive");
		ds.setPassword("123456");
		return ds;
	}
}

HiveRepository.java

public interface HiveRepository {
	public Records findOne(Integer quality);
}

JdbcHiveRepository.java

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.inject.Inject;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.stereotype.Repository;

@Repository
public class JdbcHiveRepository implements HiveRepository{
	private JdbcOperations jdbcOperations;
	
	private String SQL_SELECT_RECORDS = "select year, temperature, quality from records where quality = ?";
	
	@Inject
	public JdbcHiveRepository(JdbcOperations jdbcOperations){
		this.jdbcOperations = jdbcOperations;
	}
	
	public Records findOne(Integer quality){
		return jdbcOperations.queryForObject(SQL_SELECT_RECORDS, this::mapRecords, quality);
	}
	
	public Records mapRecords(ResultSet rs, int row) throws SQLException{
		return new Records(
				rs.getString("year"),
				rs.getInt("temperature"),
				rs.getInt("quality"));
	}
}

Records.java

public class Records {
	private String year;
	
	private Integer temperature;
	
	private Integer quality;
	
	

	public Records() {
		super();
	}

	public Records(String year, Integer temperature, Integer quality) {
		super();
		this.year = year;
		this.temperature = temperature;
		this.quality = quality;
	}


	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public Integer getTemperature() {
		return temperature;
	}

	public void setTemperature(Integer temperature) {
		this.temperature = temperature;
	}

	public Integer getQuality() {
		return quality;
	}

	public void setQuality(Integer quality) {
		this.quality = quality;
	}

	@Override
	public String toString() {
		return "Records [year=" + year + ", temperature=" + temperature + ", quality=" + quality + "]";
	}
}

springContext.xml – 这里就不贴全部文件了,只需要在该文件中添加如下一句话就可以了

<context:component-scan base-package="com.css.test.hive"></context:component-scan>

注:我这里上述文件所在的包是这个路径,这个地方读者需要根据自己情况改成自己的包路径。
TestHive2.java

import java.sql.SQLException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class TestHive2 {
    /**
     * 程序入口
     * @param args
     * @throws SQLException
     * @author 一朝风月
     */
    public static void main(String[] args) throws SQLException {
    	ApplicationContext ac = new AnnotationConfigApplicationContext(HiveConfig.class);
    	HiveRepository hr = ac.getBean(HiveRepository.class);
    	Records records = hr.findOne(2);
    	System.out.println(records);
    	((ClassPathXmlApplicationContext) ac).close();
    }
}

3 测试

数据库中表的数据:
在这里插入图片描述
运行TestHive2.java文件
结果:
在这里插入图片描述

4 补充

针对小应用来说上面的获取数据源的方式是合适的,但是引入出现并发访问数据库资源的话就会产生问题,针对大应用数据源需要改为使用数据源连接池。
常见的数据源连接池:

  • Apache Commons DBCP
  • C3p0
  • BoneCP
    这里以DBCP为例
    只需将dataSource()方法改为如下所示方式即可
@Bean
	public BasicDataSource dataSource(){
		BasicDataSource ds = new BasicDataSource();
		ds.setDriverClassName("org.apache.hive.jdbc.HiveDriver");
		ds.setUrl("jdbc:hive2://192.168.225.100:10000/default");
		ds.setUsername("hive");
		ds.setPassword("123456");
		ds.setInitialSize(5);
		ds.setMaxActive(10);
		return ds;
	}

改完之后重新编译运行程序即可,效果如第3部分所示,这里就不再截图展示了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值