为避免数据库表过大,要求按月分表。当时是先创建了两年的数据库表,然后存储数据的时候,根据当前日期的年月确定存入的数据库表,xml中的插入语句如下:
<insert id="insertCloudSwitch" parameterType="CloudSwitch">
<selectKey keyProperty="id" resultType="String" order="BEFORE">
select REPLACE(UUID(),"-","")
</selectKey>
insert into
<if test="tableName!=null and tableName!=''">
${tableName}
</if>
(id,phone,poldswitch,pnowswitch,uoldswitch,unowswitch,timestamp)
values(#{id},#{cloudSwitch.phone},#{cloudSwitch.poldswitch},#{cloudSwitch.pnowswitch},#{cloudSwitch.uoldswitch},#{cloudSwitch.unowswitch},#{cloudSwitch.timestamp})
</insert>
由于表名tableName是作为参数传进来的,在这里需要用到${tableName}。
由于$存在sql注入的风险,但是表名无法通过#来传,通过查阅资料,决定利用存储过程,把sql语句写到数据库的存储过程中去。