使用mybatis时,dao层mapper文件的基本示例

dao层mapper文件的示例

实体类 Brand:

package cn.itcast.core.bean.product;

import java.io.Serializable;

public class Brand implements Serializable {

    private static final long serialVersionUID = 1L;
    // 品牌ID bigint
    private Long id;
    // 品牌名称
    private String name;
    // 描述
    private String description;
    // 图片URL
    private String imgUrl;
    // 排序 越大越靠前
    private Integer sort;
    // 是否可用 0 不可用 1 可用
    private Integer isDisplay;// is_display
    }

dao

package cn.itcast.core.dao.product;

import java.util.List;

import cn.itcast.core.bean.product.Brand;
import cn.itcast.core.bean.product.BrandQuery;

/**
 * 查询
 * @author lx
 *
 */
public interface BrandDao {


    //查询结果集
    public List<Brand> selectBrandListByQuery(BrandQuery brandQuery);
    //查询总条数
    public Integer selectCount(BrandQuery brandQuery);
    //通过ID查询品牌
    public Brand selectBrandById(Long id);

    //修改
    public void updateBrandById(Brand brand);

    //删除
    public void deletes(Long[] ids);// List<Long> ids
}

mapper 文件

<?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="cn.itcast.core.dao.product.BrandDao">
    <resultMap type="Brand" id="brand">
        <result column="img_url" property="imgUrl"/>
        <result column="is_display" property="isDisplay"/>
    </resultMap>
    <!-- 查询 -->
    <select id="selectBrandListByQuery" parameterType="BrandQuery" resultMap="brand">
        select id , name ,description,img_url,sort,is_display
        from bbs_brand
        <where>
            <if test="name != null">
                name like "%"#{name}"%"
            </if>
            <if test="isDisplay != null">
                and is_display = #{isDisplay}
            </if>
        </where>
        <if test="startRow != null">
            limit #{startRow},#{pageSize}
        </if>
    </select>
    <!-- 查询总条数-->
    <select id="selectCount" parameterType="BrandQuery" resultType="Integer">
        select count(1)
        from bbs_brand
        <where>
            <if test="name != null">
                name like "%"#{name}"%"
            </if>
            <if test="isDisplay != null">
                and is_display = #{isDisplay}
            </if>
        </where>
    </select>
    <!-- 通过ID查询 -->
    <select id="selectBrandById" parameterType="Long" resultMap="brand">
        select id , name ,description,img_url,sort,is_display
        from bbs_brand
        <where>
            id = #{id}
        </where>
    </select>
    <!-- 修改 -->
    <update id="updateBrandById" parameterType="Brand">
        update bbs_brand
        <set>
            <if test="name != null">
                name = #{name},
            </if>
            <if test="description != null">
                description = #{description},
            </if>
            <if test="imgUrl != null">
                img_url = #{imgUrl},
            </if>
            <if test="sort != null">
                sort = #{sort},
            </if>
            <if test="isDisplay != null">
                is_display = #{isDisplay}
            </if>
        </set>
        <where>
            id = #{id}
        </where>
    </update>

    <!-- 删除 (1,2,3,4) -->
    <delete id="deletes" parameterType="Long">
        delete from bbs_brand
        <where>
            id in 
            <foreach collection="array" item="id" separator="," open="(" close=")">
                #{id}
            </foreach>
        </where>
    </delete>

</mapper>

关于mapper文件写法和结构的简单介绍:
这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值