mybatis映射XML文件的示例分析
这篇文章主要介绍mybatis映射XML文件的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
mybatis映射XML文件
一个简单的映射文件:
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="com.cnx.wxcar.mapper.CustomerMapper"></mapper>
当然这个文件中没有任何的元素
The Mapper XML files have only a few first class elements :
cache – Configuration of the cache for a given namespace.
cache-ref – Reference to a cache configuration from another namespace.
resultMap – The most complicated and powerful element that describes how to load your objects from the database result sets.
sql – A reusable chunk of SQL that can be referenced by other statements.
insert – A mapped INSERT statement.
update – A mapped UPDATE statement.
delete – A mapped DELETE statement.
select – A mapped SELECT statement.
select
简单的例子:
<selectid="selectPerson"parameterType="int"resultType="hashmap">SELECT*FROMPERSONWHEREID=#{id}</select>
select也有很多属性可以让你配置:
<selectid="selectPerson"parameterType="int"parameterMap="deprecated"resultType="hashmap"resultMap="personResultMap"flushCache="false"useCache="true"timeout="10000"fetchSize="256"statementType="PREPARED"resultSetType="FORWARD_ONLY">
insert, update and delete
<insertid="insertAuthor"parameterType="domain.blog.Author"flushCache="true"statementType="PREPARED"keyProperty=""keyColumn=""useGeneratedKeys=""timeout="20"><updateid="updateAuthor"parameterType="domain.blog.Author"flushCache="true"statementType="PREPARED"timeout="20"><deleteid="deleteAuthor"parameterType="domain.blog.Author"flushCache="true"statementType="PREPARED"timeout="20">
语句:
<insertid="insertAuthor">insertintoAuthor(id,username,password,email,bio)values(#{id},#{username},#{password},#{email},#{bio})</insert><updateid="updateAuthor">updateAuthorsetusername=#{username},password=#{password},email=#{email},bio=#{bio}whereid=#{id}</update><deleteid="deleteAuthor">deletefromAuthorwhereid=#{id}</delete>
f your database supports auto-generated key fields (e.g. MySQL and SQL Server),上面的插入语句可以写成:
<insertid="insertAuthor"useGeneratedKeys="true"keyProperty="id">insertintoAuthor(username,password,email,bio)values(#{username},#{password},#{email},#{bio})</insert>
如果你的数据库还支持多条记录插入,可以使用下面这个语句:
<insertid="insertAuthor"useGeneratedKeys="true"keyProperty="id">insertintoAuthor(username,password,email,bio)values<foreachitem="item"collection="list"separator=",">(#{item.username},#{item.password},#{item.email},#{item.bio})</foreach></insert>
sql
这个element可以定义一些sql代码的碎片,然后在多个语句中使用,降低耦合。比如:
<sqlid="userColumns">${alias}.id,${alias}.username,${alias}.password</sql>
然后在下面的语句中使用:
<selectid="selectUsers"resultType="map">select<includerefid="userColumns"><propertyname="alias"value="t1"/></include>,<includerefid="userColumns"><propertyname="alias"value="t2"/></include>fromsome_tablet1crossjoinsome_tablet2</select>
Result Maps
官网给了个最最复杂的例子
大体意思呢就是一个博客系统有一个作者,很多博文,博文中有一个作者,很多评论,很多标签(包括了一对多,一对一)
<!--VeryComplexStatement--><selectid="selectBlogDetails"resultMap="detailedBlogResultMap">selectB.idasblog_id,B.titleasblog_title,B.author_idasblog_author_id,A.idasauthor_id,A.usernameasauthor_username,A.passwordasauthor_password,A.emailasauthor_email,A.bioasauthor_bio,A.favourite_sectionasauthor_favourite_section,P.idaspost_id,P.blog_idaspost_blog_id,P.author_idaspost_author_id,P.created_onaspost_created_on,P.sectionaspost_section,P.subjectaspost_subject,P.draftasdraft,P.bodyaspost_body,C.idascomment_id,C.post_idascomment_post_id,C.nameascomment_name,C.commentascomment_text,T.idastag_id,T.nameastag_namefromBlogBleftouterjoinAuthorAonB.author_id=A.idleftouterjoinPostPonB.id=P.blog_idleftouterjoinCommentConP.id=C.post_idleftouterjoinPost_TagPTonPT.post_id=P.idleftouterjoinTagTonPT.tag_id=T.idwhereB.id=#{id}</select><!--VeryComplexResultMap--><resultMapid="detailedBlogResultMap"type="Blog"><constructor><idArgcolumn="blog_id"javaType="int"/></constructor><resultproperty="title"column="blog_title"/><associationproperty="author"javaType="Author"><idproperty="id"column="author_id"/><resultproperty="username"column="author_username"/><resultproperty="password"column="author_password"/><resultproperty="email"column="author_email"/><resultproperty="bio"column="author_bio"/><resultproperty="favouriteSection"column="author_favourite_section"/></association><collectionproperty="posts"ofType="Post"><idproperty="id"column="post_id"/><resultproperty="subject"column="post_subject"/><associationproperty="author"javaType="Author"/><collectionproperty="comments"ofType="Comment"><idproperty="id"column="comment_id"/></collection><collectionproperty="tags"ofType="Tag"><idproperty="id"column="tag_id"/></collection><discriminatorjavaType="int"column="draft"><casevalue="1"resultType="DraftPost"/></discriminator></collection></resultMap>
以上是“mybatis映射XML文件的示例分析”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。