解决GROUP_CONCAT结果长度被限制问题

解决GROUP_CONCAT结果长度被限制问题

//这是我的sql语句
<select id="statByClassByCreateTimeAndType"  resultType="StatItemVo">
		select class_id as group_id, class_name as group_name, result, count(user_id) num, GROUP_CONCAT(user_name SEPARATOR '、') AS userNameList from t_ems_dorm_check_record
		where school_id = #{schoolId}
		<if test="classId!=null">and class_id = #{classId}</if>
		<if test="startTime!=null">and check_date >= #{startTime}</if>
		<if test="endTime!=null">and #{endTime} >= check_date</if>
		<if test="confId!=null">and conf_id = #{confId}</if>
		<if test="userName !='' and userName != null"> AND user_name LIKE CONCAT('%',#{userName},'%')</if>
		<if test="result!=null">and result = #{result}</if>
		group by class_id, result
		<if test="sort == 1">
			ORDER BY num DESC
		</if>
		<if test="sort == 2">
			ORDER BY class_name DESC
		</if>
	</select>

得到的结果

{"groupId":617411,"groupName":"19高级数控加工1952班","num":720,"userNameList":"李晓曦、黄
祖宜、赵大志、林薇薇、陈紫函、李安然、郑淑仪、莫慧敏、程峰、黄远程、黄浩然、林子轩、江与城、林少杰、
邵杰、黄冠、李洋、刘洋、孙绍文、马海成、黄志毅、吴敬梓、邓凯文、林宇轩、李晓曦、黄祖宜、赵大志、林薇
薇、陈紫函、李安然、郑淑仪、莫慧敏、程峰、黄远程、黄浩然、林子轩、江与城、林少杰、邵杰、黄冠、李洋、
刘洋、孙绍文、马海成、黄志毅、吴敬梓、邓凯文、林宇轩、李晓曦、黄祖宜、赵大志、林薇薇、陈紫函、李安
然、郑淑仪、莫慧敏、程峰、黄远程、黄浩然、林子轩、江与城、林少杰、邵杰、黄冠、李洋、刘洋、孙绍文、马
海成、黄志毅、吴敬梓、邓凯文、林宇轩、李晓曦、黄祖宜、赵大志、林薇薇、陈紫函、李安然、郑淑仪、莫慧
敏、程峰、黄远程、黄浩然、林子轩、江与城、林少杰、邵杰、黄冠、李洋、刘洋、孙"},

//这里的num是求总和,这里怎么看也不像是有720的样子,于是就开始怀疑是count(user_id) num函数的问
//题,或者是分组的问题,但是无论怎么改都没用,到后面才发现是GROUP_CONCAT函数的输出可能被限制了长
//度,于是开始查询资料,可以通过改变group_concat_max_len的值拿到全部数据

接下来开始说解决方法,先说第一种,也是我推荐的一种

//在mapper接口定义
@Insert("SET SESSION group_concat_max_len=1024000")
    void setGroupConcatMaxLen();
//在调用mapper接口前调用即可	    
	    dormCheckRecordDao.setGroupConcatMaxLen();
        statItemVos = dormCheckRecordDao.statByClassByCreateTimeAndType(dormCheckRecordVo);

第二种

//在mysql中设置,但是这种方法需要重启数据库,生产环境不可能随便重启,如果是个人项目的话,随便搞
SHOW VARIABLES LIKE "group_concat_max_len";
SET SESSION group_concat_max_len = 10240000;

第三种

@Autowired
JdbcTemplate jdbcTemplate;
// 设置 group_concat_max_len 只在这个连接上生效
jdbcTemplate.execute("SET SESSION group_concat_max_len = 1000000");
//这种方法我没有成功,我怀疑是配置文件的问题,但是公司这个项目又老又大,好几个配置文件,我不敢改,果断放弃

第四种

	@Autowired
    private SqlSessionFactory sqlSessionFactory;

    public List<StatItemVo> getStats(YourParams params) {
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            // 获取当前连接
            Connection conn = sqlSession.getConnection();
            // 使用原生JDBC执行SET SESSION命令
            try (Statement stmt = conn.createStatement()) {
                stmt.executeUpdate("SET SESSION group_concat_max_len = 1000000");
            }
            // 现在执行你的MyBatis查询
            YourMapper mapper = sqlSession.getMapper(YourMapper.class);
            return mapper.statByClassByCreateTimeAndType(params);
        } catch (SQLException e) {
            // 处理异常
            throw new RuntimeException(e);
        }
    }
//这种方法我也没成功,不知道为什么,也是怀疑配置文件没有返回代码对象,但是我改不了

第五种

//直接修改配置
 @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");
        dataSource.setUsername("root");
        dataSource.setPassword("password");

        // 设置初始化SQL
        CompositeResourceBundle resourceBundle = new CompositeResourceBundle();
        resourceBundle.add(new ByteArrayResource("SET SESSION group_concat_max_len=1000000".getBytes()));
        dataSource.setConnectionProperties(resourceBundle);

        return dataSource;
    }
spring:
  datasource:
    tomcat:
      init-sql: SET SESSION group_concat_max_len=100000
spring.datasource.tomcat.init-sql=SET SESSION group_concat_max_len=100000
  • 20
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值