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

鹤岗市网站建设_网站建设公司_服务器部署_seo优化

教育网站图片,禅城网站建设联系电话,网站上有声的文章是怎么做的,受欢迎的网站建设平台目录 前言 if trim where set foreach 前言 动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架#xff0c;你应该能理解根据不同条件拼接 SQL 语句有多痛苦#xff0c;例如拼接时要确保不能忘记添加必要的空格#xff0c;还要注意去掉列表…目录 前言  if  trim where set  foreach 前言  动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架你应该能理解根据不同条件拼接 SQL 语句有多痛苦例如拼接时要确保不能忘记添加必要的空格还要注意去掉列表最后一个列名的逗号。利用动态 SQL可以彻底摆脱这种痛苦。 例如在注册用户的时候, 提交的表单中肯定有非必填字段, 前端会将拿到的数据送给后端, 但是, 用户在提交的时候, 由于存在非必填字段, 于是用户提交的字段可能是变数, 可能是一个, 两个或者三个, 例如有下面三个字段 usernamepasswordteleage(非必填)sex.....         除了age都为必填, 这个时候, 前端返回数据给后端, 然后使用sql将其存入数据库, 那么, 在插入的时候 insert into table(username, password, tele, sex, age) values(#{username},#{username}, #{tele}, #{sex}, #{age});        上面是age传入的时候, 那么试想一下, 如果这个age如果用户没有选择填写呢, 那么insert语句中的age选项就应该被抹除 使用动态 SQL 并非一件易事但借助可用于任何 SQL 映射语句中的强大的动态 SQL 语言MyBatis 显著地提升了这一特性的易用性。 如果你之前用过 JSTL 或任何基于类 XML 语言的文本处理器你对动态 SQL 元素可能会感觉似曾相识。在 MyBatis 之前的版本中需要花时间了解大量的元素。借助功能强大的基于 OGNL 的表达式MyBatis 3 替换了之前的大部分元素大大精简了元素种类现在要学习的元素种类比原来的一半还要少。 ifchoose (when, otherwise)trim (where, set)foreach if  insert idinsert parameterTypeorg.example.model.User useGeneratedKeystrue keyPropertyidinsert into user(username,password,nickname,if testsex ! nullsex,/ifbirthday,head) values (#{username},#{password},#{nickname},if testsex ! null#{sex},/if#{birthday},#{head})/insert if中有一个test属性, test是判断这个sex项是否存在的标准, 前端传过来的数据会被解析为java对象, 并将属性写入到对象中, 如果对象中的sex属性为空, 那么就代表用户没有传入这个字段, 于是就需要将这个项给抹除. trim 上面的情况是只有一个选填项, 但是如果所有的字段, 都可能是选填项, 那么就可以配合trim使用if来解决: insert idinsert parameterTypeorg.example.model.User useGeneratedKeystrue keyPropertyidinsert into usertrim prefix( suffix) suffixOverrides,if testusername ! nullusername,/ifif testpassword ! nullpassword,/ifif testnickname ! nullnickname,/ifif testsex ! nullsex,/ifif testbirthday ! nullbirthday,/ifif testhead ! nullhead,/ifif testcreateTime ! nullcreate_time,/if/trimtrim prefixvalues ( suffix) suffixOverrides,if testusername ! null#{username},/ifif testpassword ! null#{password},/ifif testnickname ! null#{nickname},/ifif testsex ! null#{sex},/ifif testbirthday ! null#{birthday},/ifif testhead ! null#{head},/ifif testcreateTime ! null#{createTime},/if/trim/insert prefix : 表示整个trim语句块的开头suffix : 表示整个trim语句块的结尾prefixOverrides : 表示整个语句块要去除掉的前缀suffixOverrides : 表示整个语句块中, 要去除掉的后缀, 这个后缀是不计suffix的. 例如trim语句块如下, 那么结束的时候就为 .... .... head, create_time,) ,呢么就会忽略 ), 去除create_time后面的, if testhead ! null head, /if if testcreateTime ! null create_time, /if 上面语句翻译过来就是 isnert into user(username, password, age, sex, ) values(#{username}, #{password}, #{age}, #{sex},) 需要注意的是, 这里的sex可能为选填, 无论如何, 这个table_name后面的参数和values中的参数, 必定有以 , 结尾的内容, 但是这个在sql中是错误语句. 于是使用suffixOverrides将后缀,去除(不包括 suffix )). where 传⼊的⽤户对象根据属性做 where 条件查询⽤户对象中属性不为 null 的都为查询条件。如user.username 为 a则查询条件为 where usernamea UserMapper 接⼝中新增条件查询⽅法: ListUser selectByCondition(User user); xml中新增查询语句: select idselectByCondition parameterTypeorg.example.model.User resultMapBaseResultMapselect id, username, password, nickname, sex, birthday, head, create_timefrom userwhereif testusername ! nulland username#{username}/ifif testpassword ! nulland password#{password}/ifif testnickname ! nulland nickname#{nickname}/ifif testsex ! nulland sex#{sex}/ifif testbirthday ! nulland birthday#{birthday}/ifif testhead ! nulland head#{head}/ifif testcreateTime ! nulland create_time#{createTime}/if/where/select 注意: 这里的and一定要写到 column #{column } 的前面, 也就是: and column #{column } where标签就会自动识别并去除多余的and.  当然, where也可以使用trim标签和if标签代替. set  根据传⼊的⽤户对象属性来更新⽤户数据可以使⽤set标签来指定动态内容 定义mapper接口: int updateById(User user); xml: update idupdateById parameterTypeorg.example.model.Userupdate usersetif testusername ! nullusername#{username},/ifif testpassword ! nullpassword#{password},/ifif testnickname ! nullnickname#{nickname},/ifif testsex ! nullsex#{sex},/ifif testbirthday ! nullbirthday#{birthday},/ifif testhead ! nullhead#{head},/ifif testcreateTime ! nullcreate_time#{createTime},/if/setwhere id#{id}/update set会自动去掉末尾的 , foreach 对集合进⾏遍历时可以使⽤该标签。foreach标签有如下属性 collection绑定⽅法参数中的集合如 ListSetMap或数组对象item遍历时的每⼀个对象open语句块开头的字符串close语句块结束的字符串separator每次遍历之间间隔的字符串 示例根据多个⽂章 id 来删除⽂章数据 ArticleMapper 中新增接⼝⽅法 int deleteByIds(ListInteger ids); 使用List接收, Integer为泛型, 传入的是一个Integer的list集合, 每一个Integer对象对应一个要被删除的文章(Article) ArticleMapper.xml 中新增删除 sql: delete iddeleteByIdsdelete from article where id inforeach collectionlist itemitem open( close) separator,#{item}/foreach /delete in后面其实是可以使用一个多表查询的, 现在这里使用的foreach进行遍历. Collection指明集合的类型itme表示每个每一个对象 然后就遍历成了: delete from article where id in  (item1, item2, item3 ..... ) 这一个个item对象就是list中的Integer对象. 也就是对应的int类型, 与id的类型对应.
http://www.ihoyoo.com/news/28318.html

