<?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="top.jztice5.mapper.PruductMapper">
<!--
注:标签不是select
values后面写的是Brand类中的属性名字
useGeneratedKeys 获取mysql自动生成的主键,自动封装到id属性值中
keyProperty 实体类中主键的属性名 brand中id
keyColumn tb_brand表中主键字段名字 id
-->
<!-- 映射的接口方法 :id后面的方法名 ;框架创建代理对象,当调用映射的接口方法后,代理对象就会找到并执行对应id的sql语句,再将结果集或需要的数据返回到实体类中-->
<!-- 在创建代理对象的时候就会加载映射文件 (mapper.xml)-->
<!-- 添加-->
<insert id="insert" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
insert into tb_brand
values (null, #{brandName}, #{companyName}, #{ordered}, #{description}, #{status})
</insert>
<!-- 修改-->
<update id="update">
update tb_brand
<set>
<if test="brandName !=null and brandName !=''">
brand_name =#{brandName},
</if>
<if test="companyName != null and companyName !=''">
company_name =#{companyName},
</if>
<if test="ordered != null">
ordered=#{ordered},
</if>
<if test="description!=null and description!=''">
description=#{description},
</if>
<if test="status!=null">
status = #{status}
</if>
</set>
<where>
id = #{id}
</where>
</update>
<!-- 删除-->
<delete id="delete">
delete
from tb_brand
where id = #{id}
</delete>
<!-- 批量删除: 数组-->
<delete id="deletes">
delete from tb_brand
where id in
-- 因为数组有别名:ids,如果没有别名,这里是数组的话,默认写 array ;
<foreach collection="ids" open="(" separator="," close=")" item="id">
#{id}
</foreach>
</delete>
<!-- 批量删除 : 集合-->
<delete id="deletesList">
delete from tb_brand
where id in
<foreach collection="collection" open="(" separator="," close=")" item="id">
#{id}
</foreach>
</delete>
<!-- 查询-->
<!--只有查询才需要定义结果集的返回值类型-->
<select id="selectOne" resultType="top.jztice5.pojo.Pruduct">
select *
from tb_brand
where id = #{id};
-- #:的底层是preparedStatement;
-- $:的底层是Statement,会有sql注入异常;(两个不能同时存在)
</select>
//多条件查询:
<select id="selectAll" resultType="top.jztice5.pojo.Pruduct">
select * from tb_brand
<where>
<if test="status != null">
`status`= #{status}
</if>
<if test="brandName != null and brandName !='' ">
AND brand_Name like "%"#{brandName}"%"
</if>
<if test="companyName != null and companyName !='' ">
AND company_Name like "%"#{companyName}"%"
</if>
</where>
</select>
<select id="selects" resultType="top.jztice5.pojo.Pruduct">
select *
from tb_brand;
</select>
<select id="selectList" resultType="top.jztice5.pojo.Pruduct">
select *
from tb_brand
<where>
<if test="status != null">
status = 1
</if>
<if test="companyName != null and companyName !=''">
and company_name = #{companyName}
</if>
<if test="brandName != null and brandName !=''">
and brand_Name = #{brandName}
</if>
</where>
</select>
</mapper>