1.mysql创建表,并导入数据

CREATE TABLE `test` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(60) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3637997 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

2.导入数据(省略)

3.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.hdlh.cpn.synchronization.client.mapper.mysql.TestMysqlMapper">  
    <select id="selectTest"  resultType="map">
    	select id,name from test
    </select>
</mapper>

4.查询调用


```java
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Demo {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    @Test
    public void select() {
        Cursor<Map> cursor = null;
        SqlSession sqlSession = null;
        try {
            long start = System.currentTimeMillis();

            sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession();
            long query = System.currentTimeMillis();
            System.out.println(" 查询消耗时间:" + (query - start));

            //query1为方法名
            Cursor<Map> tables = sqlSession.selectCursor("selectTest");
            //foreach方式,也可使用迭代器
            AtomicInteger count = new AtomicInteger();
            tables.forEach(table -> {
                //处理逻辑
                count.getAndIncrement();
                if (count.intValue() % 500000 == 0) {
                    System.out.println("执行数量:" + count);
                }
            });
            System.out.println(count);
            long total = System.currentTimeMillis();
            System.out.println(" 总消耗时间:" + (total - start));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != cursor) {
                try {
                    cursor.close();
                } catch (Exception e) {

                }
            }
            if (null != sqlSession) {
                try {
                    sqlSession.close();
                } catch (Exception e) {

                }
            }
        }
    }
}

5.执行结果```(数据量:3637996)

查询消耗时间:1
执行数量:500000
执行数量:1000000
执行数量:1500000
执行数量:2000000
执行数量:2500000
执行数量:3000000
执行数量:3500000
3637996
 总消耗时间:15148
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