美文网首页
mybatis增删改查mapper文件写法

mybatis增删改查mapper文件写法

作者: writeanewworld | 来源:发表于2018-09-26 11:45 被阅读0次
<?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
  <mapper namespace="com.wagu.crm.mapper.consumer.ConsumerMapper">
   <resultMap id="BaseResultMap" type="com.wagu.crm.po.data.consumer.Consumer">
      <id column="id" property="id" jdbcType="INTEGER"/>
      <result column="consumer_name" property="consumerName" jdbcType="VARCHAR"/>
      <result column="icon_url" property="iconUrl" jdbcType="VARCHAR"/>
      <result column="mobilephone" property="mobilephone" jdbcType="VARCHAR"/>
      <result column="password" property="password" jdbcType="VARCHAR"/>
      <result column="salt" property="salt" jdbcType="VARCHAR"/>
      <result column="md5_password" property="md5Password" jdbcType="VARCHAR"/>
      <result column="gender" property="gender" jdbcType="INTEGER"/>
      <result column="consumer_level" property="consumerLevel" jdbcType="VARCHAR"/>
      <result column="status" property="status" jdbcType="INTEGER"/>
      <result column="last_modify" property="lastModify" jdbcType="TIMESTAMP"/>
      <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
   </resultMap>

<resultMap id="BaseResultStoreMap" type="com.wagu.crm.po.data.consumer.ConsumerWithStore">
    <id column="consumer_id" property="id" jdbcType="INTEGER"/>
    <result column="consumer_name" property="consumerName" jdbcType="VARCHAR"/>
    <result column="icon_url" property="iconUrl" jdbcType="VARCHAR"/>
    <result column="mobilephone" property="mobilephone" jdbcType="VARCHAR"/>
    <result column="password" property="password" jdbcType="VARCHAR"/>
    <result column="salt" property="salt" jdbcType="VARCHAR"/>
    <result column="md5_password" property="md5Password" jdbcType="VARCHAR"/>
    <result column="gender" property="gender" jdbcType="INTEGER"/>
    <result column="consumer_level" property="consumerLevel" jdbcType="VARCHAR"/>
    <result column="consumer_status" property="status" jdbcType="INTEGER"/>
    <result column="consumer_last_modify" property="lastModify" jdbcType="TIMESTAMP"/>
    <result column="consumer_create_time" property="createTime" jdbcType="TIMESTAMP"/>
    <collection property="storeIds" ofType="java.lang.Integer">
        <result column="store_id"></result>
    </collection>
</resultMap>

<sql id="Base_Column_List">
    id, consumer_name,icon_url, mobilephone, password, salt, md5_password, gender, consumer_level, status, last_modify, create_time
</sql>

<select id="queryByRequest" resultMap="BaseResultMap" parameterType="com.wagu.crm.po.request.consumer.ConsumerRequest">
    select <include refid="Base_Column_List" />
    from  t_consumer
    <where>
        <if test="id != null" >
            id = #{id}
        </if>
        <if test="consumerName != null" >
            consumer_name = #{concumerName}
        </if>
        <if test="mobilephone != null" >
            and mobilephone = #{mobilephone}
        </if>
        <if test="password != null" >
            and password = #{password}
        </if>
        <if test="salt != null" >
            and salt = #{salt}
        </if>
        <if test="md5Password != null" >
            and md5_password = #{md5Password}
        </if>
        <if test="gender != null" >
            and gender = #{gender}
        </if>
        <if test="consumerLevel != null" >
            and consumer_level = #{consumerLevel}
        </if>
        <if test="status != null" >
            and status = #{status}
        </if>
    </where>
</select>

<select id="queryWithStore" resultMap="BaseResultStoreMap" parameterType="com.wagu.crm.po.request.consumer.ConsumerRequest">
    select  consumer.id as consumer_id, icon_url, consumer_name, mobilephone, password, salt,
        md5_password, gender, consumer_level, consumer.status as consumer_status,
        consumer.last_modify as consumer_last_modify, consumer.create_time as consumer_create_time,
        job,last_login_time
        ,store_id
    from  t_consumer consumer
    left join t_consumer_store st
    on consumer.id = st.consumer_id
    <where>
        <if test="id != null" >
            consumer.id = #{id}
        </if>
        <if test="storeId != null" >
            and store_id = #{storeId}
        </if>
        <if test="job != null" >
            and job = #{job}
        </if>
        <if test="lastLoginTime != null" >
            and last_login_time = #{lastLoginTime}
        </if>
        <if test="consumerName != null" >
            and consumer_name = #{consumerName}
        </if>
        <if test="mobilephone != null" >
            and mobilephone = #{mobilephone}
        </if>
        <if test="password != null" >
            and password = #{password}
        </if>
        <if test="salt != null" >
            and salt = #{salt}
        </if>
        <if test="md5Password != null" >
            and md5_password = #{md5Password}
        </if>
        <if test="gender != null" >
            and gender = #{gender}
        </if>
        <if test="consumerLevel != null" >
            and consumer_level = #{consumerLevel}
        </if>
        <if test="status != null" >
            and consumer_status = #{status}
        </if>
    </where>
</select>

<delete id="delete" parameterType="java.lang.Integer">
    update t_consumer set status=0 where id = #{id}
</delete>

<delete id="deleteByIds" parameterType="java.lang.Integer">
    update t_consumer set status=0 where id in
    <foreach collection="ids" item="id" index="index" separator="or" open="(" close=")">
        id = #{id}
    </foreach>
</delete>

<insert id="insert" parameterType="com.wagu.crm.po.data.consumer.Consumer" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    insert into t_consumer (id, consumer_name, icon_url, mobilephone,password, salt, md5_password,
        gender, consumer_level, status)
    values (#{id,jdbcType=INTEGER}, #{consumerName,jdbcType=VARCHAR}, #{iconUrl,jdbcType=VARCHAR},#{mobilephone,jdbcType=VARCHAR},
        #{password,jdbcType=VARCHAR},#{salt,jdbcType=VARCHAR},#{md5Password,jdbcType=VARCHAR},
        #{gender,jdbcType=INTEGER}, #{consumerLevel,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER})
</insert>

<update id="update" parameterType="com.wagu.crm.po.data.consumer.Consumer">
    update t_consumer
    <set>
        <if test="job != null" >
            job = #{job},
        </if>
        <if test="lastLoginTime != null" >
            last_login_time = #{lastLoginTime},
        </if>
        <if test="consumerName != null">
            consumer_name = #{consumerName},
        </if>
        <if test="iconUrl != null">
            icon_url = #{iconUrl},
        </if>
        <if test="mobilephone != null">
            mobilephone = #{mobilephone},
        </if>
        <if test="password != null">
            password = #{password},
        </if>
        <if test="salt != null">
            salt = #{salt},
        </if>
        <if test="md5Password != null">
            md5_password = #{md5Password},
        </if>
        <if test="gender != null">
            gender = #{gender},
        </if>
        <if test="consumerLevel != null">
            consumer_level = #{consumerLevel},
        </if>
        <if test="status != null">
            status = #{status},
        </if>
        <if test="lastModify != null">
            last_modify = now(),
        </if>
    </set>
    where id = #{id}
</update>
</mapper>

相关文章

网友评论

      本文标题:mybatis增删改查mapper文件写法

      本文链接:https://www.haomeiwen.com/subject/gxydoftx.html