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

临西网站建设费用公司简介电子版宣传册模板

临西网站建设费用,公司简介电子版宣传册模板,网站如何做备份,想自己在家做外贸网站三、动态SQL语句 有些时候#xff0c;sql语句where条件中#xff0c;需要一些安全判断#xff0c;例如按性别检索#xff0c;如果传入的参数是空的#xff0c;此时查询出的结果很可能是空的#xff0c;也许我们需要参数为空时#xff0c;是查出全部的信息。这是我们可以… 三、动态SQL语句         有些时候sql语句where条件中需要一些安全判断例如按性别检索如果传入的参数是空的此时查询出的结果很可能是空的也许我们需要参数为空时是查出全部的信息。这是我们可以使用动态sql增加一个判断当参数不符合要求的时候我们可以不去判断此查询条件。         下文均采用mysql语法和函数例如字符串链接函数CONCAT。 1 if标签 !-- 查询学生listlike姓名 -- select idgetStudentListLikeName parameterTypeStudentEntity resultMapstudentResultMap SELECT * from STUDENT_TBL ST WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT(%, #{studentName}),%) /select !-- 查询学生listlike姓名 -- select idgetStudentListLikeName parameterTypeStudentEntity resultMapstudentResultMap SELECT * from STUDENT_TBL ST WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT(%, #{studentName}),%) /select 但是此时如果studentName是null或空字符串此语句很可能报错或查询结果为空。此时我们使用if动态sql语句先进行判断如果值为null或等于空字符串我们就不进行此条件的判断。 修改为 !-- 查询学生listlike姓名 -- select id getStudentListLikeName parameterTypeStudentEntity resultMapstudentResultMap SELECT * from STUDENT_TBL ST if teststudentName!null and studentName! WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT(%, #{studentName}),%) /if /select !-- 查询学生listlike姓名 -- select id getStudentListLikeName parameterTypeStudentEntity resultMapstudentResultMap SELECT * from STUDENT_TBL ST if teststudentName!null and studentName! WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT(%, #{studentName}),%) /if /select  此时当studentName的值为null或’’的时候我们并不进行where条件的判断所以当studentName值为null或’’值不附带这个条件所以查询结果是全部。  由于参数是Java的实体类所以我们可以把所有条件都附加上使用时比较灵活 new一个这样的实体类我们需要限制那个条件只需要附上相应的值就会where这个条件相反不去赋值就可以不在where中判断。 !-- 查询学生listlike姓名性别、生日、班级使用where,参数entity类型 -- select idgetStudentListWhereEntity parameterTypeStudentEntity resultMapstudentResultMap SELECT * from STUDENT_TBL ST where if teststudentName!null and studentName! ST.STUDENT_NAME LIKE CONCAT(CONCAT(%, #{studentName}),%) /if if teststudentSex! null and studentSex! AND ST.STUDENT_SEX #{studentSex} /if if teststudentBirthday!null AND ST.STUDENT_BIRTHDAY #{studentBirthday} /if if testclassEntity!null and classEntity.classID !null and classEntity.classID! AND ST.CLASS_ID #{classEntity.classID} /if /where /select 2 where、set、trim标签 2.1 where    当if标签较多时这样的组合可能会导致错误。例如like姓名等于指定性别等 !-- 查询学生listlike姓名性别 --   select idgetStudentListWhere parameterTypeStudentEntity resultMapstudentResultMap       SELECT * from STUDENT_TBL ST            WHERE            if teststudentName!null and studentName!                ST.STUDENT_NAME LIKE CONCAT(CONCAT(%, #{studentName}),%)            /if           if teststudentSex! null and studentSex!                 AND ST.STUDENT_SEX  #{studentSex}            /if   /select   如果上面例子参数studentName为null或’’则或导致此sql组合成“WHERE AND”之类的关键字多余的错误SQL。  这时我们可以使用where动态语句来解决。这个“where”标签会知道如果它包含的标签中有返回值的话它就插入一个‘where’。此外如果标签返回的内容是以AND 或OR 开头的则它会剔除掉。  上面例子修改为 Xml代码   !-- 查询学生listlike姓名性别 -- select idgetStudentListWhere parameterTypeStudentEntity resultMapstudentResultMap SELECT * from STUDENT_TBL ST where if teststudentName!null and studentName! ST.STUDENT_NAME LIKE CONCAT(CONCAT(%, #{studentName}),%) /if if teststudentSex! null and studentSex! AND ST.STUDENT_SEX #{studentSex} /if /where /select 2.2 set 当在update语句中使用if标签时如果前面的if没有执行则或导致逗号多余错误。使用set标签可以将动态的配置SET 关键字和剔除追加到条件末尾的任何不相关的逗号。 没有使用if标签时如果有一个参数为null都会导致错误如下示例 !-- 更新学生信息 -- update idupdateStudent parameterTypeStudentEntity UPDATE STUDENT_TBL SET STUDENT_TBL.STUDENT_NAME #{studentName}, STUDENT_TBL.STUDENT_SEX #{studentSex}, STUDENT_TBL.STUDENT_BIRTHDAY #{studentBirthday}, STUDENT_TBL.CLASS_ID #{classEntity.classID} WHERE STUDENT_TBL.STUDENT_ID #{studentID}; /update 使用setif标签修改后如果某项为null则不进行更新而是保持数据库原值。如下示例 !-- 更新学生信息 -- update idupdateStudent parameterTypeStudentEntity UPDATE STUDENT_TBL set if teststudentName!null and studentName! STUDENT_TBL.STUDENT_NAME #{studentName}, /if if teststudentSex!null and studentSex! STUDENT_TBL.STUDENT_SEX #{studentSex}, /if if teststudentBirthday!null STUDENT_TBL.STUDENT_BIRTHDAY #{studentBirthday}, /if if testclassEntity!null and classEntity.classID!null and classEntity.classID! STUDENT_TBL.CLASS_ID #{classEntity.classID} /if /set WHERE STUDENT_TBL.STUDENT_ID #{studentID}; /update 2.3 trim  trim是更灵活的去处多余关键字的标签他可以实践where和set的效果。  where例子的等效trim语句 !-- 查询学生listlike姓名性别 -- select idgetStudentListWhere parameterTypeStudentEntity resultMapstudentResultMap SELECT * from STUDENT_TBL ST trim prefixWHERE prefixOverridesAND|OR if teststudentName!null and studentName! ST.STUDENT_NAME LIKE CONCAT(CONCAT(%, #{studentName}),%) /if if teststudentSex! null and studentSex! AND ST.STUDENT_SEX #{studentSex} /if /trim /select set例子的等效trim语句 !-- 更新学生信息 -- update idupdateStudent parameterTypeStudentEntity UPDATE STUDENT_TBL trim prefixSET suffixOverrides, if teststudentName!null and studentName! STUDENT_TBL.STUDENT_NAME #{studentName}, /if if teststudentSex!null and studentSex! STUDENT_TBL.STUDENT_SEX #{studentSex}, /if if teststudentBirthday!null STUDENT_TBL.STUDENT_BIRTHDAY #{studentBirthday}, /if if testclassEntity!null and classEntity.classID!null and classEntity.classID! STUDENT_TBL.CLASS_ID #{classEntity.classID} /if /trim WHERE STUDENT_TBL.STUDENT_ID #{studentID}; /update 3 choose (when, otherwise)          有时候我们并不想应用所有的条件而只是想从多个选项中选择一个。MyBatis提供了choose 元素按顺序判断when中的条件出否成立如果有一个成立则choose结束。当choose中所有when的条件都不满则时则执行otherwise中的sql。类似于Java 的switch 语句choose为switchwhen为caseotherwise则为default。          if是与(and)的关系而choose是或or的关系。          例如下面例子同样把所有可以限制的条件都写上方面使用。选择条件顺序when标签的从上到下的书写顺序 !-- 查询学生listlike姓名、或性别、或生日、或班级使用choose -- select idgetStudentListChooseEntity parameterTypeStudentEntity resultMapstudentResultMap SELECT * from STUDENT_TBL ST where choose when teststudentName!null and studentName! ST.STUDENT_NAME LIKE CONCAT(CONCAT(%, #{studentName}),%) /when when teststudentSex! null and studentSex! AND ST.STUDENT_SEX #{studentSex} /when when teststudentBirthday!null AND ST.STUDENT_BIRTHDAY #{studentBirthday} /when when testclassEntity!null and classEntity.classID !null and classEntity.classID! AND ST.CLASS_ID #{classEntity.classID} /when otherwise /otherwise /choose /where /select 4 foreach 对于动态SQL 非常必须的主是要迭代一个集合通常是用于IN 条件。 List 实例将使用“list”做为键数组实例以“array” 做为键。 foreach属性 属性描述 item 循环体中的具体对象。支持属性的点路径访问如item.age,item.info.details。 具体说明在list和数组中是其中的对象在map中是value。 该参数为必选。 collection 要做foreach的对象作为入参时List?对象默认用list代替作为键数组对象有array代替作为键Map对象没有默认的键。 当然在作为入参时可以使用Param(keyName)来设置键设置keyName后list,array将会失效。 除了入参这种情况外还有一种作为参数对象的某个字段的时候。举个例子 如果User有属性List ids。入参是User对象那么这个collection ids 如果User有属性Ids ids;其中Ids是个对象Ids有个属性List id;入参是User对象那么collection ids.id 上面只是举例具体collection等于什么就看你想对那个元素做循环。 该参数为必选。 separator 元素之间的分隔符例如在in()的时候separator,会自动在元素中间用“,“隔开避免手动输入逗号导致sql错误如in(1,2,)这样。该参数可选。 open foreach代码的开始符号一般是(和close)合用。常用在in(),values()时。该参数可选。 close foreach代码的关闭符号一般是)和open(合用。常用在in(),values()时。该参数可选。 index 在list和数组中,index是元素的序号在map中index是元素的key该参数可选。 4.1参数为list实例的写法 SQL写法 select idgetStudentListByClassIDs resultMapstudentResultMap SELECT * FROM STUDENT_TBL ST WHERE ST.CLASS_ID IN foreach collectionlist itemclassList open( separator, close) #{classList} /foreach /select 接口的方法声明 public ListStudentEntity getStudentListByClassIDs(ListString classList);   ListString classList  new ArrayListString();    classList.add(20000002);    classList.add(20000003);       ListStudentEntity studentList  studentMapper.getStudentListByClassIDs(classList);    for( StudentEntity entityTemp : studentList){        System.out.println(entityTemp.toString());    }  4.2参数为Array实例的写法   SQL语句 select idgetStudentListByClassIDs resultMapstudentResultMap SELECT * FROM STUDENT_TBL ST WHERE ST.CLASS_ID IN foreach collectionarray itemids open( separator, close) #{ids} /foreach /select  接口的方法声明 Java代码   public ListStudentEntity getStudentListByClassIDs(String[] ids);   测试代码查询学生中在20000002、20000003这两个班级的学生 Java代码   String[] ids  new String[2];    ids[0]  20000002;    ids[1]  20000003;    ListStudentEntity studentList  studentMapper.getStudentListByClassIDs(ids);    for( StudentEntity entityTemp : studentList){        System.out.println(entityTemp.toString());    }
http://www.zqtcl.cn/news/789910/

