MyBatis -- ResultMap 学习

在mybatis默认中采用 Auto Mapping特性进行对数据库查询结果与实体类的映射,这时要求实体类所有的属性必须与查询得到的列的名字相同,否则就会出现空值。

使用ResultMap标检可以进行自定义映射关系

1.单表映射

      1.1  数据库设计--student表

               

       1.2  实体类设计

                   

        1.3  采用 mybatis 默认设置查询所得结果

                ------- 可以看到当实体类的属性与查询所得列名不同时是无法接受到参数值得。

                   

         1.4  ResultMap 标签 进行自定义

<mapper namespace="com.yyl.Mapper.StudentMapper">
		<resultMap type="student" id="stuMap"> // type 表示返回值类型  id 表示此resultmap名字
			<id column="id" property="sid"/>   // id 标签表示 主键  column 表示 列名
			<result column="name" property="sname"/> //property 表示 实体类的属性名
			<result column="age" property="age"/>
			<result column="tid" property="tid"/>
		</resultMap>
		<select id="selAll" resultMap="stuMap">  // resultMap需要与上面的对应
			select * from student
		</select>
</mapper>

         1.5 结果

             

2.使用ResultMap实现关联单个对象

     即查询出一个表的信息,在根据这个表的信息查询出另一个表的信息。

        2.1 创建另一个实体类 -- Teacher  

               学生与教师之间关系为多对多

               在原来的student中增加Teacher 

               

               

              数据库设计如下:

             

        2.2  自定义 ResultMap标签

// <association> property 返回值类型  column 表示 传给另一个查询参数 
// select 第二个查询语句所在位置   

<mapper namespace="com.yyl.Mapper.StudentMapper">
		<resultMap type="student" id="stuMap">
			<id column="id" property="sid"/>
			<result column="name" property="sname"/>
			<result column="age" property="age"/>
			<result column="tid" property="tid"/>
			<association property="teacher" column="tid" 
                         select="com.yyl.Mapper.TeacherMapper.selById">
			</association>
		</resultMap>
		
		<select id="selAll" resultMap="stuMap">
			select * from student
		</select>
		
</mapper>

              2.3 结果

                   

3.ResultMap 查询关联集合对象

           3.1  在Teacher 表中添加 List<Student>

                 教师与学生之间的关系为 1 对 多

                

            3.2  自定义映射

// <collectio> 集合  property 表示 属性  column 表示 传给select的参数  ofType 表示集合泛型
// select 同上

<mapper namespace="com.yyl.Mapper.TeacherMapper">
  	<resultMap type="teacher" id="teaMap">
  		<id column="id" property="id"/>
  		<result column="name" property="name"/>
  		<collection property="list" column="id" ofType="student" 
           select="com.yyl.Mapper.StudentMapper.selById"></collection>
  	</resultMap>

           3.3 结果

                 

4.ResultMap 加载集合数据 

             ------------ 两表联合查询

          4.1  自定义ResultMap

<resultMap type="teacher" id="teaMap2">
  		<id column="tid" property="id"/>
  		<result column="tname" property="name"/>
		<collection property="list" ofType="student">
  			<id column="sid" property="id"/> //返回结果为Student 确定student的属性之间映射关系
  			<result column="sname" property="name"/>
  			<result column="sage" property="age"/>
  			<result column="tid" property="tid"/>
  		</collection>
</resultMap>

       4.2 SQL语句

<select id="selTea" resultMap="teaMap2">
  	select t.id tid,t.name tname,s.id sid,s.name sname,s.age sage, tid from teacher t left 
    join student s on t.id = s.tid
</select>

      4.3 运行结果

5 使用Auto Mapping 和别名实现多表查询

       5.1 sql 语句

<select id="selStu" resultType="student">
	select t.id `teacher.id`,t.name `teacher.name`,s.id sid,s.name sname,s.age                 
sage,tid from student s left join teacher t on s.tid = t.id		
</select>

       5.2 结果

              

6. MyBatis 注解

      6.1 建立Mapper 接口


	@Select("select * from student")
	List<Student> selAllStu();
	@Insert("SQL 语句")
	int insStu();
	@Delete("SQL 语句")
	int delStu();
	@Update("SQL 语句")
	int upStu(String name,int age,int tid);

----------------------------------------------------------
    相当于 collection

    @Results(value={
	    @Result(id=true,column="",property=""), // id 为true 表示 主键 
	    @Result(column="",property=""),	    // column 与 property 与上面作用相同    
	    @Result(column="",property="",many=@Many(select="")),	// many 相当于 collection
	})
	@Select("SQL 语句")

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis-Plus 是一个 MyBatis 的增强工具,它提供了一系列增强功能来简化 MyBatis 的使用。其中之一就是提供了一个比 MyBatis 更加方便的结果映射方式,就是通过 `ResultMap`。 `ResultMap` 是 MyBatis 提供的结果映射器,它可以将数据库查询返回的结果映射成 Java 对象。MyBatis-Plus 的 `ResultMap` 功能就是在 MyBatis 的 `ResultMap` 基础上进行的增强。 在 MyBatis-Plus 中,我们可以通过 `@TableName` 注解来指定实体类和数据库表的映射关系,然后使用 `@TableField` 注解来指定实体类中的属性和数据库表中的字段的映射关系。 使用 MyBatis-Plus 的 `ResultMap` 功能时,我们只需要在实体类中定义一个 `ResultMap`,然后在查询语句中使用该 `ResultMap` 即可将查询结果映射成 Java 对象。 以下是一个使用 MyBatis-Plus `ResultMap` 的示例: ```java @TableName("user") public class User { @TableId(value = "id", type = IdType.AUTO) private Long id; @TableField("username") private String username; @TableField("password") private String password; // getter 和 setter 省略 } // 定义 ResultMap private static final ResultMap USER_RESULT_MAP = new ResultMap.Builder(configuration, "userResultMap", User.class, new ArrayList<ResultMapping>()) .id(new ResultMapping.Builder(configuration, "id", "id", Long.class).build()) .result(new ResultMapping.Builder(configuration, "username", "username", String.class).build()) .result(new ResultMapping.Builder(configuration, "password", "password", String.class).build()) .build(); // 使用 ResultMap 查询 List<User> userList = sqlSession.selectList("com.example.mapper.UserMapper.selectUserList", null, new RowBounds(0, 10), USER_RESULT_MAP); ``` 在上面的示例中,我们首先使用 `@TableName` 和 `@TableField` 注解指定了实体类和数据库表之间的映射关系。然后我们定义了一个 `ResultMap`,并在其中指定了实体类中的属性和数据库表中的字段的映射关系。最后我们在查询语句中使用该 `ResultMap` 将查询结果映射成 Java 对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值