mybatis 多对一、一对一(association)和一对多 (collection)
多对一、一对一 使用 association
一对多 使用 collection
collection使用:
可多层嵌套
<collection property="list" javaType="list" ofType="User">
<id property="id" column="u_id" />
<result property="name" column="u_name"/>
<collection property="list" jdbcType="list" ofType="Menu">
<id property="id" column="m_id"></id>
<result property="name" column="m_name" />
</collection>
</collection>
</resultMap>
<select id="findCkass" resultMap="aaa">
select * from ckass ks join user u on ks.id=u.ckass_id join menu m on u.id=m.uid where ks.id=#{orgId}
</select>
注意:各个表之间尽量不要有重名字段,包括主键id,不然可能会造成数据混乱错误;
JavaType和ofType都是用来指定对象类型的
JavaType是用来指定pojo中属性的类型
ofType指定的是映射到list集合属性中pojo的类型。
association使用:
一对一:
public class User {
// ID,唯一性
private int id;
// 登录ID
private String loginId;
// 用户名
private String userName;
// 角色
private Role role;
// 备注
private String note;
public User(){
}
public User(int id, String loginId, String userName, Role role, String note) {
this.id = id;
this.loginId = loginId;
this.userName = userName;
this.role = role;
this.note = note;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getLoginId() {
return loginId;
}
public void setLoginId(String loginId) {
this.loginId = loginId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
@Override
public String toString() {
return "User [id=" + id + ", loginId=" + loginId + ", userName="
+ userName + ", role=" + role + ", note=" + note + "]";
}
}
public class Role {
private int id;
private String roleName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public Role(){
}
public Role(int id, String roleName) {
this.id = id;
this.roleName = roleName;
}
@Override
public String toString() {
return "Role [id=" + id + ", roleName=" + roleName + "]";
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<resultMap type="com.queen.mybatis.bean.User" id="userResultMap">
<id property="id" column="id"/>
<result property="loginId" column="loginId" />
<result property="userName" column="userName"/>
<result property="note" column="note"/>
<!--assocication可以指定联合的JavaBean对象
property="role"指定哪个属性是联合的对象
javaType:指定这个属性对象的类型
-->
<association property="role" javaType="com.queen.mybatis.bean.Role">
<id column="role_id" property="id"/>
<result column="roleName" property="roleName"/>
</association>
</resultMap>
<select id="getUserById" resultMap="userResultMap">
select m.id id, m.loginId loginId, m.userName userName, m.roleId roleId,m.note note, n.id role_id, n.roleName roleName
from t_user m left join t_role n on m.roleId=n.id
where m.id=#{id}
</select>
多对一:实际不还是一对一?(多个学生一个语文老师,实体类中只要一个语文老师对象就可以了。而一对多是:一个语文老师有多个学生,所以实体类中有个list。一个对象和list才是区分一对多和多对一的本质吧???),写法和一对一一样
版权声明:本文为weixin_44315847原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。