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

临汾尚世互联网站建设广东东莞智通人才市场

临汾尚世互联网站建设,广东东莞智通人才市场,长沙推广网站,网站欢迎页面设计作者:longgangbai 以前用过ibatis2,但是听说ibatis3有较大的性能提升#xff0c;而且设计也更合理#xff0c;他不兼容ibatis2.尽管ibatis3还是beta10的状态#xff0c;但还是打算直接使用ibatis3.0, ibatis3.0应该更简单高效.最近还自己写了个ibatis3.0与spring集成的bean而且设计也更合理他不兼容ibatis2.尽管ibatis3还是beta10的状态但还是打算直接使用ibatis3.0, ibatis3.0应该更简单高效.最近还自己写了个ibatis3.0与spring集成的bean运行还正常还自鸣得意了一番但是当独立使用ibatis时在事务管理这个方面还是出现不少问题所以还是打算再认真研究一番ibatis3.0 1.SqlSessionFactory 每个ibatis应用都应该只有一个SqlSessionFactory的实例对象,所以一般设置为static属性或者使用spring管理时返回singleton类型与spring集成时其实也是写一个怎样构建SqlSessionFactory的Bean, 构建SqlSessionFactory一般是SqlSessionFactoryBuild通过读取ibatis的配置文件而进行build的 Reader reader Resources.getResourceAsReader(SqlMapConfig.xml); SqlSessionFactory sessionFactory new SqlSessionFactoryBuild().build(reader); 配置文件SqlMapConfig.xml的一般结构(元素顺序不能变)  ?xml version1.0 encodingUTF-8 ? !DOCTYPE configuration PUBLIC -//ibatis.apache.org//DTD Config 3.0//EN http://ibatis.apache.org/dtd/ibatis-3-config.dtd configuration properties resourcejdbc.properties / settings setting namecacheEnabled valuefalse / setting namelazyLoadingEnabled valuetrue / setting namemultipleResultSetsEnabled valuefalse / setting nameuseColumnLabel valuetrue / setting namedefaultExecutorType valueSIMPLE / /settings typeAliases typeAlias aliasPerson typetest.Person/ /typeAliases environments defaultdev environment iddev transactionManager typejdbc property name value / /transactionManager dataSource typePOOLED property namedriver value${driver} / property nameurl value${url} / property nameusername value${user} / property namepassword value${password} / /dataSource /environment /environments mappers mapper resourcesqlMappers/Person.xml / mapper resourcesqlMappers/UserShop.xml / /mappers /configuration settings /是配置ibatis的具体行为属性的  typeAliases /是为了将较长的module类名简化在mappers /里可以使用 environment /是配置transaction manager和connection pooling的 mappers /是sql语句的构造地方一般每个module对应一个文件 2.SqlSession 可以从SqlSessionFactory得到SqlSession: sessionFactory.openSession(); SqlSession是一切Sql相关数据库操作的中心,insert,select,update,delete... SqlSession不是线程安全的(也就是有状态的),所以它的作用域最好是在一个Thread下,每个Thread有自己的SqlSession对象实例彼此不相关. Never keep references to a SqlSession instance in a static field or even an instance field of a class.  Never keep references to a SqlSession in any sort of managed scope, such as HttpSession of of the Servlet framework. 默认sessionFacory.openSession()拿到的SqlSession不是自动commit的所以如果是更新操作必须自己执行session.commit() 关闭SqlSession很重要,必须保证在线程结束时关闭这个SqlSession,可以在finally中 session.close(); 那跟Spring集成是怎样做到这一点的呢,因为dataSource是由spring管理的,所以他可以保证在一个Thread的每个方法中拿到的Connection是同一个对象, 虽然每个方法从sessionFactory.openSession()拿到的SqlSession对象是不同的但是sqlSession对象中的connection是相同的所以spring就可以在service层的方法结束之前将这个connection commit跟close这样就实现了事务控制. 我们往往在dao层是一个方法对应一个sql语句的不在这里控制事务控制事务应该在service层, dao的每个方法拿到的sqlsession对象都是不相同的(尽管它的connection可能相同). 那我们应该怎样在没有spring的情况下实现ibatis的事务控制呢?还要保持dao的结构以保持能跟spring随时切换? 看来ThreadLocal要派上用场了 占位 3.讲完了SqlSession这个大头,再来说说具体的配置信息 配置文件结构 configuration     properties     settings     typeAliases     typeHandlers     objectFactory     plugins     environments        environment            transactionManager            dataSource     mappers        4.ibatis可以配置多个environment环境 供开发测试上线等切换 但是一个SqlSessionFactory只能对应一个environment, 也就是 one SqlSessionFactory per database 5.transactionManager There are two TransactionManager types (i.e. type”?????”) that are included with iBATIS:     JDBC – This configuration simply makes use of the JDBC commit and rollback facilities directly.  It relies on the connection retrieved from the dataSource to manage the scope of the transaction.       MANAGED  – This configuration simply does nothing, quite literally.  It never commits, rolls back or closes a connection.  Instead, it lets the container manage the full lifecycle of the transaction (e.g. Spring or a JEE Application Server context). 6.dataSource的配置 类型可使用UNPOOLED,POOLED,JNDI三种 7.接下来是重头戏mappers 这里是我们直接写sql的地方,ibatis在这里也花了最多功夫特别是关于select的mapper. ?xml version1.0 encodingutf-8? !DOCTYPE mapper PUBLIC -//ibatis.apache.org//DTD mapper 3.0//EN http://ibatis.apache.org/dtd/ibatis-3-mapper.dtdmapper namespacetest.UserShop insert ... ... /insert select ... ... /select update ... ... /update delete ... ... /delete /mapper select 元素的所有的如下 selectidparameterTyperesultTyperesultMapflushCacheuseCachetimeoutfetchSizestatementTyperesultSetType    在Ibatis3中parameterMap已经不再使用。(1).select select idgetUser parameterTypeint resultTypetest.User        select * from user where userId #{value} /select 注意#{value},resultType可以换成resultMap,这个resultMap就比较复杂了,也是最强大的工具可以完成复杂的mapper(resultSet-object) (2).insert,update,delete 这几个操作都比较类似返回的一般是受影响的行数. insert 可以返回auto_increament的值  insert idinsertPerson parameterTypePerson useGeneratedKeystrue keyPropertyuserId insert into person(name,age) values(#{name},#{age}) /insert 这样,传入的Person类对象的userId就会被set成auto_increament那个值. (3).可以使用sql idcolumnsname,age/sql构造可重用的sql片段 这样就可以在那四个主要的元素里用include refidcolumns引用这个常用sql片段 (4).resultMap很复杂它可以处理一对一一对多多对多关系 比如一个blog对应一个author, 一个blog对应多个post, 一个post对应多个comment resultMap的结构 resultMap     constructor (向java构造函数设置值)        idArg        arg     id (id result,同result     result (字段映射注入)     association (一对一关系它里面有自己的result映射     collection  (一对多关系, 它里面有自己的result映射,和可能的collection)     discriminator (5).association (一个Blog对应一个Author)  resultMap idblogResult typeBlog association propertyauthor columnblog_author_id javaTypeAuthor id propertyid columnauthor_id / result propertyusername columnauthor_username / /associaton /resultMap 当然association也可以使用 association propertyauthor columnblog_author_id javaTypeAuthor resultMapauthorResult/ 在Blog类定义里会有一个Author类字段,会自动装载数据 private Author author; (6).collection (一个Blog对应多个Post)  resultMap idblogResult typeBlog collection propertyposts columnblog_id ofTypePost javaTypeArrayList resultMappostResult / /resultMap collection也可以精简为 collection propertyposts ofTypePost resultMappostResult / 相就地在Blog类定义里会有: private ListPost posts 8.cache  其实我是不打算在ibatis这层做cache的一般cache还是在service层做. ibatis对于select才会进行cache,而我觉得这个时候cache没多大意义,因为多次請求相同的select語句(sql相同)而又没有进行表的相关update的情况并不多. 但还是看看ibatis是怎样处理cache的 要开启cache需要在mapper文件中加上一句: cache / (1).cache所有select的結果 (2).在同一mapper文件中的insert,update,delete会flush cache (3).cache使用least recently used(LRU)算法 (4).cache没有定时flush的功能 (5).cache保存1024个对象或list引用 (6).cache是read/write cache, 从cache拿出对象不是共享的,caller可以任意修改而不用担心其他caller也拿到相同的对象(相同的reference) 9.动态SQL 以前用jdbc时经常要根据输入条件而组装成不同的sql这种就是动态sql ibatis的动态sql关键词:  if, choose(when,otherwise),trim(where,set),foreach (1).if if testtitle ! null AND title like #{title} /if ![CDATA[ ]] (2).choose(when,otherwise)只选择其中一种情况  select ... choose when testtitle ! null AND title like #{title} /when when testauthor ! null and author.name ! null AND title like #{author.name} /when otherwise AND featured 1 /otherwise /choose /select (3).trim, where, set  select idfindBlog paramaterTypeBlog resultTypeBlog select * from blog where if test”state ! null” state ${state} /if if test”title ! null” AND title like ${title} /if if test”author ! null and author.name ! null” AND title like ${author.name} /if /where /select update idupdateAuthorIfNecessary parameterTypedomain.blog.Author update Author set if testusername ! nullusername#{username},/if if testpassword ! nullpassword#{password},/if if testemail ! nullemail#{email},/if if testbio ! nullbio#{bio}/if /set where id#{id} /update   (4).foreach  select idselectPostIn resultTypedomain.blog.Post SELECT * FROM POST P WHERE ID in foreach itemitem indexindex collectionlist open( separator, close) #{item} /foreach /select 10.ibatis的annotation ibatis3.0也增加了annotation当然主要还是用在mapper这个问题上 个人感觉是鸡肋,暂时不会使用.转载于:https://www.cnblogs.com/lxl57610/p/7399184.html
http://www.zqtcl.cn/news/372180/

