您好,欢迎访问三七文档
MyBatisMyBatis是支持普通SQL查询,存储过程等一个轻量级的ORM中间件。一、myBatis框架概述与传统的JDBC开发相比,MyBatis消除了几乎所有的代码和参数的手工设置。MyBatis使用简单的XML或注解方式,用于配置和原始映射,将接口和Java的POJOs(PlanOldJavaObjects,普通的Java对象)映射成数据库中的记录。二、mybatis与hibernate比较Hibernate基本上可以自动生成。其对数据库结构提供了较为完整的封装开发效率上,如果使用纯面向对象方式Hibernate比较快,但如果以HQL其他方式相差不多。可维护性方面,mybatis框架是以sql的开发方式,可以进行细粒度的优化。Hibernate自动生成的sql效果不理想。三、Mybatis开始3.1SqlSessionFactoryStringresource=org/mybatis/example/Configuration.xml;Readerreader=Resources.getResourceAsReader(resource);SqlSessionFactoryfactory=sqlSessionFactoryBuilder.build(reader,props);3.2调用sqlSqlSessionsession=sqlMapper.openSession();try{Blogblog=(Blog)session.selectOne(selectBlog,101);}finally{session.close();}3.3Mybatis的文件组成1Configuration.xml2Mapper.xml1Configuration.xml系统的核心设置,包含获取数据库连接实例的数据源和决定事务范围和控制的事务管理器1.1Configuration.xml简单的示例1.1.1配置数据源configuration–environmentsdefault=developmentenvironmentid=development–transactionManagertype=JDBC/–dataSourcetype=POOLED–propertyname=drivervalue=${driver}/–propertyname=urlvalue=${url}/–propertyname=usernamevalue=${username}/–propertyname=passwordvalue=${password}/–/dataSource/environment–/environments/configuration1.1.2告诉MyBatis到哪里去找相关映射文件mappersmapperresource=org/mybatis/builder/AuthorMapper.xml/mapperresource=org/mybatis/builder/BlogMapper.xml/mapperresource=org/mybatis/builder/PostMapper.xml//mappers//Usingurlfullyqualifiedpathsmappersmapperurl=file:///var/sqlmaps/AuthorMapper.xml/mapperurl=file:///var/sqlmaps/BlogMapper.xml/mapperurl=file:///var/sqlmaps/PostMapper.xml//mappers2Mapper.xml定义具体SQL映射语句的文件2.1Mapper.xml简单的示例selectid=”selectPerson”parameterType=”int”resultType=”hashmap”SELECT*FROMPERSONWHEREID=#{id}/select2.2输入属性参数parameterType将会传入这条语句的参数类的完全限定名或别名。2.3输出属性参数resultType或resultMap,但不能同时使用resultType从这条语句中返回的期望类型的类的完全限定名或别名。注意集合情形,那应该是集合可以包含的类型,而不能是集合本身。resultMap命名引用外部的resultMap。返回map是MyBatis最具力量的特性,对其有一个很好的理解的话,许多复杂映射的情形就能被解决了。2.4resultMapresultMapid=BaseResultMaptype=com.dingxun.tiku.vo.Area–idcolumn=area_idjdbcType=INTEGERproperty=areaId/–resultcolumn=codejdbcType=VARCHARproperty=code/–resultcolumn=parentjdbcType=INTEGERproperty=parent//resultMap2.5使用上的区别resultTypeselectid=”selectUsers”parameterType=”int”resultType=”com.someapp.model.User”selectid,username,hashedPasswordfromsome_tablewhereid=#{id}/selectselectid=”selectUsers”parameterType=”int”resultType=”User”selectuser_idas“id”,user_nameas“role.name”,fromsome_tablewhereid=#{id}/selectresultMapresultMapid=userResultMaptype=Useridproperty=idcolumn=user_id/resultproperty=usernamecolumn=username/resultproperty=passwordcolumn=password//resultMap引用它的语句使用resultMap属性就行了(注意我们去掉了resultType属性)。比如:selectid=”selectUsers”parameterType=”int”resultMap=”userResultMap”selectuser_id,user_name,hashed_passwordfromsome_tablewhereid=#{id}/select2.6resultMap一对一、多对一resultMapid=BaseResultMap2type=com.dingxun.tiku.vo.Adminidcolumn=admin_idjdbcType=INTEGERproperty=adminId/resultcolumn=role_idjdbcType=INTEGERproperty=roleId/associationproperty=rolecolumn=role_idjavaType=com.dingxun.tiku.vo.Roleidcolumn=role_idjdbcType=INTEGERproperty=roleId/resultcolumn=namejdbcType=VARCHARproperty=name//association/resultMapresultMapid=blogResulttype=Blogidproperty=”blog_id”column=id/resultproperty=titlecolumn=blog_title/associationproperty=authorcolumn=blog_author_idjavaType=AuthorresultMap=”authorResult”//resultMapresultMapid=authorResulttype=Authoridproperty=idcolumn=author_id/resultproperty=usernamecolumn=author_username/resultproperty=passwordcolumn=author_password//resultMapresultMap一对多collectionproperty=postsofType=Postidproperty=idcolumn=post_id/resultproperty=subjectcolumn=post_subject/resultproperty=bodycolumn=post_body//collection2.6其他insertid=insertAuthorparameterType=domain.blog.Author–flushCache=true–statementType=PREPARED–keyProperty=–useGeneratedKeys=–timeout=20000updateid=insertAuthorarameterType=domain.blog.Author–flushCache=true–statementType=PREPARED–timeout=20000deleteid=insertAuthorarameterType=domain.blog.Author–flushCache=true–statementType=PREPARED2.7可重用的SQL代码段sqlid=”userColumns”id,username,password/sql这个SQL片段可以被包含在其他语句中,例如:selectid=”selectUsers”parameterType=”int”resultType=”hashmap”selectincluderefid=”userColumns”/fromsome_tablewhereid=#{id}/select2.8动态SQLMyBatis的一个强大的特性之一通常是它的动态SQL能力。如果你有使用JDBC或其他相似框架的经验,你就明白条件地串联SQL字符串在一起是多么的痛苦,确保不能忘了空格或在列表的最后省略逗号。动态SQL可以彻底处理这种痛苦。通常使用动态SQL不可能是独立的一部分,MyBatis当然使用一种强大的动态SQL语言来改进这种情形,这种语言可以被用在任意映射的SQL语句中。例子selectid=”findActiveBlogWithTitleLike”parameterType=”Blog”resultType=”Blog”SELECT*FROMBLOGWHEREstate=„ACTIVE‟iftest=”title!=null”ANDtitlelike#{title}/if/select处理了一个臭名昭著的动态SQL问题whereiftest=”state!=null”state=#{state}/ififtest=”title!=null”ANDtitlelike#{title}/ififtest=”author!=nullandauthor.name!=null”ANDtitlelike#{author.name}/if/where四缓
本文标题:mybatis3
链接地址:https://www.777doc.com/doc-3648418 .html