mybatis整合mySql的一些常用批量操作

基础数据准备:

 
     `ID`, NAME,PHONE,IS_DELETED

1.批量插入


INSERT INTO
test.`t_TEST`
(

)
VALUES

(
#{ item.id },
#{ item.name},
#{ item.phone}
)

2.批量更新

     1) 推荐使用       


UPDATE
test.`t_TEST`



WHEN (
`IS_DELETED`=0
AND `ID` = #{ item.id }
)
THEN #{item.name}




WHEN (
`IS_DELETED`=0
AND `ID` = #{ item.id }
)
THEN #{item.phone}


WHERE

(
`IS_DELETED`=0
AND `ID` = #{ item.id }
)

2)



UPDATE
test.`t_TEST`
SET
`NAME`=#{ item.name },
WHERE
`IS_DELETED`=0
AND `ID`=#{item.id}

   3.根据某个列表批量查询

SELECT
< include refid = "entityColumnList" />
FROM
 test.`t_TEST`
WHERE
`IS_DELETED` = 0

AND NAME IN
< foreach collection = "request.nameList" item = "name" INDEX = "index" OPEN = "(" CLOSE = ")" SEPARATOR = "," >
#{name}



4.数据库直接插入查询出的数据:

INSERT INTO   test.`t_TEST_COPY`
(
`ID`,
`NAME`,
`PHONE`
)
SELECT
RIGHT (UUID_SHORT(), 10) ID,
` t`.NAME AS `NAME`,
` t`.`PHONE` AS `PHONE`
FROM
test.`t_TEST` t
WHERE
(`t`.`IS_DELETED` = 0)
UNION ALL
SELECT
RIGHT (UUID_SHORT(), 10) ID,
`mcr`.`NAME` AS `NAME`,
`mcr`.`PHONE` AS `PHONE`
FROM
test.`t_TEST_1` t1
WHERE
(`t1`.`IS_DELETED` = 0)

 

你可能感兴趣的:(mybatis整合mySql的一些常用批量操作)