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

什么叫门户网站益阳建设厅网站

什么叫门户网站,益阳建设厅网站,wordpress文章标签调用,广州花都网站建设三、动态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/76959/

相关文章:

  • 免费发布信息网网站哈尔滨网站建设一薇ls15227
  • 网站开发技术学习网站备案 公司注销
  • 美食网站建设的内容分析有哪些档案网站
  • 陕西省住房和城市建设厅网站成都市 建设领域信用系统网站
  • 做淘宝客要建网站吗wordpress页面教程视频教程
  • 企业社交网站定制书店网站建设目标调研的方案
  • 哪个网站可以接图纸做网站如何创建
  • 网站ip和pv的比例wordpress 小工具居中
  • 响应式网站做法网站建设取得了
  • 企业建站系统开源佛山建设外贸网站公司吗
  • 东莞seo建站投放嘉兴网站搭建
  • aso.net 网站开发网页的网站建设在哪里
  • 程序员做项目的网站如何做线下推广
  • 大良商城网站建设菏泽网站推广
  • 潍坊市建设一体化平台网站wordpress加速r
  • 网站建设的开发方式知乎网站建设 方案下载
  • 凡科免费建微信小程序网站婚恋网站 备案条件
  • 聊城制作手机网站公司长沙有什么好玩的游乐场
  • 技术支持东莞网站建设河北唐山建设工程协会网站
  • 如何用源码建站专业设计公司vi设计
  • 做健康食品的网站金融行业做网站需要什么
  • wordpress网站放icp信息系统开发方案
  • 百度指数可以查询多长时间的seo如何优化关键词上首页
  • 网站导航栏一般有什么内容青岛神马排名优化
  • 个人网站首页设计优秀作品自动的东莞网站制作公司
  • 360提示的危险网站重庆工程建设信息网官网入口
  • 优质的外国网站大连网站制作案例
  • 中文网站建设南通建公司网站
  • 移动开发网站建设建筑模板是什么东西
  • 小白怎么做网站搬家教程个人网站注册名称