Mybatis标签语法大全

59 阅读1分钟

Mybatis标签语法大全 ,更多用法请参照:baomidou.gitee.io/mybatis-plu…

  1. <sql>

    • 在配置文件中定义可复用的 SQL 片段,便于在其他查询语句中引用:``` id, username, password ... SELECT FROM users

  2. <select/>, <insert/>, <update/>, <delete/>

    • 这些标签在映射文件中分别用来定义 SQL 查询、插入、更新和删除操作,以及它们的执行逻辑和结果映射:``` SELECT * FROM users WHERE id = #{id}

      INSERT INTO users(username, password) VALUES(#{username}, #{password})
  3. <if>, <choose>, <when>, <otherwise>, <where>, <set>, <foreach>

    • 动态 SQL 标签使得 SQL 语句可以根据参数动态构建:```

      SELECT * FROM users as users AND users.id = #{param.id} AND users.user_name LIKE CONCAT('%',#{param.userName},'%') and users.id in (#{id}) ```xml <!-- 使用 foreach 循环遍历集合参数 --> <insert id="batchInsertUsers"> INSERT INTO users(username, email) VALUES <foreach item="user" index="index" collection="list" open="(" separator="),(" close=")"> #{user.username}, #{user.email} </foreach> </insert> ``` ```xml <!-- 使用 choose when 判断 --> <if test="param.regType !=null and param.regType != ''"> <choose> <when test="param.regType == 'weixin'"> and users.reg_type = 'weixin' </when> <when test="param.regType == 'app'"> and users.reg_type ='app' </when> <otherwise> and users.reg_type = 'other' </otherwise> </choose> </if> 原文链接 www.hanyuanhun.cn | node.hanyuanhun.cn