mapper增删改查样板(User,Cart)

mapper增删改查样板

笔记

User

package com.chinasofti.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.io.Serializable;

/**
 * 用户表 user
 *
 * @author
 * @date 2019-08-01
 */
public class User implements Serializable
{
	private static final long serialVersionUID = 1L;

	/**  */
	private Long id;
	/** 用户名 */
	private String username;
	/** 密码,加密存储 */
	@JsonIgnore
	private String password;
	/** 注册手机号 */
	private String phone;
	/** 注册邮箱 */
	private String email;

	public void setId(Long id)
	{
		this.id = id;
	}

	public Long getId()
	{
		return id;
	}
	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getUsername()
	{
		return username;
	}
	public void setPassword(String password)
	{
		this.password = password;
	}

	public String getPassword()
	{
		return password;
	}
	public void setPhone(String phone)
	{
		this.phone = phone;
	}

	public String getPhone()
	{
		return phone;
	}
	public void setEmail(String email)
	{
		this.email = email;
	}

	public String getEmail()
	{
		return email;
	}

}

UserMapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.chinasofti.manager.mapper.IUserMapper">
    <resultMap id="UserResult" type="com.chinasofti.domain.User">
        <result property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <result property="phone" column="phone"/>
        <result property="email" column="email"/>
    </resultMap>

    <sql id="selectUserVo">
        select id,username,password,phone,email from user
    </sql>

    <!-- 根据ID进行查询 -->
    <select id="selectUserById" parameterType="Long" resultMap="UserResult">
        <include refid="selectUserVo"/>
        where id = #{id}
    </select>
    
    <!-- 根据条件查询 -->
    <select id="selectUserList" parameterType="com.chinasofti.domain.User" resultMap="UserResult">
        <include refid="selectUserVo"/>
        <where>
            <if test="id!=null">and id=#{id}</if>
            <if test="username!=null and username!=''">and username=#{username}</if>
            <if test="password!=null and password!=''">and password=#{password}</if>
            <if test="phone!=null and phone!=''">and phone=#{phone}</if>
            <if test="email!=null and email!=''">and email=#{email}</if>
        </where>
    </select>

    <!-- 增加 -->
    <insert id="insertUser" parameterType="com.chinasofti.domain.User">
        insert into user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id!=null">id,</if>
            <if test="username!=null and username!=''">username,</if>
            <if test="password!=null and password!=''">password,</if>
            <if test="phone!=null and phone!=''">phone,</if>
            <if test="email!=null and email!=''">email,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id!=null">#{id},</if>
            <if test="username!=null and username!=''">#{username},</if>
            <if test="password!=null and password!=''">#{password},</if>
            <if test="phone!=null and phone!=''">#{phone},</if>
            <if test="email!=null and email!=''">#{email},</if>
        </trim>
    </insert>

    <!-- 根据id值进行删除 -->
    <delete id="deleteUserById" parameterType="Long">
        delete from user where id=#{id}
    </delete>

    <!-- 修改用户 -->
    <update id="updateUser" parameterType="com.chinasofti.domain.User">
        update user
        <trim prefix="set" suffixOverrides=",">
            <if test="username !=null and username !=''">username =#{username},</if>
            <if test="password !=null and password !=''">password =#{password},</if>
            <if test="phone !=null and phone !=''">phone =#{phone},</if>
            <if test="email !=null and email !=''">email =#{email},</if>
        </trim>
        where id=#{id}
    </update>
    
    <!-- 根据ids 进行批量删除 -->
    <delete id="deleteUserByIds" parameterType="java.lang.String">
        delete from user where id in
        <foreach collection="array" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>

Cart

package com.chinasofti.domain;


import java.io.Serializable;
import java.util.Date;

/**
 * 购物车表 cart
 *
 * @author
 * @date 2019-08-01
 */
public class Cart implements Serializable
{
	private static final long serialVersionUID = 1L;

	/** 自增ID */
	private Long id;
	/** 用户ID */
	private Long userId;
	/** 商品ID */
	private Long productId;
	/** 商品标题 */
	private String productTitle;
	/** 商品主图 */
	private String productImage;
	/** 商品价格,单位为:分 */
	private Long productPrice;
	/** 购买数量 */
	private Integer num;
	/**  */
	private Date created;
	/**  */
	private Date updated;

	public void setId(Long id)
	{
		this.id = id;
	}

	public Long getId()
	{
		return id;
	}
	public void setUserId(Long userId)
	{
		this.userId = userId;
	}

