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

好用的网站开发软件如何建设电影网站

好用的网站开发软件,如何建设电影网站,最好用的网站推广经验,百度关键字怎么搜到公司网站MyBatis 动态 SQL、MyBatis 标签、MyBatis关联查询 1、MyBatis动态 sql 的特性2、MyBatis 标签2.1、if 标签#xff1a;条件判断2.2、whereif 标签2.3、set 标签2.4、choose(when,otherwise) 语句2.5、trim2.6、MyBatis foreach 标签 3、整合案例3.1、XML3.2、测试类 4、sql 标… MyBatis 动态 SQL、MyBatis 标签、MyBatis关联查询 1、MyBatis动态 sql 的特性2、MyBatis 标签2.1、if 标签条件判断2.2、whereif 标签2.3、set 标签2.4、choose(when,otherwise) 语句2.5、trim2.6、MyBatis foreach 标签 3、整合案例3.1、XML3.2、测试类 4、sql 标签5、include 标签6、 如何引用其他 XML 中的 SQL 片段7、MyBatis 关联查询7.1、MyBatis一对多关联查询7.2、MyBatis多对一关联查询7.3、MyBatis多对多关联查询 收录的原文地址链接 收录的原文地址链接 收录的原文地址链接 动态 SQL 是 MyBatis 的强大特性之一。在 JDBC 或其它类似的框架中开发人员通常需要手动拼接 SQL 语句。根据不同的条件拼接 SQL 语句是一件极其痛苦的工作。例如拼接时要确保添加了必要的空格还要注意去掉列表最后一个列名的逗号。而动态 SQL 恰好解决了这一问题可以根据场景动态的构建查询。 动态SQLcode that is executed dynamically它一般是根据用户输入或外部条件动态组合的SQL语句块。动态SQL能灵活的发挥SQL强大的功能、方便的解决一些其它方法难以解决的问题。相信使用过动态SQL的人都能体会到它带来的便利然而动态SQL有时候在执行性能 (效率)上面不如静态SQL而且使用不恰当往往会在安全方面存在隐患 (SQL 注入式攻击)。 1、MyBatis动态 sql 的特性 1Mybatis 动态 sql 是做什么的? Mybatis 动态 sql 可以让我们在 Xml 映射文件内以标签的形式编写动态 sql完成逻辑判断和动态拼接 sql 的功能。 2Mybatis 的动态 sql 标签有哪些 元素作用备注if判断语句单条件分支判断choose(when、otherwise)相当于 Java 中的 switch case 语句多条件分支判断trim、where辅助元素用于处理一些 SQL 拼装问题foreach循环语句在 in 语句等列举条件中常用bind辅助元素拼接参数 3动态 sql 的执行原理 原理为使用 OGNL 从 sql 参数对象中计算表达式的值根据表达式的值动态拼接 sql以此来完成动态 sql 的功能。 2、MyBatis 标签 2.1、if 标签条件判断 MyBatis if 类似于 Java 中的 if 语句是 MyBatis 中最常用的判断语句。使用 if 标签可以节省许多拼接 SQL 的工作把精力集中在 XML 的维护上。 1不使用动态sql select idselectUserByUsernameAndSexresultTypeuser parameterTypecom.ys.po.User!-- 这里和普通的sql 查询语句差不多对于只有一个参数后面的 #{id}表示占位符里面不一定要写id,写啥都可以但是不要空着如果有多个参数则必须写pojo类里面的属性 --select * from user where username#{username} and sex#{sex} /selectif 语句使用方法简单常常与 test 属性联合使用。语法如下: if test判断条件 SQL语句/if2使用动态sql 上面的查询语句我们可以发现如果 #{username} 为空那么查询结果也是空如何解决这个问题呢使用 if 来判断可多个 if 语句同时使用。 以下语句表示为可以按照网站名称name或者网址url进行模糊查询。如果您不输入名称或网址则返回所有的网站记录。但是如果你传递了任意一个参数它就会返回与给定参数相匹配的记录。 select idselectAllWebsite resultMapmyResult select id,name,url from website where 11 if testname ! null AND name like #{name} /if if testurl! null AND url like #{url} /if /select2.2、whereif 标签 where、if 同时使用可以进行查询、模糊查询。 PS注意if失败后where 关键字只会去掉库表字段赋值前面的 and不会去掉语句后面的 and 关键字即注意where 只会去掉if 语句中的最开始的 and 关键字。所以下面的形式是不可取的 select idfindQuery resultTypeStudentinclude refidselectvp/whereif testsacc ! nullsacc like concat(% #{sacc} %)/ifif testsname ! nullAND sname like concat(% #{sname} %)/ifif testsex ! nullAND sex#{sex}/ifif testphone ! nullAND phone#{phone}/if/where /select这个 “where” 标签会知道如果它包含的标签中有返回值的话它就插入一个 ‘where’ 。此外如果标签返回的内容是以 AND 或 OR 开头的则它会剔除掉。 2.3、set 标签 set 可以用来修改 update idupdupdate studentsetif testsname ! nullsname#{sname},/ifif testspwd ! nullspwd#{spwd},/ifif testsex ! nullsex#{sex},/ifif testphone ! nullphone#{phone}/ifsid#{sid}/setwhere sid#{sid} /update2.4、choose(when,otherwise) 语句 有时候我们不想用到所有的查询条件只想选择其中的一个查询条件有一个满足即可使用 choose 标签可以解决此类问题类似于 Java 的 switch 语句 select idselectUserByChoose resultTypecom.ys.po.User parameterTypecom.ys.po.Userselect * from userwherechoosewhen testid ! and id ! nullid#{id}/whenwhen testusername ! and username ! nulland username#{username}/whenotherwiseand sex#{sex}/otherwise/choose/where/select也就是说这里我们有三个条件id、username、sex只能选择一个作为查询条件 如果 id 不为空那么查询语句为select * from user where id?如果 id 为空那么看username 是否为空如果不为空那么语句为 select * from user where username?;如果 username 为空那么查询语句为 select * from user where sex? 2.5、trim trim 标记是一个格式化的标记可以完成 set 者是 where 标记的功能。 1用 trim 改写上面第二点的 ifwhere 语句 select idselectUserByUsernameAndSex resultTypeuser parameterTypecom.ys.po.Userselect * from user!-- whereif testusername ! nullusername#{username}/ifif testusername ! nulland sex#{sex}/if/where --trim prefixwhere prefixOverridesand | orif testusername ! nulland username#{username}/ifif testsex ! nulland sex#{sex}/if/trim /selectprefix前缀prefixoverride去掉第一个 and 或者 or。 2用 trim 改写上面第三点的 ifset 语句 !-- 根据 id 更新 user 表的数据 -- update idupdateUserById parameterTypecom.ys.po.Userupdate user u!-- setif testusername ! null and username ! u.username #{username},/ifif testsex ! null and sex ! u.sex #{sex}/if/set --trim prefixset suffixOverrides,if testusername ! null and username ! u.username #{username},/ifif testsex ! null and sex ! u.sex #{sex},/if/trimwhere id#{id} /updatesuffix后缀suffixoverride去掉最后一个逗号也可以是其他的标记就像是上面前缀中的 and 一样。 3trimif 同时使用可以添加 insert idaddinsert into studenttrim prefix( suffix) suffixOverrides,if testsname ! nullsname,/ifif testspwd ! nullspwd,/ifif testsex ! nullsex,/ifif testphone ! nullphone,/if/trimtrim prefixvalues ( suffix) suffixOverrides,if testsname ! null#{sname},/ifif testspwd ! null#{spwd},/ifif testsex ! null#{sex},/ifif testphone ! null#{phone}/if/trim/insert2.6、MyBatis foreach 标签 foreach 是用来对集合的遍历这个和 Java 中的功能很类似。通常处理 SQL 中的 in 语句。 foreach 元素的功能非常强大它允许你指定一个集合声明可以在元素体内使用的集合项item和索引index变量。它也允许你指定开头与结尾的字符串以及集合项迭代之间的分隔符。这个元素也不会错误地添加多余的分隔符。 你可以将任何可迭代对象如 List、Set 等、Map 对象或者数组对象作为集合参数传递给 foreach。当使用可迭代对象或者数组时index 是当前迭代的序号item 的值是本次迭代获取到的元素。当使用 Map 对象或者 Map.Entry 对象的集合时index 是键item 是值。 //批量查询 select idfindAll resultTypeStudent parameterTypeIntegerinclude refidselectvp/ WHERE sid inforeach itemids collectionarray open( separator, close)#{ids}/foreach /select //批量删除 delete iddel parameterTypeIntegerdelete from student where sid inforeach itemids collectionarray open( separator, close)#{ids}/foreach /delete3、整合案例 3.1、XML XML ?xml version1.0 encodingUTF-8 ? !DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd mapper namespacecom.yzx.mapper.StuMappersql idselectvpselect * from student/sqlselect idfind resultTypeStudentinclude refidselectvp//selectselect idfindbyid resultTypestudentinclude refidselectvp/WHERE 11if testsid ! nullAND sid like #{sid}/if/selectselect idfindQuery resultTypeStudentinclude refidselectvp/whereif testsacc ! nullsacc like concat(% #{sacc} %)/ifif testsname ! nullAND sname like concat(% #{sname} %)/ifif testsex ! nullAND sex#{sex}/ifif testphone ! nullAND phone#{phone}/if/where/selectupdate idupdupdate studentsetif testsname ! nullsname#{sname},/ifif testspwd ! nullspwd#{spwd},/ifif testsex ! nullsex#{sex},/ifif testphone ! nullphone#{phone}/ifsid#{sid}/setwhere sid#{sid}/updateinsert idaddinsert into studenttrim prefix( suffix) suffixOverrides,if testsname ! nullsname,/ifif testspwd ! nullspwd,/ifif testsex ! nullsex,/ifif testphone ! nullphone,/if/trimtrim prefixvalues ( suffix) suffixOverrides,if testsname ! null#{sname},/ifif testspwd ! null#{spwd},/ifif testsex ! null#{sex},/ifif testphone ! null#{phone}/if/trim/insertselect idfindAll resultTypeStudent parameterTypeIntegerinclude refidselectvp/ WHERE sid inforeach itemids collectionarray open( separator, close)#{ids}/foreach/selectdelete iddel parameterTypeIntegerdelete from student where sid inforeach itemids collectionarray open( separator, close)#{ids}/foreach/delete/mapper3.2、测试类 测试类 package com.yzx.test;import com.yzx.entity.Student; import com.yzx.mapper.StuMapper; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test;import java.io.IOException; import java.io.InputStream; import java.util.List;public class StuTest {SqlSession sqlSessionnull;InputStream isnull;Beforepublic void before() throws IOException {//1.读取核心配置文件is Resources.getResourceAsStream(sqlMapperConfig.xml);//2.拿到工厂构建类SqlSessionFactoryBuilder sqlSessionFactoryBuildernew SqlSessionFactoryBuilder();//3.拿到具体工厂SqlSessionFactory buildsqlSessionFactoryBuilder.build(is);//4.拿到sessionsqlSession build.openSession();}Afterpublic void after(){//7提交事务sqlSession.commit();//8.关闭资源sqlSession.close();if(is!null){try {is.close();} catch (IOException e) {e.printStackTrace();}};}//查询所有Testpublic void find(){//5.获取具体的mapper接口StuMapper mappersqlSession.getMapper(StuMapper.class);//6.调用执行ListStudent listmapper.find();list.forEach(a- System.out.println(a));}//查询单个Testpublic void findbyid(){StuMapper mappersqlSession.getMapper(StuMapper.class);ListStudent listmapper.findbyid(2);list.forEach(a- System.out.println(a));}//模糊查询Testpublic void findQuery(){StuMapper mappersqlSession.getMapper(StuMapper.class);Student stunew Student();stu.setSname(小);stu.setSex(男);ListStudent listmapper.findQuery(stu);list.forEach(a- System.out.println(a));}//修改Testpublic void upd(){StuMapper mappersqlSession.getMapper(StuMapper.class);Student stunew Student();stu.setSid(3);stu.setSname(小若);stu.setSex(人妖);int imapper.upd(stu);System.out.println(修改了i条数据 stu.toString());}//添加Testpublic void add(){StuMapper mappersqlSession.getMapper(StuMapper.class);Student stunew Student();stu.setSname(小贺);stu.setSex(男);stu.setPhone(99999999);int imapper.add(stu);System.out.println(添加了i条数据 stu.toString());}//批量操作Testpublic void findAll(){StuMapper mappersqlSession.getMapper(StuMapper.class);Integer[] i{1,2,3,4};ListStudent listmapper.findAll(i);list.forEach(a- System.out.println(a));}//批量操作//批量删除Testpublic void del(){StuMapper mappersqlSession.getMapper(StuMapper.class);Integer[] i{1,2,3,4};int i1mapper.del(i);System.out.println(删除了i1条数据);} }4、sql 标签 在实际开发中会遇到许多相同的 SQL比如根据某个条件筛选这个筛选很多地方都能用到我们可以将其抽取出来成为一个公用的部分这样修改也方便一旦出现了错误只需要改这一处便能处处生效了此时就用到了 sql 这个标签了。 当多种类型的查询语句的查询字段或者查询条件相同时可以将其定义为常量方便调用。为求 select 结构清晰也可将 sql 语句分解。 sql idselectvpselect * from student /sql5、include 标签 这个标签和 sql 是天仙配是共生的include 用于引用 sql 标签定义的常量。比如引用上面 sql 标签定义的常量。 refid 这个属性就是指定 sql 标签中的 id 值唯一标识 select idfindbyid resultTypestudentinclude refidselectvp/WHERE 11if testsid ! nullAND sid like #{sid}/if /select6、 如何引用其他 XML 中的 SQL 片段 比如你在 com.xxx.dao.xxMapper 这个 Mapper 的 XML 中定义了一个 SQL 片段如下 sql idBase_Column_List ID,MAJOR,BIRTHDAY,AGE,NAME,HOBBY/sql此时我在 com.xxx.dao.Patinet 这个 Mapper 中的 XML 文件中需要引用如下 include refidcom.xxx.dao.xxMapper.Base_Column_List/include7、MyBatis 关联查询 7.1、MyBatis一对多关联查询 !--一对多-- resultMap idmyStudent1 typestudent1id propertysid columnsid/result propertysname columnsname/result propertysex columnsex/result propertysage columnsage/collection propertylist ofTypeteacherid propertytid columntid/result propertytname columntname/result propertytage columntage//collection /resultMap!--一对多-- select idfind1 resultMapmyStudent1select * from student1 s left join teacher t on s.sidt.sid /select7.2、MyBatis多对一关联查询 !--多对一-- resultMap idmyTeacher typeteacherid propertytid columntid/result propertytname columntname/result propertytage columntage/association propertystudent1 javaTypeStudent1id propertysid columnsid/result propertysname columnsname/result propertysex columnsex/result propertysage columnsage//association /resultMap!--多对一-- select idfind2 resultMapmyTeacher select * from teacher t right join student1 s on t.sids.sid /select7.3、MyBatis多对多关联查询 !--多对多 以谁为主表查询的时候主表约等于1的一方,另一方相当于多的一方-- select idfind3 resultMapmyStudent1select * from student1 s left join relevance r on s.sidr.sid left join teacher t on r.tidt.tid /select
http://www.w-s-a.com/news/544575/

