当前位置:Gxlcms > 数据库问题 > MySQL和mybatis查询相关

MySQL和mybatis查询相关

时间:2021-07-01 10:21:17 帮助过:18人阅读

* from t_blog where id in <foreach collection="list" index="index" item="item" open="(" separator="," close=")"> #{item} </foreach> </select>

 2.mybatis批量插入更新

  方法一:

<update id="batchUpdate" parameterType="java.util.List"> 
      <foreach separator=";" index="index" item="item" collection="list" close="" open=""> 
      update sys_group set level = #{item.level,jdbcType=INTEGER}
       where group_id = #{item.groupId,jdbcType=INTEGER}
      </foreach> 
  </update>

  方法二:

<update id="batchUpdate1" parameterType="java.util.List"> 
     
      update sys_group set level = null where level in
       <foreach separator="," index="index" item="item" collection="list" close=")" open="("> 
         #{item}
      </foreach> 
  </update>

 3.mybatis映射

<resultMap id="BaseResultMap" type="me.gacl.domain.User" >
      <id column="user_id" property="userId" jdbcType="CHAR" />
      <result column="user_name" property="userName" jdbcType="VARCHAR" />
      <result column="user_birthday" property="userBirthday" jdbcType="DATE" />
      <result column="user_salary" property="userSalary" jdbcType="DOUBLE" />
    </resultMap>
   <sql id="Base_Column_List" >
     user_id, user_name, user_birthday, user_salary
   </sql>

4.mybatis查询select

<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
     select 
     <include refid="Base_Column_List" />
     from t_user
     where user_id = #{userId,jdbcType=CHAR}
   </select>

5.mybatis删除delete

<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
     delete from t_user
     where user_id = #{userId,jdbcType=CHAR}
   </delete>

6.mybatis插入insert

  一:

 <insert id="insert" parameterType="me.gacl.domain.User" >
     insert into t_user (user_id, user_name, user_birthday, 
       user_salary)
     values (#{userId,jdbcType=CHAR}, #{userName,jdbcType=VARCHAR}, #{userBirthday,jdbcType=DATE}, 
       #{userSalary,jdbcType=DOUBLE})
   </insert>

  二:

 <insert id="insertSelective" parameterType="me.gacl.domain.User" >
     insert into t_user
     <trim prefix="(" suffix=")" suffixOverrides="," >
       <if test="userId != null" >
         user_id,
       </if>
       <if test="userName != null" >
         user_name,
       </if>
       <if test="userBirthday != null" >
         user_birthday,
       </if>
       <if test="userSalary != null" >
         user_salary,
       </if>
     </trim>
     <trim prefix="values (" suffix=")" suffixOverrides="," >
       <if test="userId != null" >
         #{userId,jdbcType=CHAR},
       </if>
       <if test="userName != null" >
         #{userName,jdbcType=VARCHAR},
       </if>
       <if test="userBirthday != null" >
         #{userBirthday,jdbcType=DATE},
       </if>
       <if test="userSalary != null" >
         #{userSalary,jdbcType=DOUBLE},
       </if>
     </trim>
   </insert>

7.mybatis更新update

  一:

<update id="updateByPrimaryKey" parameterType="me.gacl.domain.User" >
     update t_user
     set user_name = #{userName,jdbcType=VARCHAR},
       user_birthday = #{userBirthday,jdbcType=DATE},
       user_salary = #{userSalary,jdbcType=DOUBLE}
     where user_id = #{userId,jdbcType=CHAR}
   </update>

  二:

<update id="updateByPrimaryKeySelective" parameterType="me.gacl.domain.User" >
     update t_user
     <set >
       <if test="userName != null" >
         user_name = #{userName,jdbcType=VARCHAR},
       </if>
       <if test="userBirthday != null" >
         user_birthday = #{userBirthday,jdbcType=DATE},
       </if>
       <if test="userSalary != null" >
         user_salary = #{userSalary,jdbcType=DOUBLE},
       </if>
     </set>
     where user_id = #{userId,jdbcType=CHAR}
   </update>

 

MySQL和mybatis查询相关

标签:util   domain   col   type   更新   day   har   ati   log   

人气教程排行