标签:
collection的使用有两种resultMap和select,必须手动指定一种
1. 实体类:
1 package com.mrlu.mybatis.domain; 2 3 import java.util.List; 4 5 /** 6 * Created by stefan on 15-12-31. 7 */ 8 public class User { 9 private Integer id; 10 private String name; 11 private List<Account> accounts; 12 13 public Integer getId() { 14 return id; 15 } 16 17 public void setId(Integer id) { 18 this.id = id; 19 } 20 21 public String getName() { 22 return name; 23 } 24 25 public void setName(String name) { 26 this.name = name; 27 } 28 29 @Override 30 public String toString() { 31 return "User{" + 32 "id=" + id + 33 ", name=‘" + name + ‘\‘‘ + 34 ", accounts=" + accounts + 35 ‘}‘; 36 } 37 38 public List<Account> getAccounts() { 39 return accounts; 40 } 41 42 public void setAccounts(List<Account> accounts) { 43 this.accounts = accounts; 44 } 45 46 }
package com.mrlu.mybatis.domain; /** * Created by stefan on 15-12-31. */ public class Account { private Integer id; private Integer userId; private String num; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } @Override public String toString() { return "Account{" + "id=" + id + ", userId=" + userId + ", num=‘" + num + ‘\‘‘ + ‘}‘; } }
2. DAO:
package com.mrlu.mybatis.dao; import com.mrlu.mybatis.domain.User; import java.util.List; /** * Created by stefan on 15-12-31. */ public interface UserDao { public void insert(User user); public List<User> selectAll(); }
package com.mrlu.mybatis.dao; import com.mrlu.mybatis.domain.Account; /** * Created by stefan on 15-12-31. */ public interface AccountDao { public void insert(Account account); }
3. mapper.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.mrlu.mybatis.dao.AccountDao" >
<insert id="insert" useGeneratedKeys="true" keyProperty="id">
INSERT INTO account(user_id, num) VALUES(
#{userId}, #{num}
)
</insert>
</mapper>
resultMap:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.mrlu.mybatis.dao.UserDao" > 4 <resultMap id="BaseResultMap" type="com.mrlu.mybatis.domain.User"> 5 <result column="id" property="id" /> 6 <result column="name" property="name" /> 7 <collection property="accounts" ofType="com.mrlu.mybatis.domain.Account" javaType="java.util.ArrayList" resultMap="accountMap"> 8 <result column="id" property="id" /> 9 <result column="user_id" property="userId" /> 10 <result column="num" property="num" /> 11 </collection> 12 </resultMap> 13 <resultMap id="accountMap" type="com.mrlu.mybatis.domain.Account"> 14 <result column="id" property="id" /> 15 <result column="user_id" property="userId" /> 16 <result column="num" property="num" /> 17 </resultMap> 18 <insert id="insert" keyProperty="id" useGeneratedKeys="true"> 19 INSERT INTO user(name) VALUES( 20 #{name} 21 ) 22 </insert> 23 24 <select id="selectAll" resultMap="BaseResultMap"> 25 SELECT a.id,a.user_id,a.num FROM user t 26 RIGHT JOIN account a ON t.id=a.user_id 27 </select> 28 </mapper>
select:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.mrlu.mybatis.dao.UserDao" > 4 <resultMap id="BaseResultMap" type="com.mrlu.mybatis.domain.User"> 5 <result column="id" property="id" /> 6 <result column="name" property="name" /> 7 <collection property="accounts" column="id" select="selectAccount"> 8 <result column="id" property="id" /> 9 <result column="user_id" property="userId" /> 10 <result column="num" property="num" /> 11 </collection> 12 </resultMap> 13 <insert id="insert" keyProperty="id" useGeneratedKeys="true"> 14 INSERT INTO user(name) VALUES( 15 #{name} 16 ) 17 </insert> 18 19 <select id="selectAll" resultMap="BaseResultMap"> 20 SELECT * FROM user t 21 </select> 22 23 <select id="selectAccount" resultType="com.mrlu.mybatis.domain.Account"> 24 SELECT * FROM account 25 WHERE user_id = #{id} 26 </select> 27 </mapper>
其中标出的字段都是必须的,没标出的都是可选的
关于mybatis中mapper文件resultMap中collection的使用
标签:
原文地址:http://www.cnblogs.com/stefanking/p/5092474.html