mybatis 脚本处理语句(条件查询,批量增删改查)
2017-06-21 10:11
429 查看
模糊查询:
批量添加:
批量删除:
批量修改:
批量查询:
条件查询,if里面不仅可以判空,还可以判断是否满足某个条件
条件查询:
@Select({ "SELECT * FROM account where account like CONCAT('%',#{query},'%') or email like CONCAT('%',#{query},'%')" }) Account findAccountByAccountOrMail(@Param("query") String query);
批量添加:
@Insert({ "<script>" + "INSERT INTO company_label(company_id,label_id) values " + " <foreach collection=\"item\" item=\"item\" index=\"index\" separator=\",\" > " + " (#{companyId},#{item}) " + " </foreach>" + "</script>" }) void insertLabelForCompany(@Param("companyId") Long companyId,@Param("item") List<Long> item);
批量删除:
@Delete({ "<script>delete from company_label where company_id = #{companyId} and label_id in " + "<foreach collection = \"item\" item = \"item\" open=\"(\" separator=\",\" close=\")\">" + "#{item}" + "</foreach>" + "</script>" }) void removeLabelForCompany(@Param("companyId") Long companyId,@Param("item") List<Long> item);
批量修改:
@Update(value = "<script>" + "update banner b set b.display = #{status} where b.id in "+ "<foreach item = 'item' index = 'index' collection = 'ids' open = '(' separator = ',' close = ')'>#{item}</foreach>" + "" + "</script>") int updateStatus(@Param("status") Long status, @Param("ids") Long[] ids);
批量查询:
@Select({ "<script>" + "select * from product where id in" + "<foreach item = 'item' index = 'index' collection = 'idList' open = '(' separator = ',' close = ')'>#{item}</foreach>" + "</script>" }) List<Product> findByIdList(@Param("idList")List<Long> idList);
条件查询,if里面不仅可以判空,还可以判断是否满足某个条件
@Select({ "<script>SELECT * FROM company where 1=1 and parent_id = #{companyId} " + //平级 "<if test = \"isScanSameLevelValue == 1\">and type = #{type}</if>" + "<if test = \"isScanSameLevelValue == 0\">and type != #{type}</if>" + "</script> " }) List<Company> findCompanyConditional(@Param("isScanSameLevelValue") String isScanSameLevelValue, @Param("isScanParentLevelValue") String isScanParentLevelValue, @Param("companyId") Long companyId, @Param("type") Integer type);
条件查询:
*/ @Lang(XMLLanguageDriver.class) @Select({"<script>select DISTINCT p.* FROM `us_product`.`hot_category_surgery` hcs "+ "LEFT JOIN `us_product`.`product` p ON hcs.`product_id` =p.`id`"+ "LEFT JOIN `us_product`.`category_surgery` cs on cs.`product_id` =p.`id`"+ "LEFT JOIN `us_product`.`merchant_product` mp on mp.`product_id` = p.`id`"+ "LEFT JOIN `us_product`.`org_product` op on op.`product_id` =p.`id`"+ "where p.`type` =1 and p.`is_for_sale` =1 "+ " <if test=\"hId != null\"> and hcs.hot_category_id = #{hId} and p.id = hcs.product_id</if>" + //热门类目id " <if test=\"categoryId != null\"> and cs.category_id = #{categoryId} and p.id = cs.product_id</if>" + //类目id " <if test=\"input != null\"> and (p.name like CONCAT('%',#{input},'%') or p.company like CONCAT('%',#{input},'%')) </if> "+ //用户输入,包括商品名和店铺名,模糊 " <if test = \" location != null\"> and p.location like CONCAT('%',#{location},'%') </if> "+ //位置.. " <if test=\"method != null\"> and mp.filter_id = #{method} and p.id = mp.product_id</if> "+ //筛选条件 手术方式 " <if test=\"org != null\"> and op.filter_id = #{org} and p.id = op.product_id</if> "+ //筛选条件 所属机构 " ORDER BY sale_volume DESC"+ " </script>" }) List<Product> findProductFromLocal(@Param("hId")Long hId,@Param("categoryId")Long categoryId,@Param("input")String input,@Param("method")Long method,@Param("org")Long org,@Param("location")String location);
相关文章推荐
- Mybatis 条件查询 批量增删改查功能
- Mybatis查询语句中的条件判断问题
- 资料 mybatis 一条语句 分页查询(同时获得符合条件的总记录数)
- MyBatis中动态SQL语句完成多条件查询
- 关于linq语句与sql语句查询条件==null处理的奇妙现象
- SQL 查询语句 IIF()处理条件判断
- 关于mybatis增删改查时条件为一个集合时如何利用foreach书写sql语句
- mybatis foreach in查询语句 关于逗号 空值处理问题
- 当某个参数不传入值时,查询语句中where后面不加相应条件处理
- 使用MyBatis对项目中的统计功能进行处理的查询语句优化
- mybatis处理集合、循环、数组和in查询等语句的使用
- 动态生成SQL查询条件定制表单及语句的脚本
- mybatis使用in语句作为查询条件
- mybatis 语句中where 后边要跟必要条件和多个选择条件处理方法
- MyBatis中动态SQL语句完成多条件查询
- mybatis逆向工程进行不同条件查询的应用处理
- mybatis处理集合、循环、数组和in查询等语句的使用
- 在mybatis中处理集合、循环、数组和in查询等语句的使用
- Mybatis 不确定条件查询(动态语句where和if)借助实体类封装参数
- mybatis分页多条件查询指定时间段数据的sql语句