标签:style blog io ar color 使用 sp java on
mybatis 查询思路
第一种思路:
1:先查询,编写查询语句;
2:查询出结果后,将ResultMap 进行拆分, 组合成想要的模型;
这种思路是将联合查询语句组合成一个ResultMap;
<select id="selectBlog" parameterType="int" 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> 注意这个连接,注意结果都被别名为一个唯一且明确的名称。这将使映射变得更多容易。我们可以这样 做: <resultMap id="blogResult" type="Blog"> <id property=”blog_id” column="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>
第二种思路:
1:写两个查询语句两个<select>;
2:一个结果集<ResultMap>;结果集理组合对象;
示例: <resultMap id=”blogResult” type=”Blog”> <association property="author" column="blog_author_id" javaType="Author" select=”selectAuthor”/> </resultMap> <select id=”selectBlog” parameterType=”int” resultMap=”blogResult”> SELECT * FROM BLOG WHERE ID = #{id} </select> <select id=”selectAuthor” parameterType=”int” resultType="Author"> SELECT * FROM AUTHOR WHERE ID = #{id} </select>
按照mybatis说明文档的写法是:
不同的地方是你需要告诉MyBatis 如果加载一个联合。MyBatis 可以用两种方式加载:
?? Nested Select: 执行一个其它映射的SQL 语句返回一个期望的复杂类型
?? Nested Results: 使用一个嵌套的结果映射来处理交合的结果的重复的子集
标签:style blog io ar color 使用 sp java on
原文地址:http://www.cnblogs.com/aquariusm/p/4159700.html