关于mybatis和mysql的几点体会

本文探讨了SQL中使用union操作符时不同数值类型的合并问题,特别是在MyBatis框架下如何处理可能出现的数据类型转换,以及如何通过Java的Number类来统一处理各种数值类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原理不明,纯属经验

#查询返回结果为Map

<!--<select id="test" resultType="map">-->
<!--这是一个统计某天数据的例子,resultType两种写法应该都是可以的-->
<select id="test" resultType="java.util.Map">
	select 
		temp.tinyint_column as tinyintColumn, <!--Long-->
		sum(temp.cnt) as cnt <!--BigDecimal-->
	from
	(
		select 
			tinyint_column, <!--Integer-->
			count(*) as cnt <!--Long-->
		from
			test_table
		<where>
			day = #{dayInt}
		</where>
		group by tinyint_column
		union
		select 
			tinyint_column -1 as tinyint_column, <!--Integer-->
			count(*) as cnt <!--Long-->
		from
			test_table
		<where>
			day > #{dayInt} and tinyint_column > 1
		</where>
		group by tinyint_column
	) temp
	group by temp.tinyint_column
</select>

类似于这样的嵌套查询,返回值类型在运算中会变化,
`
个人认为:
union 操作符将两部分数据合并时如果不改变数值类型就有可能存在数值长度问题

  1. Integer+Integer长度可能会超出Integer所以返回类型为Long
  2. Long+Long长度可能会超出Long,故而返回类型为BigDecimal

无论返回何种数值类型,都属于Number类的子类,所以dao中这么写
`

public List<Map<String, Number>> test() {
	int dayInt = 20180424;
	return getSqlSession().selectList(NAME_SPACE + ".test", dayInt);
}
>`这块存在一个问题,就是如果返回值使用List

然后在使用的时候,可以灵活取出相应数值类型,下面附上Number源码

 * @see     java.lang.Byte
 * @see     java.lang.Double
 * @see     java.lang.Float
 * @see     java.lang.Integer
 * @see     java.lang.Long
 * @see     java.lang.Short
 * @since   JDK1.0
 */
public abstract class Number implements java.io.Serializable {
    /**
     * Returns the value of the specified number as an <code>int</code>.
     * This may involve rounding or truncation.
     *
     * @return  the numeric value represented by this object after conversion
     *          to type <code>int</code>.
     */
    public abstract int intValue();

    /**
     * Returns the value of the specified number as a <code>long</code>.
     * This may involve rounding or truncation.
     *
     * @return  the numeric value represented by this object after conversion
     *          to type <code>long</code>.
     */
    public abstract long longValue();

    /**
     * Returns the value of the specified number as a <code>float</code>.
     * This may involve rounding.
     *
     * @return  the numeric value represented by this object after conversion
     *          to type <code>float</code>.
     */
    public abstract float floatValue();

    /**
     * Returns the value of the specified number as a <code>double</code>.
     * This may involve rounding.
     *
     * @return  the numeric value represented by this object after conversion
     *          to type <code>double</code>.
     */
    public abstract double doubleValue();

    /**
     * Returns the value of the specified number as a <code>byte</code>.
     * This may involve rounding or truncation.
     *
     * @return  the numeric value represented by this object after conversion
     *          to type <code>byte</code>.
     * @since   JDK1.1
     */
    public byte byteValue() {
        return (byte)intValue();
    }

    /**
     * Returns the value of the specified number as a <code>short</code>.
     * This may involve rounding or truncation.
     *
     * @return  the numeric value represented by this object after conversion
     *          to type <code>short</code>.
     * @since   JDK1.1
     */
    public short shortValue() {
        return (short)intValue();
    }

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -8742448824652078965L;
}

#判断集合参数是否为空

<if test="listIds != null and listIds.size() > 0">
	or id in
	<foreach collection="listIds" item="id" open="(" close=")" separator=",">
		#{id}
	</foreach>
</if> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值