mybatis映射XML文件详解及实例
程序员文章站
2022-06-24 13:43:19
mybatis映射xml文件
一个简单的映射文件:
...
mybatis映射xml文件
一个简单的映射文件:
<?xml version="1.0" encoding="utf-8" ?> <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="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
简单的例子:
<select id="selectperson" parametertype="int" resulttype="hashmap"> select * from person where id = #{id} </select>
select也有很多属性可以让你配置:
<select id="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
<insert id="insertauthor" parametertype="domain.blog.author" flushcache="true" statementtype="prepared" keyproperty="" keycolumn="" usegeneratedkeys="" timeout="20"> <update id="updateauthor" parametertype="domain.blog.author" flushcache="true" statementtype="prepared" timeout="20"> <delete id="deleteauthor" parametertype="domain.blog.author" flushcache="true" statementtype="prepared" timeout="20">
语句:
<insert id="insertauthor"> insert into author (id,username,password,email,bio) values (#{id},#{username},#{password},#{email},#{bio}) </insert> <update id="updateauthor"> update author set username = #{username}, password = #{password}, email = #{email}, bio = #{bio} where id = #{id} </update> <delete id="deleteauthor"> delete from author where id = #{id} </delete>
f your database supports auto-generated key fields (e.g. mysql and sql server),上面的插入语句可以写成:
<insert id="insertauthor" usegeneratedkeys="true" keyproperty="id"> insert into author (username,password,email,bio) values (#{username},#{password},#{email},#{bio}) </insert>
如果你的数据库还支持多条记录插入,可以使用下面这个语句:
<insert id="insertauthor" usegeneratedkeys="true" keyproperty="id"> insert into author (username, password, email, bio) values <foreach item="item" collection="list" separator=","> (#{item.username}, #{item.password}, #{item.email}, #{item.bio}) </foreach> </insert>
sql
这个element可以定义一些sql代码的碎片,然后在多个语句中使用,降低耦合。比如:
<sql id="usercolumns"> ${alias}.id,${alias}.username,${alias}.password </sql>
然后在下面的语句中使用:
<select id="selectusers" resulttype="map"> select <include refid="usercolumns"><property name="alias" value="t1"/></include>, <include refid="usercolumns"><property name="alias" value="t2"/></include> from some_table t1 cross join some_table t2 </select>
result maps
官网给了个最最复杂的例子
大体意思呢就是一个博客系统有一个作者,很多博文,博文中有一个作者,很多评论,很多标签(包括了一对多,一对一)
<!-- very complex statement --> <select id="selectblogdetails" resultmap="detailedblogresultmap"> select b.id as blog_id, b.title as blog_title, b.author_id as blog_author_id, a.id as author_id, a.username as author_username, a.password as author_password, a.email as author_email, a.bio as author_bio, a.favourite_section as author_favourite_section, p.id as post_id, p.blog_id as post_blog_id, p.author_id as post_author_id, p.created_on as post_created_on, p.section as post_section, p.subject as post_subject, p.draft as draft, p.body as post_body, c.id as comment_id, c.post_id as comment_post_id, c.name as comment_name, c.comment as comment_text, t.id as tag_id, t.name as tag_name from blog b left outer join author a on b.author_id = a.id left outer join post p on b.id = p.blog_id left outer join comment c on p.id = c.post_id left outer join post_tag pt on pt.post_id = p.id left outer join tag t on pt.tag_id = t.id where b.id = #{id} </select> <!-- very complex result map --> <resultmap id="detailedblogresultmap" type="blog"> <constructor> <idarg column="blog_id" javatype="int"/> </constructor> <result property="title" column="blog_title"/> <association property="author" javatype="author"> <id property="id" column="author_id"/> <result property="username" column="author_username"/> <result property="password" column="author_password"/> <result property="email" column="author_email"/> <result property="bio" column="author_bio"/> <result property="favouritesection" column="author_favourite_section"/> </association> <collection property="posts" oftype="post"> <id property="id" column="post_id"/> <result property="subject" column="post_subject"/> <association property="author" javatype="author"/> <collection property="comments" oftype="comment"> <id property="id" column="comment_id"/> </collection> <collection property="tags" oftype="tag" > <id property="id" column="tag_id"/> </collection> <discriminator javatype="int" column="draft"> <case value="1" resulttype="draftpost"/> </discriminator> </collection> </resultmap>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
上一篇: mysql分表分库的应用场景和设计方式
下一篇: Android实现隐私政策弹窗与链接功能