相关文章:

  • php网站开发手机绑定软件开发公司哪家强
  • 网站建设公司广告语大学建设网站的意义
  • 东莞如何搭建网站建设建立公司网站的重点
  • 网站模板破解版网站流量共享
  • 原创手做网站ftp网站后台
  • 义乌城市投资建设集团网站我要自学网网页制作视频教程
  • 网站系统与网站源码的关系广东建设厅官网证书查询
  • 做淘宝客注册网站好做吗wordpress自动适应手机
  • 网站开发不用框架中铁快运关于网站建设
  • 网站访问量很大怎么办制作企业网站的问题
  • 东莞微信网站建设咨询彩票网站建设要多少钱
  • 永州市开发建设投资有限公司网站网站做的一样侵权吗
  • 自己做外贸购物网站所见即所得的网站开发软件
  • 网站代理协议SEO网站建设入驻程流
  • 建一个论坛网站要多少钱企业招聘信息发布平台
  • 网站建设的优势是什么意思网页制作软件序列号
  • 建设银行 商户网站打不开四川建设厅网上查询网站首页
  • 福建漳州网站建设哪家便宜义县网站建设
  • 学校网站php源码立方米网站建设
  • 响应式网站模板 金融云南建投第七建设有限公司网站
  • 免费建网站广告语商业网站平台
  • 网站推广的内涵重庆市招标网
  • 基础微网站开发价位做苗木选择哪个网站
  • 营销型网站建设方案华米手表官方网站
  • 网站建设工作总结培训嵌入式软件开发哪家好
  • 网上工商营业注册登记江门百度seo
  • wordpress 手机插件游戏优化是什么意思?
  • 门户网站模板源码提供建设服务的网络公司
  • 扶贫工作网站怎么做wordpress显示摘要插件
  • 阜南做网站酒店网站建设论文