运维开发网

spark (scala)使用mybatis插入数据到mysql

运维开发网 https://www.qedev.com 2020-03-27 20:23 出处:网络 作者:运维开发网整理
val reader = Resources.getResourceAsReader("mybatis-priceratio.xml") val ds:DataSource = new SingleConnectionDataSource(GlobalConfig.priceratioJdbcUrl,GlobalConfig.priceratioJdbcUser,GlobalCon
        val reader = Resources.getResourceAsReader("mybatis-priceratio.xml")
        val ds:DataSource = new SingleConnectionDataSource(GlobalConfig.priceratioJdbcUrl,GlobalConfig.priceratioJdbcUser,GlobalConfig.priceratioJdbcPwd,true);
        val transactionFactory:TransactionFactory  = new JdbcTransactionFactory()
        val environment:Environment  = new Environment("development", transactionFactory, ds)
        val configuration = new XMLConfigBuilder(reader).parse()
        configuration.setEnvironment(environment)
        val fac:SqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration)
        val session = fac.openSession();
        try{
          val mp:HotelChannelMappingMapper = session.getMapper(classOf[HotelChannelMappingMapper])
          val list = tp2._2.toSeq
          mp.insertWithBatch(seqAsJavaList(list));
          //不提交事务 ,数据不会保存
          session.commit();
        }finally{
          session.close();
        }

2) mybatis-priceratio.xml 配置

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

	<!-- 指定映射文件 -->
	<mappers>
		
		<mapper resource="mapper/ChannelScenicProductPackageMapper.xml"/>
		<mapper resource="mapper/ScenicChannelMappingMapper.xml"/>
	
	</mappers>
</configuration>  
3)依赖包 spring 
0

精彩评论

暂无评论...
验证码 换一张
取 消