数据库同步表数据利器,mybatis 一个sql控制所有表增删改
在项目开发过程中,尤其是多系统项目集群中,经常会遇到需要从一个数据库同步n张表到另一个数据库中的需求,还需要对这些表做监听,在发现有修改的时候进行增量数据同步。
通常的方法是在接受数据库对应的项目中写接口供数据源项目调用,这也就意味着我们需要写n张表的增删改sql语句,一条一条的写,估计会累死,好在mybatis足够给力,可以通过动态参数实现一条sql控制多张表的操作。
sql如下:
insert id="insertPotenTable" parameterType="com.ztesoft.zop.common.appvo.PotenChange" useGeneratedKeys="false">
insert into ${table_name}
<foreach collection="parameterMap" index="key" item="value" separator="," open="(" close=")">
${key}
</foreach>
values
<foreach collection="parameterMap" index="key" item="value" separator="," open="(" close=")">
<choose>
<when test="key.endsWith('_time') or key.endsWith('_date')">
to_date(#{value},'yyyy-mm-dd hh24:mi:ss')
</when>
<otherwise>
#{value}
</otherwise>
</choose>
</foreach>
</insert>
<update id="updatePotenTable" parameterType="com.ztesoft.zop.common.appvo.PotenChange">
update ${table_name}
set
<foreach collection="parameterMap" index="key" item="value" separator=",">
${key} =
<choose>
<when test="key.endsWith('_time') or key.endsWith('_date')">
to_date(#{value},'yyyy-mm-dd hh24:mi:ss')
</when>
<otherwise>
#{value}
</otherwise>
</choose>
</foreach>
where ${main_key} = #{main_key_value}
</update>
<delete id="delPotenTable" parameterType="com.ztesoft.zop.common.appvo.PotenChange">
delete from ${table_name}
where ${main_key} = #{main_key_value}
</delete>
其中${xxx} 和 #{xxx} 的区别在于 mybatis会将${xxx}中的值不做任何处理写到sql中,一般用于表名或字段名。
比较惊喜的是mybatis的if判断居然还支持 字符串.endsWith(“xxx”),可以用来做比较特殊的字段传值操作,如date之类的特殊数据类型。
另外所传的数据为json格式 示例如下:
{
"rows": [{
"sql_id": "5",
"sql_type": "insert",
"main_key_value": "c1082b67391f4c6cb7caaee984dcfeac",
"main_key": "log_id",
"table_name": "potential_customer_oper_log",
"parameterMap": {
"log_id": "c1082b67391f4c6cb7caaee984dcfeac",
"cmut_id": "1",
"insert_type": "1",
"create_time": "2018-05-17 19:26:41",
"sys_user_id": "19920511",
"the_day": "20",
"pote_cust_id": "56",
"the_month": "5",
"the_year": "2018",
"the_week": "17",
"history_status": "0-1"
}
}, {
"sql_id": "8",
"sql_type": "update",
"main_key_value": "a7729d697de14398a388f850aa2ac66f",
"main_key": "log_id",
"table_name": "potential_customer_oper_log",
"parameterMap": {
"log_id": "a7729d697de14398a388f850aa2ac66f",
"cmut_id": "1",
"insert_type": "1",
"create_time": "2018-05-19 16:56:56",
"sys_user_id": "19911234",
"the_day": "20",
"pote_cust_id": "66",
"the_month": "5",
"the_year": "2018",
"the_week": "19",
"history_status": "0-1"
}
}, {
"sql_id": "7",
"sql_type": "insert",
"main_key_value": "d7970dc013104223874baa5ebe8d965c",
"main_key": "log_id",
"table_name": "potential_customer_oper_log",
"parameterMap": {
"log_id": "d7970dc013104223874baa5ebe8d965c",
"cmut_id": "1",
"insert_type": "2",
"create_time": "2018-05-19 17:45:27",
"sys_user_id": "19920511",
"the_day": "19",
"pote_cust_id": "66",
"the_month": "5",
"the_year": "2018",
"the_week": "20",
"history_status": "1-2"
}
}, {
"sql_id": "6",
"sql_type": "delete",
"main_key_value": "f7526bf3d95946adae5187e8fd739fb1",
"main_key": "log_id",
"table_name": "potential_customer_oper_log",
"parameterMap": {
"log_id": "f7526bf3d95946adae5187e8fd739fb1",
"cmut_id": "1",
"insert_type": "1",
"create_time": "2018-05-19 17:01:23",
"sys_user_id": "19920511",
"the_day": "20",
"pote_cust_id": "67",
"the_month": "5",
"the_year": "2018",
"the_week": "19",
"history_status": "0-1"
}
}],
}