网上搜了一把居然没有mybaits hive 整合,很多人说mybaits不支持hive,我就陷入了思考,mybatis就是个orm框架,支持mysql为什么不支持hive,没道理啊。果断行动起来,真的是可以的。
1.spring数据源设置
pom.xml
<dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId> <version>2.3.3</version> </dependency>
注意hive-jdbc版本,低版本有可能不支持mybatis
properties
#hive数据库 hive.jdbc.user=root hive.jdbc.password=123456 hive.jdbc.url=jdbc:hive2://10.10.22.133:10000/datacenter hive.jdbc.driver=org.apache.hive.jdbc.HiveDriver
spring xml
<!-- hive datasource --> <bean id="dataSourceHive" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="driverClassName" value="${hive.jdbc.driver}" /> <property name="url" value="${hive.jdbc.url}" /> <property name="username" value="${hive.jdbc.user}" /> <property name="password" value="${hive.jdbc.password}" /> </bean> <bean id="sqlSessionFactoryHive" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSourceHive" /> <property name="mapperLocations"> <list> <value>classpath*:com/itxw/hiveDao/mapper/*Mapper.xml </value> </list> </property> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itxw.hiveDao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryHive" /> </bean>
配置都和mysql数据源一样的呀。
2.我都懒得写了,全和mysql一样
dao
public interface HiveSubjectMapper { int insertList(List<SubjectTotalScore> list); }
mapper
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.itxw.hiveDao.HiveSubjectMapper"> <insert id="insertList" parameterType="java.util.List"> insert into subject (PROJECT_ID, PROJECT_NAME, STUDENT_ID, STUDENT_NAME, CLASS_ID, CLASS_NAME, SCHOOL_ID, SCHOOL_NAME, TOTAL_SCORE, FULL_SCORE,UNION_TAG,GRADE,START_SCHOOL_YEAR ) values <foreach collection="list" item="item" index="index" separator=","> ( #{item.projectId,jdbcType=VARCHAR}, #{item.projectName,jdbcType=VARCHAR}, #{item.studentId,jdbcType=VARCHAR}, #{item.studentName,jdbcType=VARCHAR}, #{item.classId,jdbcType=VARCHAR}, #{item.className,jdbcType=VARCHAR}, #{item.schoolId,jdbcType=VARCHAR}, #{item.schoolName,jdbcType=VARCHAR}, #{item.totalScore,jdbcType=DECIMAL}, #{item.fullScore,jdbcType=DECIMAL}, #{item.unionTag},#{item.grade},#{item.startSchoolYear} ) </foreach> </insert> </mapper>
亲测有效,而且我测试还是配置的两个数据源,一个mysql一个hive分别扫描不同的包!!!