有个需求:code为树状结构。如一级:1000、1001、1002...第二级:100001、100002、100101...第三级:10000101、10000102...现在插入一个新的结点,给你父节点id,找到新插入子节点的code值。
<select id="getMaxCode" parameterType="map" resultType="String">
<if test="pcode == '0'.toString()">
select IFNULL(MAX(code),999)+1 as code from ${tableName} WHERE 1=1
and code like CONCAT('100', '%')
AND 4 >= LENGTH(code)
</if>
<if test="pcode != '0'.toString()">
select IFNULL(MAX(code),CONVERT(#{pcode}*'100',SIGNED))+1 as code from ${tableName} WHERE 1=1
and code like CONCAT(#{pcode}, '%')
AND LENGTH(#{pcode})+2 = LENGTH(code)
</if>
</select>
总结:
1、test里面和字符串相比较,str要toString()一下。
2、ifnull、convert使用,前者是如果第一个参数为空则用第二个;后者是用于类型转化
这样java代码少写了好多......