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文件写法和结构的简单介绍: