<?xml version="1.0" encoding="UTF-8" ?>标签:status,01,Java,name,brand,company,tb,id From: https://www.cnblogs.com/bzsc/p/17875025.html
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--参数占位符#{} :执行SQL时,会将 #{} 占位符替换为?,将来自动设置参数值。从上述例子可以看出使用#{} 底层使用的是 PreparedStatement-->
<!-- 转意字符 < 就是 < 的转义字符。 -->
<!-- 还可以用!<![CDATA[ -->
<!-- < -->
<!-- ]]> 来表达转意字符 -->
<!--namespace名称空间-->
<mapper namespace="com.itheima.mapper.BrandMapper">
<!--查询数据库全部信息-->
<select id="selectAll" resultType="brand">
select *
from tb_brand;
</select>
<!--查询id为几的全部信息-->
<select id="selectById" resultType="com.itheima.pojo.Brand">
select *
from tb_brand where id = #{id};
</select>
<!--模糊查询,多条件查询-->
<select id="selectByCondition" resultType="com.itheima.pojo.Brand">
select *
from tb_brand
<where>
<if test="status != null">
and status = #{status}
</if>
<if test="company_name != null and company_name != '' ">
and company_name like #{company_name}
</if>
<if test="brand_name != null and brand_name != ''">
and brand_name like #{brand_name}
</if>
</where>
</select>
<!--单条件查询-->
<select id="selectByConditionSingle" resultType="com.itheima.pojo.Brand">
select *
from tb_brand
<where>
<choose><!--相当于switch-->
<when test="status != null"><!--相当于case-->
status = #{status}
</when>
<when test="company_name != null and company_name != '' "><!--相当于case-->
company_name like #{company_name}
</when>
<when test="brand_name != null and brand_name != ''"><!--相当于case-->
brand_name like #{ brand_name}
</when>
</choose>
</where>
</select>
<!--添加-->
<!-- 这个sql语句没用,与本例子无关,是关联到第二个表<<添加订单>>,一个订单又多个商品信息,主键id传给后面的商品信息-->
<insert id="addOrder" useGeneratedKeys="true" keyProperty="id">
insert into tb_order (payment,payment_type,status)
values (#{payment}, #{payment_type}, #{status});
</insert>
<!-- 这个sql语句没用,与本例子无关,是关联到第一个表<<添加该订单下的商品信息>>,获得前面的id-->
<insert id="addOrderItem" >
insert into tb_order_item (goods_name,goods_price,count,order_id)
values (#{goods_name}, #{goods_price}, #{count},#{order_id});
</insert>
<insert id="add" useGeneratedKeys="true" keyProperty="id">
insert into tb_brand (brand_name, company_name, ordered, description, status)
values (#{brand_name}, #{company_name}, #{ordered}, #{description}, #{status});
</insert>
<!--修改-->
<update id="update">
update tb_brand
<set>
<if test="brand_name != null and brand_name != ''">
brand_name = #{brand_name},
</if>
<if test="company_name != null and company_name != ''">
company_name = #{company_name},
</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};
</update>
<!--删除-->
<delete id="deleteById">
delete from tb_brand where id = #{id};
</delete>
</mapper>