Mybatis学习 04 (结果集映射)

单表查询(数据库字段和实体类可以对应的情况)

<select id="getUserById" resultType="User">
	select *
    from user
    where id = #{userId}
</select>

1. 这里使用resultType中的User就是java中对应bean类
2. 这样操作这个实体类可以被映射到ResultSet上
3. 这样操作的时候mybatis会自动创建一个resultMap并且根据属性名成映射到实体类上,如果名称不匹配可以设置在sql上设置别名

<select id="getUserById" resultType="map">
	select *
    from user
    where id = #{userId}
</select>

1. resultType也可以直接映射为map
2. 这样可以把所有的值都映射到HashMap上

多表查询(当数据库字段和实体类不可以对应的情况)

  1. 一个实体类中包含另一个实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;

    private String name;

    private String password;

    private UserType userType;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserType {
    private Integer id;

    private Integer userId;

    private String name;
}

1. 当前User类中还有一个UserType的类
2. 比如现在的查询条件是根据id查询User中对应数据同时查询UserType中的关联数据,这个时候单独使用resultType就不好处理啦,这个时候应该使用resultMap来处理

<select id="getUserById" resultMap="useResult">
	select u.id, u.name, u.pwd, t.name tname
	from user u
	left join user_type t
	on u.id = t.user_id
	where u.id = #{userId}
</select>
<resultMap id="useResult" type="User">
	<result property="id" column="id"/>
	<result property="name" column="name"/>
	<result property="password" column="pwd"/>
    	<association property="userType" javaType="UserType">
    		<result property="userId" column="user_id"/>
    		<result property="name" column="tname"/>
    	</association>
</resultMap>

4. 在查询多张表的时候如果多个表中字段有重名记得重命名
5. resultMap="useResult"中的useResult就是对应resultMap标签中的id,resultMap标签中的type就是对应最终要返回的实体类
6. result表示一个一个对应的返回值,property表示实体类字段名称,column表示数据库字段达成对应关系
7. association表示一个对象因为上文中User类中的是一个对象所以用这个标签,property在User这个类中当前实体类的名称, javaType表示当前对应的实体类
  1. 一个实体类中包含一个集合
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Integer id;

    private String name;

    private String password;

    private List<UserType> userList;
}

@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserType {
    private Integer id;

    private Integer userId;

    private String name;
}

1. 当前User类中还有一个userList

<select id="getUserById" resultMap="useResult">
	select u.id, u.name, u.pwd, t.name tname
	from user u
	left join user_type t
	on u.id = t.user_id
	where u.id = #{userId}
</select>
<resultMap id="useResult" type="User">
	<result property="id" column="id"/>
	<result property="name" column="name"/>
	<result property="password" column="pwd"/>
    	<collection property="userList" ofType="UserType">
    		<result property="userId" column="user_id"/>
    		<result property="name" column="tname"/>
    	</collection>
</resultMap>

1. collection表示集合,property表示在User类中这个实体类的名称,ofType表示当前对应的实体类
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值