相关文章:

  • 政务网站建设标准项目经理接到网站开发怎么开展
  • 网站框架设计好后怎么做网站广告销售怎们做
  • asp技校网站保定八大平台公司
  • wordpress网站前端优化网站备案批量查询
  • 北京企业做网站杭州seo中心
  • 护肤品网站建设前的行业分析wordpress电子书模板
  • 做网站怎么销售.net开发网站怎么样
  • 蚌埠网站优化网站换空间wordpress
  • 微网站开发框架公司企业logo
  • 大淘客官网做的网站打不开网站建设完成
  • 婚纱摄影网站模板让别人做网站怎样才安全
  • 技术支持 骏域网站建设专家佛山网站运营管理教材
  • 个体营业执照可以做网站服务吗电商运营学校培训
  • 企业网站免费推广的方法.wordpress 爱情模板下载地址
  • 轻淘客 轻网站怎么做手机开发人员选项怎么打开
  • 天津做网站制作公司html网站 下载
  • 哪个网站的课件做的好crm客户管理系统全称
  • 网站建设工作室创业计划书seo是什么职位的简称
  • o2o平台网站开发什么是白帽seo
  • 免费建个人手机网站WordPress 简历库
  • 建网站 是否 数据库阳瘘的最佳治疗方法是什么
  • 知晓程序网站怎么做网站基础维护
  • 兼职做网站赚钱吗图片设计制作哪个软件好手机
  • 做手机旅游网站智慧校园登录入口
  • 莆田网站建设维护国外极简网站
  • 百度怎样收录网站缪斯设计集团
  • 网站建设在开封找谁做wordpress 数据转换
  • 旅游网站开发的流程江苏付费网络推广培训
  • 网站软文标题2018wordpress主题
  • 德清网站设计wordpress免登录发布接