-
SQL语句中特殊字段处理(转义字符 同xml)
-
多条件查询当功能有多个参数时,使用@Param
("参数名称")
标记每一个参数,在映射配置文件中就需要使用 #{参数名称} 进行占位,也可以将多个参数封装成对象(里面的内容必须和实体类属性名保持一致 )或者map集合的形式(里面的内容必须和map集合中键的名称一致 )List<Brand> selectByCondition(@Param("status") int status, @Param("companyName") String companyName,@Param("brandName") String brandName);
<select id="selectByCondition" resultType="brand"> select * from tb_brand where status = #{status} and companyName like #{companyName} and brandName like #{brandName} </select>
- 动态SQL---(了解mybatis高级特性!!!有难度)
# <where>标签会自动去掉第一个条件的 and select * from tb_brand <where> <if test="status!=null"> #if(status!=null)//有status条件 status=? </if> <if test="brandName!=null and brandName!=''" > #品牌名称写了必须不是空字符串 and brandName=? </if> <if test="companyName!=null and companyName!=''" > #品牌名称写了必须不是空字符串 and companyName=? </if> </where> <if>标签作用: 表示条件判断 test属性里面写判断语句 符合条件就拼接 不符合不拼接 --形成动态条件形式了--动态sql <where> 标签作用 替代了where 后面有条件有where 没条件没where 后面条件的第一个and会自动去除
-
添加-主键返回 (这种思想要理解)
<!-- /* 完成 添加操作 */ void add(Brand brand); 需要做主键回显 就需要两个属性 写出来 useGeneratedKeys 是否要获取自增主键的值。 keyProperty 把这个主键的值 给谁?给哪个属性信息 --> <insert id="add" useGeneratedKeys="true" keyProperty="id"> insert into tb_brand values(null,#{brandName},#{companyName},#{ordered},#{description},#{status}) </insert>
- 修改
<update id="update"> update tb_brand set brandName = #{brandName}, companyName = #{companyName}, ordered = #{ordered}, description = #{description}, status = #{status} where id =#{id} </update>
-
批量删除(扩展讲解)
/* 设计 根据多个id删除的方法 参数 int[] 数组里面放置多个id 返回值 void */ void deleteByIds(int[] ids);
<!-- void deleteByIds(int[] ids); 根据多个id删除多条数据 假如 int[] ids = {1,2,4} {3,5,6,9} delete from tb_brand where id=ids; delete from tb_brand where id in(1,2,4); delete from tb_brand where id in(#{ids[0]},#{ids[1],#{ids[2]}); 问题是 (1,2,4)怎么给搞出来 (3,5,6,9) 3,5,6,9 <foreach> 完成数组或集合的遍历 属性 collection 被遍历容器是谁 数组 写 array 集合 list open 遍历之前拼接点啥 close 遍历之后拼接点啥 separator 元素之间的分隔符 item 定义被遍历出来的元素的接收变量名 比如 item="id" id就是被遍历出来的数字 #{id} 把每个元素拿出来进行拼接 --> <delete id="deleteByIds"> delete from tb_brand where id in <foreach collection="array" open="(" close=")" separator="," item="id"> #{id} </foreach> </delete>
Mybatis练习---mybatis 代理形式
最新推荐文章于 2023-10-17 10:05:24 发布