	public Long getUserId()
	{
		return userId;
	}
	public void setProductId(Long productId)
	{
		this.productId = productId;
	}

	public Long getProductId()
	{
		return productId;
	}
	public void setProductTitle(String productTitle)
	{
		this.productTitle = productTitle;
	}

	public String getProductTitle()
	{
		return productTitle;
	}
	public void setProductImage(String productImage)
	{
		this.productImage = productImage;
	}

	public String getProductImage()
	{
		return productImage;
	}
	public void setProductPrice(Long productPrice)
	{
		this.productPrice = productPrice;
	}

	public Long getProductPrice()
	{
		return productPrice;
	}
	public void setNum(Integer num)
	{
		this.num = num;
	}

	public Integer getNum()
	{
		return num;
	}
	public void setCreated(Date created)
	{
		this.created = created;
	}

	public Date getCreated()
	{
		return created;
	}
	public void setUpdated(Date updated)
	{
		this.updated = updated;
	}

	public Date getUpdated()
	{
		return updated;
	}

}

CartMapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.chinasofti.mapper.CartMapper">

    <resultMap type="Cart" id="CartResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="userId"    />
        <result property="productId"    column="productId"    />
        <result property="productTitle"    column="productTitle"    />
        <result property="productImage"    column="productImage"    />
        <result property="productPrice"    column="productPrice"    />
        <result property="num"    column="num"    />
        <result property="created"    column="created"    />
        <result property="updated"    column="updated"    />
    </resultMap>

	<sql id="selectCartVo">
        select id, userId, productId, productTitle, productImage, productPrice, num, created, updated from cart
    </sql>

    <select id="selectCartList" parameterType="Cart" resultMap="CartResult">
        <include refid="selectCartVo"/>
        <where>
            <if test="id != null "> and id = #{id}</if>
             <if test="userId != null "> and userId = #{userId}</if>
             <if test="productId != null "> and productId = #{productId}</if>
             <if test="productTitle != null  and productTitle != '' "> and productTitle = #{productTitle}</if>
             <if test="productImage != null  and productImage != '' "> and productImage = #{productImage}</if>
             <if test="productPrice != null "> and productPrice = #{productPrice}</if>
             <if test="num != null "> and num = #{num}</if>
             <if test="created != null "> and created = #{created}</if>
             <if test="updated != null "> and updated = #{updated}</if>
         </where>
    </select>

    <select id="selectCartById" parameterType="Long" resultMap="CartResult">
        <include refid="selectCartVo"/>
        where id = #{id}
    </select>

    <insert id="insertCart" parameterType="Cart" useGeneratedKeys="true" keyProperty="id">
        insert into cart
		<trim prefix="(" suffix=")" suffixOverrides=",">
			<if test="userId != null  ">userId,</if>
			<if test="productId != null  ">productId,</if>
			<if test="productTitle != null  and productTitle != ''  ">productTitle,</if>
			<if test="productImage != null  and productImage != ''  ">productImage,</if>
			<if test="productPrice != null  ">productPrice,</if>
			<if test="num != null  ">num,</if>
			<if test="created != null  ">created,</if>
			<if test="updated != null  ">updated,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
			<if test="userId != null  ">#{userId},</if>
			<if test="productId != null  ">#{productId},</if>
			<if test="productTitle != null  and productTitle != ''  ">#{productTitle},</if>
			<if test="productImage != null  and productImage != ''  ">#{productImage},</if>
			<if test="productPrice != null  ">#{productPrice},</if>
			<if test="num != null  ">#{num},</if>
			<if test="created != null  ">#{created},</if>
			<if test="updated != null  ">#{updated},</if>
         </trim>
    </insert>

    <update id="updateCart" parameterType="Cart">
        update cart
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null  ">userId = #{userId},</if>
            <if test="productId != null  ">productId = #{productId},</if>
            <if test="productTitle != null  and productTitle != ''  ">productTitle = #{productTitle},</if>
            <if test="productImage != null  and productImage != ''  ">productImage = #{productImage},</if>
            <if test="productPrice != null  ">productPrice = #{productPrice},</if>
            <if test="num != null  ">num = #{num},</if>
            <if test="created != null  ">created = #{created},</if>
            <if test="updated != null  ">updated = #{updated},</if>
        </trim>
        where id = #{id}
    </update>

	<delete id="deleteCartById" parameterType="Long">
        delete from cart where id = #{id}
    </delete>

    <delete id="deleteCartByIds" parameterType="String">
        delete from cart where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值