美文网首页
spring整合myBatis常用增删改查

spring整合myBatis常用增删改查

作者: zain丶月下 | 来源:发表于2020-01-08 18:18 被阅读0次

参考文章:https://blog.csdn.net/hellozpc/article/details/80878563

在这里就不介绍myBatis是干什么的了。自行百度查询。这里只告诉如何使用。
这里粘贴项目中用到的实体类

set/get方法省略
public class Product {
    // 主键ID
    private Long productId;
    // 商品名
    private String productName;
    // 商品简介
    private String productDesc;
    // 简略图
    private String imgAddr;
    // 原价
    private String normalPrice;
    // 现价(推广价格)
    private String promotionPrice;
    // 权重,越大越排前显示
    private Integer priority;
    // 商品积分
    private Integer point;
    // 创建时间
    private Date createTime;
    // 最近一次的更新时间
    private Date lastEditTime;
    // 0.下架 1.在前端展示系统展示
    private Integer enableStatus;

    // 图片详情图列表,跟商品是多对一的关系
    private List<ProductImg> productImgList;
    // 商品类别,一件商品仅属于一个商品类别
    private ProductCategory productCategory;
    // 店铺实体类,标明商品属于哪个店铺
    private Shop shop;
  }
1.1 select

select中的几个属性说明:
id属性:当前名称空间下的statement的唯一标识。必须。要求id和mapper接口中的方法的名字一致。
resultType:将结果集映射为java的对象类型。必须(和 resultMap 二选一)
parameterType:传入参数类型。可以省略

接口方法定义
    /**
     * 查询商品列表并分页,可输入的条件有: 商品名(模糊),商品状态,店铺Id,商品类别
     * @param productCondition
     * @param rowIndex
     * @param pageSize
     * @return
     */
    List<Product> queryProductList(@Param("productCondition")Product productCondition,@Param("rowIndex")int rowIndex,
            @Param("pageSize")int pageSize);
xml文档编写
<resultMap id="productMap" type="com.tzf.o2o.entity.Product">
        <id column="product_id" property="productId" />
        <result column="product_name" property="productName" />
        <result column="product_desc" property="productDesc" />
        <result column="img_addr" property="imgAddr" />
        <result column="normal_price" property="normalPrice" />
        <result column="promotion_price" property="promotionPrice" />
        <result column="priority" property="priority" />
        <result column="create_time" property="createTime" />
        <result column="last_edit_time" property="lastEditTime" />
        <result column="enable_status" property="enableStatus" />
        <result column="point" property="point" />
        <association property="productCategory" column="product_category_id"
            javaType="com.tzf.o2o.entity.ProductCategory">
            <id column="product_category_id" property="productCategoryId" />
            <result column="product_category_name" property="productCategoryName" />
        </association>
        <association property="shop" column="shop_id"
            javaType="com.tzf.o2o.entity.Shop">
            <id column="shop_id" property="shopId" />
            <result column="owner_id" property="ownerId" />
            <result column="shop_name" property="shopName" />
        </association>
        <collection property="productImgList" column="product_id"
            ofType="com.tzf.o2o.entity.ProductImg">
            <id column="product_img_id" property="productImgId" />
            <result column="detail_img" property="imgAddr" />
            <result column="img_desc" property="imgDesc" />
            <result column="priority" property="priority" />
            <result column="create_time" property="createTime" />
            <result column="product_id" property="productId" />
        </collection>
    </resultMap>
    <select id="queryProductList" resultType="com.tzf.o2o.entity.Product">
        select 
        product_id,
        product_name,
        product_desc,
        img_addr,
        normal_price,
        promotion_price,
        priority,
        create_time,
        last_edit_time,
        enable_status,
        point,
        product_category_id,
        shop_id
        FROM
        tb_product
        <where>
            <if test="productCondition.shop!=null and productCondition.shop.shopId != null">
                and shop_id = #{productCondition.shop.shopId}
            </if>
            <if test="productCondition.productCategory!=null 
            and productCondition.productCategory.productCategoryId!=null">
                and product_category_id = #{productCondition.productCategory.productCategoryId}
            </if>
                    <!-- 写like语句的时候 一般都会写成 like '% %' 在mybatis里面写就是应该是 like '%${name} %' 而不是 
            '%#{name} %' ${name} 是不带单引号的,而#{name} 是带单引号的 -->
            <if test="productCondition.productName!=null">
                and product_name like '%${productCondition.productName}%'
            </if>
            <if test="productCondition.enableStatus!=null">
                and enable_status = #{productCondition.enableStatus}
            </if>
        </where>
        ORDER BY
        priority DESC
        limit #{rowIndex},#{pageSize};
    </select>