相关文章:

  • 腾讯科技微信小程序电商seo是什么意思啊
  • 手机网站模板更换方法新闻客户端网站开发
  • 湛江定制建站黄页推广app软件
  • 盈利型网站做安卓app用什么软件
  • wordpress优秀移动站点西宁公司网站建设
  • 浙江网站建设的要求建设网上商城网站的目的和意义
  • 西峰住房和城乡建设局网站关于校园网站升级建设的报告
  • 网站怎么自适应屏幕大小达人室内设计网app
  • 做网站的软件名字全拼wordpress面包屑文字大小如何调整
  • 如何建设软件下载网站北京网站建设出名 乐云践新
  • 网站seo外包南宁网站建设活动
  • 汽车行业网站设计做互联网公司网站谈单模拟视频教学
  • 做网站界面设计注意什么江苏宿迁房产网
  • 传奇服务器网站如何建设帮人做兼职的网站
  • 织梦手机网站有广告位wordpress媒体库现实不全
  • 网站建设外包公司怎么样珠海网站排名提升
  • 电子商务网站建设结业论文做网站的图片字虚
  • 米拓建站最新进展注册做网站的公司有哪些
  • 设计网站设计wordpress 改系统
  • 学校网站建设评审会议通知网站是怎么赢利的
  • 手机网站建设 苏州优化网站哪个好
  • 网站建设流程方案通州网站建设公司
  • 免费的十大免费货源网站全国领先网站制作
  • 农业网站建设方案 ppt中国有什么网站做跨境零售
  • 网站文章结构变更怎么做301如何自己制作自己的网站
  • 网站网站平台建设方案免费制作桥架app
  • 杭州网站界面设计招网站建设销售
  • 网站开发 流程图广州优化seo
  • 夫妻工作室网站建设品牌建设的内容
  • php搭建网站后台建设银行网站如何修改账单地址