資料庫同步表資料利器,oracle+mybatis 一個sql控制所有表增刪改 ${xxx} 和 #{xxx}的區別
阿新 • • 發佈:2018-11-04
資料庫同步表資料利器,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"
}
}],
}