1
2 |
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" |
1 |
<mapper namespace= "com.pro.dao.UserDao" ></mapper> |
1
2
3
4
5
6
7
8
9
10
11 |
<select id= "selectPerson"
//唯一标识sql语句,与接口中的标识一致。 parameterType= "int" //入参类型 resultType= "hashmap" //期望返回类型的类名或别名,集合时,应该是集合可以包含的类型,而不能是集合本身 resultMap= "personResultMap" //引用resultMap标签定义 flushCache= "false" //设置为true,清空缓存 useCache= "true" //使用cache:本条结果将被缓存 timeout= "10000" fetchSize= "256" statementType= "PREPARED" //Statement,PreparedStatement 或 CallableStatement。 默认值:PREPARED resultSetType= "FORWARD_ONLY" > //FORWARD_ONLY|SCROLL_SENSITIVE|SCROLL_INSENSITIVE 中的一种。默认是不设置(驱动自行处理)。<br><br><strong>多表查询:</strong> |
<!-- 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>
1 |
selectKey:selectKey 元素将会首先运行, id 会被设置,然后插入语句 会被调用 |
1
2
3
4
5
6
7
8
9 |
<insert id= "insertAuthor" > <selectKey keyProperty= "id"
resultType= "int"
order= "BEFORE" > select CAST(RANDOM()* 1000000
as INTEGER) a from SYSIBM.SYSDUMMY1 </selectKey> insert into Author (id, username, password, email,bio, favourite_section) values (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR}) </insert> |
1
2
3
4
5
6 |
<sql id= "userColumns" > id,username,password </sql> <select id= "selectUsers"
resultType= "map" > select <include refid= "userColumns" /> from some_table where id = #{id} </select><br><insert id= "insertUser"
parameterType= "User" > <br> insert into users (id, username, password) values (#{id}, #{username}, #{password}) <br></insert> |
1 |
关联元素处理“有一个”类型的关系。 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 |
<resultMap id= "blogResult"
type= "Blog" > <id property= "id"
column= "blog_id"
/> <result property= "title"
column= "blog_title" /> <association property= "author"
column= "blog_author_id"
javaType= "Author"
resultMap= "authorResult" /> </resultMap> <resultMap id= "authorResult"
type= "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" /> </resultMap> <select id= "selectBlog"
resultMap= "blogResult" > 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 from Blog B left outer join Author A on B.author_id = A.id where B.id = #{id} </select> |
<cache/>
字面上看就是这样。这个简单语句的效果如下:
所有的这些属性都可以通过缓存元素的属性来修改。比如:
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
mybatis--mapper配置总结,布布扣,bubuko.com
原文:http://www.cnblogs.com/leeying/p/3577431.html