当前位置: 首页 > news >正文

推荐常州网站建设公司如何选择大良网站建设

推荐常州网站建设公司,如何选择大良网站建设,杭州网站开发工程师,seo公司推荐推广平台动态SQL常用场景 批量删除delete from t_car where id in(1,2,3,4,5,6,......这里的值是动态的,根据用户选择的 id不同,值是不同的);多条件查询哪些字段会作为查询条件是不确定的,根据用户而定 select * from 1 t_car where brand like 丰田…

动态SQL常用场景

  • 批量删除
    delete from t_car where id in(1,2,3,4,5,6,......这里的值是动态的,根据用户选择的
    id不同,值是不同的);
    
  • 多条件查询
    哪些字段会作为查询条件是不确定的,根据用户而定
    select * from 1 t_car where brand like '丰田%' and guide_price > 30 and .....;
    
  1. if 标签
    List<Car> selectByMultiCondition(@Param("brand") String brand, @Param("guidePrice") Double guidePrice, @Param("carType") String carType);
    
    <select id="selectByMultiCondition" resultType="car">
    select * from t_car where
    <if test="brand != null and brand != ''">
    brand like #{brand}"%"
    </if>
    <if test="guidePrice != null and guidePrice != ''">
    and guide_price >= #{guidePrice}
    </if>
    <if test="carType != null and carType != ''">
    and car_type = #{carType}
    </if>
    </select>
    
    存在一些连接词的问题例如 and 和 or,当brand 为空时可能出现 select * from t_car where and…这种语法报错情况,所以需要结合其他 标签使用
  2. where 标签
    <select id="selectByMultiConditionWithWhere" resultType="car">
    select * from t_car
    <where>
    <if test="brand != null and brand != ''">
    and brand like #{brand}"%"
    </if>
    <if test="guidePrice != null and guidePrice != ''">
    and guide_price >= #{guidePrice}
    </if>
    <if test="carType != null and carType != ''">
    and car_type = #{carType}
    </if>
    </where>
    </select>
    
    解决了if 标签存在的一些问题,where标签的作用:让where子句更加动态智能。
    所有条件都为空时,where标签保证不会生成where子句。
    自动去除某些条件前面多余的and或or,注意后面的 and 和 or 是不会被去除的
  3. trim 标签
    trim标签的属性:
    • prefix:在 SQL 语句的开头添加指定的前缀。
    • suffix:在 SQL 语句的结尾添加指定的后缀。
    • prefixOverrides:去掉 SQL 语句开头的指定前缀。
    • suffixOverrides:去掉 SQL 语句结尾的指定后缀。
    <select id="selectByMultiConditionWithTrim" resultType="car">
    select * from t_car
    <trim prefix="where" suffixOverrides="and|or">
    <if test="brand != null and brand != ''">
    brand like #{brand}"%" and
    </if>
    <if test="guidePrice != null and guidePrice != ''">
    guide_price >= #{guidePrice} and
    </if>
    <if test="carType != null and carType != ''">
    car_type = #{carType}
    </if>
    </trim>
    </select>
    
    所有条件为空时,不会添加前缀,比where标签更加灵活,可以去除结尾的连接词
  4. set标签
    主要使用在update语句当中,用来生成set关键字,同时去掉最后多余的“,”
    int updateWithSet(Car car);
    
    <update id="updateWithSet">
    update t_car
    <set>
    <if test="carNum != null and carNum != ''">car_num = #{carNum},</if>
    <if test="brand != null and brand != ''">brand = #{brand},</if>
    <if test="guidePrice != null and guidePrice != ''">guide_price = #{gui
    dePrice},</if>
    <if test="produceTime != null and produceTime != ''">produce_time = #
    {produceTime},</if>
    <if test="carType != null and carType != ''">car_type = #{carType},</i
    f>
    </set>
    where id = #{id}
    </update>
    
  5. choose when otherwise
    这三个标签是一起使用的,类似于 if else 嵌套选择
    <select id="selectWithChoose" resultType="car">
    select * from t_car
    <where>
    <choose>
    <when test="brand != null and brand != ''">
    brand like #{brand}"%"
    </when>
    <when test="guidePrice != null and guidePrice != ''">
    guide_price >= #{guidePrice}
    </when>
    <otherwise>
    produce_time >= #{produceTime}
    </otherwise>
    </choose>
    </where>
    </select>
    
  6. foreach 标签
    循环数组或集合,动态生成sql,比如这样的SQL:
    • 用 in 实现批量删除
      int deleteBatchByForeach(@Param("ids") Long[] ids);
      
      <!--
      collection:集合或数组
      item:集合或数组中的元素
      separator:分隔符,最后一个不会加上分隔符
      open:foreach标签中所有内容的开始
      close:foreach标签中所有内容的结束
      -->
      <delete id="deleteBatchByForeach">
      delete from t_car where id in
      <foreach collection="ids" item="id" separator="," open= "("   close=  ")"  >
      #{id}
      </foreach>
      </delete>
      
    • 用 or 实现批量删除
      <delete id="deleteBatchByForeach2">
      delete from t_car where
      <foreach collection="ids" item="id" separator="or">
      id = #{id}
      </foreach>
      </delete>
      
    • 批量添加
      int insertBatchByForeach(@Param("cars") List<Car> cars);
      
      <insert id="insertBatchByForeach">
      insert into t_car values
      <foreach collection="cars" item="car" separator=",">
      (null,#{car.carNum},#{car.brand},#{car.guidePrice},#{car.produceTime},#
      {car.carType})
      </foreach>
      </insert>
      
  7. sql标签与include标签
    sql标签用来声明sql片段
    include标签用来将声明的sql片段包含到某个sql语句当中
    作用:代码复用。易维护。
    <sql id="carCols">id,car_num carNum,brand,guide_price guidePrice,produce_t
    ime produceTime,car_type carType</sql>
    <select id="selectAllRetMap" resultType="map">
    select <include refid="carCols"/> from t_car
    </select>
    <select id="selectAllRetListMap" resultType="map">
    select <include refid="carCols"/> carType from t_car
    </select>
    <select id="selectByIdRetMap" resultType="map">
    select <include refid="carCols"/> from t_car where id = #{id}
    </select>
    
