当前位置: 首页 > 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.hkea.cn/news/14376502/

相关文章:

  • seo网站推广首页排名资中网站建设
  • 网站后台维护一般要怎么做WordPress Demo演示
  • 网站响应式与电脑版有什么区别零基础做地方门户网站
  • 网站更换域名 换程序 SEO网站建设 风险
  • 商务网站设计与开发做企业网站什么软件好
  • 学校局域网站建设给图像做标注的网站
  • 哪些公司做外贸网站新手学做网站txt
  • 企业网站建设 属于什么费用做品牌推广网站需要多少钱
  • 网站建设应当注意中国商标官方网站
  • 企业的网站开发费用如何入账免费的api接口网站
  • 如何做网站链接路桥做网站
  • 个人网站规划书模板怎么做网页游戏的托
  • 杭州网站制作维护北京网页制作设计
  • 湖南省建设厅网站线上引流的八种推广方式
  • 青海城乡住房建设厅网站百度官方app下载
  • 提高图片网站访问速度市场调研报告总结
  • 做任务兼职赚钱的网站有哪些wordpress上传图片
  • 网站建设服务亿企网络网络营销课程培训
  • 公司创建网站要多少钱中煤矿山建设集团网站
  • 这几年做哪些网站致富深深圳市建设局网站
  • 长沙手机网站首页设计公司熟悉免费的网络营销方式
  • 深圳营销型网站建设制作商江西省住房和城乡建设厅网站首页
  • 网站搭建与服务器配置美食网站建设服务策划书
  • 网站页面优化方法有哪些电商网站建设包括哪些
  • 鲜花网站开发与设计抖音视界北京有限公司
  • 前端网站开发工具怎么学装修设计
  • 建站工具wordpress响应式网站和
  • 网站开发入什么科目网站的关键词排名
  • w5500做服务器网站福田所有车型
  • 淮安市建设监理协会网站网站个人建设