我们在使用mybatis时有时候需要查询所有的列,或者较多的列,这时候一个一个添加就会非常麻烦,我们可以使用SQL这样处理:
SELECT
GROUP_CONCAT(COLUMN_NAME SEPARATOR ",")
FROM
information_schema.COLUMNS
WHERE
TABLE_SCHEMA = '数据库名'
AND TABLE_NAME = '表名'
这样我们就把所有的列用逗号连接起来了,但重要的是使用它们,
在mybatis的xml文件中可以这样使用:
<sql id = "base_sql">
case_info,ctcsid,date,degrade_or_outtime,gui_shu_ju,handle_plan,id,
line,lu_ju,machine_num,msisdn,remark,train_num,trouble_appearance,
trouble_cell,trouble_code,trouble_end_location,trouble_end_time,
trouble_RBC,trouble_reason,trouble_start_location,
trouble_start_time,trouble_type
</sql>
<select id="selectCaseInfoByTime" resultMap="caseInfoTableMap" parameterType="java.util.Map">
select
<include refid="base_sql"></include>
from case_info_table
where trouble_start_time
between #{startTime}
and
#{endTime}
order by trouble_start_time desc
</select>
这就为我们提供了很大的便捷.
但是需要注意一点,MySQL的查询结果默认最大字段长度是1024个字节,如果表的字段过多,超出长度限制,将会导致最终结果被截断。
解决办法:
调整最大长度限制,直接执行:
SET GLOBAL group_concat_max_len=102400;
SET SESSION group_concat_max_len=102400;