公司網(wǎng)站制作公司排名網(wǎng)絡(luò)營銷推廣策略有哪些
if標簽與where標簽
if標簽
test如果為true就會拼接查詢條件,否則不會
- 當(dāng)沒有使用@Param,test出現(xiàn)arg0/param1
- 當(dāng)使用@Param,test為@Param指定的值
- 當(dāng)使用Pojo,test為對象的屬性名
select * from car where
<if test="name!=null || name!='' ">name like concat('%',${name},'%')
</if>
<if test="price!=null || price!='' ">and price=#{price}
</if>
...
注意日期不能判斷為空字符串
如何所有條件都不滿足上述代碼會報錯
解決
如果用 where 1=1 后面判斷必須加and
select * from car where 1=1
<if test="name!=null || name!='' ">and name like concat('%',${name},'%')
</if>
<if test="price!=null || price!='' ">and price=#{price}
</if>
...
where標簽
- 所有條件都為空時,where子句不會生成
- 自動去掉前面多余的and,or
select * from car
<where><if test="name!=null || name!='' ">name like concat('%',${name},'%')</if><if test="price!=null || price!='' ">and price=#{price}</if>
</where>
...
trim標簽
- prefix在標簽前面動態(tài)的添加屬性值
- suffix在標簽后面動態(tài)的添加屬性值
- suffixOverrides去除標簽內(nèi)容后面中指定的屬性值
- prefixOverrides去除標簽內(nèi)容前面中指定的屬性值
select * from car
<trim prefix="where" suffixOverrides="and | or" prefixOverrides="" suffix=""><if test="name!=null || name!='' ">name like concat('%',${name},'%')</if><if test="price!=null || price!='' ">and price=#{price}</if>
</trim>
set標簽
- 主要用在update標簽中,只會提交不為空的條件
- 可以動態(tài)去除語句中多余的,
update car
<set><if test="name!=null || name!='' ">name=#{name} , </if><if test="price!=null || price!='' ">price=#{price} ,</if>
</set>
where id=#{id}
choose when otherwise標簽
一般在多條件中只執(zhí)行某一個條件查詢
用法類似與 if else if else
selecr * from car
<where><choose><when test="name!=null || name!='' ">name=#{name} </when><when test="price!=null || price!='' ">price=#{price} </when><otherwise>id=#{id} <otherwise></choose>
<where>
因為只會滿足一種查詢條件所有不需要加and
forEach標簽
- collection為循環(huán)列表
- item為循環(huán)元素
- separator為循環(huán)元素之間的分隔符
- open為標簽前面加屬性值
- close為標簽后面加屬性值
批量刪除
delete from car where id in(<foreach collection="ids" item="item" separator=",">#{item}</foreach>
)delete from car where id in<foreach collection="ids" item="item" open="(" separator="," close=")">#{item}</foreach>delete from car where <foreach collection="ids" item="item" separator="or" >id=#{item}</foreach>
批量插入
insert into car( name, price)
values<foreach collection="list" item="item" separator=",">(#{item.name},#{item.price})</foreach>
sql與include標簽
主要用于字段的封裝和復(fù)用
<sql id="CarSql">id,car_num as carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
</sql><select id="selectAll" resultType="com.example.webapplication.pojo.Car">select<include refid="CarSql"></include>from car
</select>