MySql batch database operations, mysql database

Source: Internet
Author: User

MySql batch database operations, mysql database

  • 1) Batch insert
  • Batch operations mainly use the foreach of Mybatis to traverse the parameter list and perform corresponding operations,
  • Therefore, the batch insert, update, and delete statements are similar, but SQL statements are slightly different.
  • Mysql batch operation requires database connection configuration allowMultiQueries = true.
  • <Insert id = "batchInsert" parameterType = "java. util. List" useGeneratedKeys = "true">
  • <Foreach close = "" collection = "list" index = "index" item = "item" open = "" separator = ";">
  • Insert into user (name, age, dept_code) values
  • (# {Item. name, jdbcType = VARCHAR },
  • # {Item. age, jdbcType = INTEGER },
  • # {Item. deptCode, jdbcType = VARCHAR}
  • )
  • </Foreach>
  • </Insert>
  • Or
  • <Insert id = "batchInsert" parameterType = "java. util. List" useGeneratedKeys = "true">
  • Insert into user (name, age, dept_code) values
  • <Foreach collection = "list" index = "index" item = "item" open = "" close = "" separator = ",">
  • (# {Item. name, jdbcType = VARCHAR },
  • # {Item. age, jdbcType = INTEGER },
  • # {Item. deptCode, jdbcType = VARCHAR}
  • )
  • </Foreach>
  • </Insert>
  • (2) Batch update
  • <Update id = "batchUpdate" parameterType = "java. util. List">
  • <Foreach close = "" collection = "list" index = "index" item = "item" open = "" separator = ";">
  • Update user set name = # {item. name, jdbcType = VARCHAR}, age = # {item. age, jdbcType = INTEGER}
  • Where id = # {item. id, jdbcType = INTEGER}
  • </Foreach>
  • </Update>
  • (3) Batch Delete
  • <Delete id = "batchDelete" parameterType = "java. util. List">
  • <Foreach close = "" collection = "list" index = "index" item = "item" open = "" separator = ";">
  • Delete from user
  • Where id = # {item. id, jdbcType = INTEGER}
  • </Foreach>
  • </Delete>

 

  • Ii. Fuzzy search
  • <Select id = "selectLikeName" parameterType = "java. lang. String" resultMap = "BaseResultMap">
  • Select
  • <Include refid = "Base_Column_List"/>
  • From user
  • Where name like CONCAT ('%', # {name}, '% ')
  • </Select>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.