属性
id
应该总是指定一个或多个可以唯一标识结果的属性。 虽然,即使不指定这个属性,MyBatis 仍然可以工作,但是会产生严重的性能问题。 只需要指定可以唯一标识结果的最少属性。显然,你可以选择主键(复合主键也可以)
result
注入到字段或 JavaBean 属性的普通结果
id/result 内部属性
Property 映射到列结果的字段或属性 bean
Column 数据表的列名
<resultMap type="com.itheima.domain.User" id="userMap">
<id column="id" property="userId"/>
<result column="username" property="userName"/>
</resultMap>
javaType
一个Java类的完全限定名,或一个类型别名。如果映射到一个JavaBean,myBatis通常可以断定类型
typeHandler
类型处理器。使用这个属性,可以覆盖默认的类型处理器。这个属性值是类的完全限定名或者是一个类型处理器的实现,或者是类型别名。
constructor
类在实例化时, 用来注入结果到构造方法中
idArg - ID 参数; 标记结果作为 ID 可以帮助提高整体效能
arg - 注入到构造方法的一个普通结果
public class User {
public User(Integer id, String username, int age) {
}
}
<constructor>
<idArg column="id" javaType="int"/>
<arg column="username" javaType="String"/>
<arg column="age" javaType="_int"/>
</constructor>
一对一查询
@Data
public class Account implements Serializable {
private Integer id;
private Integer uid;
private Double money;
private User user;
}
List<Account> findAll1();
<resultMap type="account" id="accountMap">
<id column="aid" property="id"/>
<result column="uid" property="uid"/>
<result column="money" property="money"/>
<association property="user" javaType="user">
<id column="id" property="userId"/>
<result column="username" property="userName"/>
<result column="sex" property="userSex"/>
<result column="birthday" property="userBirthday"/>
<result column="address" property="userAddress"/>
</association>
</resultMap>
<select id="findAll1" resultMap="accountMap">
select u.*,a.id as aid,a.uid,a.money from account a,user u where a.uid =u.id;
</select>
<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"/>
</resultMap>
一对多
@Data
public class User implements Serializable {
private Integer userId;
private String userName;
private String userAddress;
private String userSex;
private Date userBirthday;
private List<Account> accounts;
}
List<User> findAllAccount();
<resultMap type="user" id="userMap1">
<id column="id" property="userId"></id>
<result column="username" property="userName"/>
<result column="address" property="userAddress"/>
<result column="sex" property="userSex"/>
<result column="birthday" property="userBirthday"/>
<!-- collection 是用于建立一对多中集合属性的对应关系
ofType 用于指定集合元素的数据类型
-->
<collection property="accounts" ofType="account">
<id column="aid" property="id"/>
<result column="uid" property="uid"/>
<result column="money" property="money"/>
</collection>
</resultMap>
<select id="findAllAccount" resultMap="userMap1">
select u.*, a.id as aid, a.uid, a.money
from user u
left outer join account a on u.id = a.uid
</select>
多对多
多对多关系其实我们看成是双向的一对多关系。
中间表用户角色
@Data
public class Role {
private Integer roleId;
private String roleName;
private String roleDesc;
//多对多的关系映射:一个角色可以赋予多个用户
private List<User> users;
}
List<Role> findAll();
<!--定义 role 表的 ResultMap-->
<resultMap id="roleMap" type="role">
<id property="roleId" column="rid"></id>
<result property="roleName" column="role_name"></result>
<result property="roleDesc" column="role_desc"></result>
<collection property="users" ofType="user">
<id column="id" property="userId"></id>
<result column="username" property="userName"/>
<result column="address" property="userAddress"/>
<result column="sex" property="userSex"/>
<result column="birthday" property="userBirthday"/>
</collection>
</resultMap>
<!--查询所有-->
<select id="findAll" resultMap="roleMap">
select u.*, r.id as rid, r.role_name, r.role_desc
from role r
left outer join user_role ur on r.id = ur.rid
left outer join user u on u.id = ur.uid
</select>
鉴别器
switch 语句
<resultMap id="vehicleResult" type="Vehicle">
<id property="id" column="id" />
<result property="vin" column="vin"/>
<discriminator javaType="int" column="vehicle_type">
<case value="1" resultMap="carResult"/>
<case value="2" resultMap="truckResult"/>
</discriminator>
</resultMap>
<resultMap id="vehicleResult" type="Vehicle">
<id property="id" column="id" />
<result property="vin" column="vin"/>
<discriminator javaType="int" column="vehicle_type">
<case value="1" resultType="carResult">
<result property="doorCount" column="door_count" />
</case>
<case value="2" resultType="truckResult">
<result property="boxSize" column="box_size" />
<result property="extendedCab" column="extended_cab" />
</case>
<case value="3" resultType="vanResult">
<result property="powerSlidingDoor" column="power_sliding_door" />
</case>
<case value="4" resultType="suvResult">
<result property="allWheelDrive" column="all_wheel_drive" />
</case>
</discriminator>
</resultMap>