1.2 insert

insert 的几个属性说明:
id:唯一标识,随便写,在同一个命名空间下保持唯一,使用动态代理之后要求和方法名保持一致
parameterType:参数的类型,使用动态代理之后和方法的参数类型一致
useGeneratedKeys:开启主键回写
keyColumn:指定数据库的主键
keyProperty:主键对应的pojo属性名

接口方法定义
    /**
     * 插入商品
     * @param product
     * @return
     */
    int insertProduct(Product product);
xml文档编写
<insert id="insertProduct" parameterType="com.tzf.o2o.entity.Product"
     useGeneratedKeys="true" keyProperty="productId" keyColumn="product_id">
        INSERT INTO
        tb_product(product_name,product_desc,img_addr,
        normal_price,promotion_price,priority,create_time,
        last_edit_time,enable_status,point,product_category_id,
        shop_id)
        VALUES
        (#{productName},#{productDesc},#{imgAddr},
        #{normalPrice},#{promotionPrice},#{priority},#{createTime},
        #{lastEditTime},#{enableStatus},#{point},#{productCategory.productCategoryId},
        #{shop.shopId})
    </insert>
1.3 update

delete 的几个属性说明:
id属性:当前名称空间下的statement的唯一标识(必须属性);
parameterType:传入的参数类型,可以省略。

接口方法定义
    /**
     * 更新商品信息
     * @param product
     * @return
     */
    int updateProduct(Product product);
xml文档编写
<update id="updateProduct" parameterType="com.tzf.o2o.entity.Product"
            keyProperty="product_id" useGeneratedKeys="true">   
        update tb_product
        <set>
            <if test="productName != null">product_name=#{productName},</if>
            <if test="productDesc != null">product_desc=#{productDesc},</if>
            <if test="imgAddr != null">img_addr=#{imgAddr},</if>
            <if test="normalPrice != null">normal_price=#{normalPrice},</if>
            <if test="promotionPrice != null">promotion_price=#{promotionPrice},</if>
            <if test="priority != null">priority=#{priority},</if>
            <if test="lastEditTime != null">last_edit_time=#{lastEditTime},</if>
            <if test="enableStatus != null">enable_status=#{enableStatus},</if>
            <if test="point != null">point=#{point},</if>
            <if
                test="productCategory != null
                 and productCategory.productCategoryId != null">
                product_category_id=#{productCategory.productCategoryId}
            </if>
        </set>
        WHERE product_id = #{productId}
        AND shop_id=#{shop.shopId}
    </update>
1.4 delete

delete 的几个属性说明:
id属性:当前名称空间下的statement的唯一标识(必须属性);
parameterType:传入的参数类型,可以省略。

接口方法定义(其他接口展示)
    /**
     * 删除指定商品类别
     * 
     * @param productCategoryId
     * @param shopId
     * @return effectedNum
     */
    int deleteProductCategory(@Param("productCategoryId") long productCategoryId, @Param("shopId") long shopId);
xml文档编写
<delete id="deleteProductCategory">
        delete from 
        tb_product_category
        where  product_category_id = #{productCategoryId} 
        and shop_id = #{shopId}
    </delete>

相关文章

网友评论

      本文标题:spring整合myBatis常用增删改查

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