相关文章:

  • 天津谁做网站莱芜人才网招聘网
  • 学做网站的书籍自己做网站 最好的软件
  • 手机网站专题电商入门视频教程免费
  • aspx网站模板制作网页常用的软件有哪些
  • 网站主关键词湖南网站定制
  • 长沙seo网站排名优化公司进入秦皇岛最新规定
  • 企业网站优化平台宝山北京网站建设
  • 给人做代工的网站加盟代理网
  • 网站建设用dw电脑谷歌浏览器打开是2345网址导航
  • 做外贸一般总浏览的网站太原的网站建设公司哪家好
  • 台州建网站公司wordpress 用微信登陆
  • 广州白云网站建设家在深圳业主
  • 呼和浩特网站建设哪家最便宜?携程旅行网网站策划书
  • 网站建设及相关流程北京网站备案域名
  • 汉字叔叔花了多少钱做网站微商城科技
  • 网站代理被抓html网站开发实战
  • 如何建立免费的网站网站copyright写法
  • 官方网站下载12306合肥有没有做网站的单位
  • 甘露园网站建设网站框架图片
  • 做网站怎样赚卖流量石家庄网站建设联系电话
  • wordpress 图片网站本地免费发布信息网站
  • 建设网站和别人公司重名新乡建设招标投标网站
  • 四川省建设厅网站证想开个网站怎样开公司
  • 做机械一般做那个外贸网站电商软件开发费用
  • 网站外链坏处龙岗网站设计信息
  • 郑州网站建设乙汉狮网络搜索优化网络推广
  • Dw做html网站百度推广竞价排名
  • 北京市电力建设公司网站万云网络网站
  • 校园网站开发方案做网站现在用什么语言
  • 网站建设学什么书中联建设集团股份有限公司网站