http://www.yayakq.cn/news/816833/

相关文章:

  • 东莞网站制作培训多少钱网站的毕业设计怎么做
  • 建站公司海报制作
  • 如何把自己做的网站放到网上阿里巴巴网站建设的功能定位
  • 学校多语言网站建设郑州教育信息网
  • 企业网站策划书ppt融资计划书
  • 惠州公司网站建设下载app到手机
  • 深圳龙华建设局官方网站如皋官方网站建设什么地铁
  • 网站是先解析后备案吗什么是电子商务网站推广
  • 做戒烟网站素材wordpress设置前台投稿
  • 上海机电设备公司网站建设wordpress 列表 分类
  • 网站建设属于设备吗医疗器械公司网站建设
  • 酒店网站策划书机房托管
  • 网站优化培训百度网盟 网站定向
  • 茶文化网站开发旅游机票网站建设
  • 毕业设计做网站论文好写吗网站注销备案查询系统
  • 区网站建设wordpress最新文章调用
  • 网站域名后缀西安线上推广公司
  • 塘厦镇做网站十种营销方法
  • php网站培训班网站设计制作的连接方式
  • 长尾词挖掘免费工具宁波seo搜索排名优化
  • 视频变成网站怎么做wordpress进不了
  • 做外贸兼职的网站有哪些seo高级
  • 做网站代理需要办什么营业执照静态网站开发课程
  • 怎么做动态的实时更新的网站网站关键词调整 收录
  • 网站开发公司 网站空间门户网站制作哪专业
  • 微信网站建设模板下载成都设计公司提成
  • 网站 设计 文档wordpress是cms
  • 分析网站建设发展措施wordpress后台
  • 义乌本地网站开发腾讯adq广告平台
  • 影视网站seo描述手机网站活动策划方案