java – Mybatis在spring中使用xml配置返回大结果
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – Mybatis在spring中使用xml配置返回大结果,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4742字,纯文字阅读大概需要7分钟。
内容图文
![java – Mybatis在spring中使用xml配置返回大结果](/upload/InfoBanner/zyjiaocheng/745/62ab76782c89427abfeeb7cb2c70c5c2.jpg)
我需要将数据从oracle中的表转储到elasticsearch(1亿条记录),
我的JVM内存限制为256M,我使用以下代码和配置从oracle(mybatis spring)获取数据:
界面:
package com.fudy.mapper;
import java.util.List;
import com.fudy.domain.Person;
public interface PersonMapper {
List<Person> selectAllPerson();
}
xml配置:
<?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.fudy.mapper.PersonMapper">
<resultMap type="com.fudy.domain.Person" id="PersonAlias">
<id column="ID" property="id" />
<result column="NAME" property="name" />
</resultMap>
<select id="selectAllPerson" fetchSize="10000" resultMap="PersonAlias">
SELECT * FROM person
</select>
</mapper>
ApplicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<tx:annotation-driven transaction-manager="transactionManager" />
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="URL" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
<property name="connectionCachingEnabled" value="true"/>
</bean>
<!-- define the SqlSessionFactory, notice that configLocation is not needed
when you use MapperFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:Configuration.xml" />
</bean>
<!-- scan for mappers and let them be autowired -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.fudy.mapper" />
<!-- optional unless there are multiple session factories defined -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
我的junit测试:
package com.fudy.mapper;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.fudy.domain.Person;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"/ApplicationContext.xml"})
public class PersonMapperTest {
@Autowired
private PersonMapper mapper;
@Test
public void testSelectAllPerson() {
List<Person> list = mapper.selectAllPerson();
for ( Person person : list) {
System.out.println(person.getId());
System.out.println(person.getName());
//TODO insert into elasticsearch
}
}
}
你可以从junit测试中看到,Mybatis将返回结果的整个列表,这将导致内存不足问题.谷歌之后,我发现ResultHandler可以解决问题,但我找不到工作的演示.
解决方法:
有两种选择:
>使用ResultHandler
>自3.4.1起使用Cursor
ResultHandler
这就是你可以使用自定义ResultHandler的方法:
PersonMapper.xml
<mapper namespace="com.fudy.mapper.PersonMapper">
<resultMap type="com.fudy.domain.Person" id="PersonAlias">
<id column="ID" property="id" />
<result column="NAME" property="name" />
</resultMap>
<select id="selectAllPerson" resultMap="PersonAlias">
SELECT * FROM person
</select>
</mapper>
PersonMapper.java
public interface PersonMapper {
void selectAllPersons(ResultHandler handler);
}
MyService.java
class PersonResultHandler implements ResultHandler {
@Override
public void handleResult(ResultContext context) {
Person person = (Person)context.getResultObject();
// process person here
}
};
PersonResultHandler handler = new PersonResultHandler();
PersonMapper personMapper = ...;
personMapper.selectAllPersons(handler);
光标
从mybatis 3.4.1开始,你可以返回Cursor这是Iterable并且可以像这样使用(在结果有序的情况下,详见上面的Cursor API java doc):
PersonMapper.java
public interface PersonMapper {
Cursor<Person> selectAllPersons();
}
MyService.java
PersonMapper personMapper = ...;
try (Cursor<Person> persons = personMapper.selectAllPersons()) {
for (Person person : persons) {
// process one person
}
}
内容总结
以上是互联网集市为您收集整理的java – Mybatis在spring中使用xml配置返回大结果全部内容,希望文章能够帮你解决java – Mybatis在spring中使用xml配置返回大结果所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。