当前位置: 首页 > news >正文

Mybatis 40_resultMap基础和@Results注解

40_resultMap基础和@Results注解

  • resultType和resultMap
    • <resultMap.../>元素
      • 项目0501resultMap
    • @Results注解
      • 项目0502resultMap注解(@Results)
    • 【总结】

resultType和resultMap

  • resultType指定结果集每条记录要映射的对象类型。
  • resultMap表明使用自定义的映射规则,resultMap属性值为一个<resultMap…/>元素的id属性值
      resultMap比指定resultType要稍微复杂一些,但它的功能更强大

<resultMap…/>元素

resultMap专门定义一个映射规则:完成结果集与Java对象之间的映射关系
必须指定如下属性:
- id:指定该<resultMap…/>的唯一标识。
- type:指定该<resultMap…/>映射的Java对象的类型。

<resultMap…/>的两个常用子元素:<id…/>和<result…/>,
<id…/>元素映射标识属性列,<result…/>映射普通列
它们都可指定以下属性:
- column:指定列名。
- property:指定对应的属性名。
- javaType:指定属性的Java类型。一般无需指定,MyBatis可以自动推断。
- jdbcType:指定该列的JDBC类型。一般无需指定,MyBatis可以自动推断。
- typeHandler:为该数据列与属性值之间的转换指定类型处理器。

项目0501resultMap

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 根元素是mapper,该元素的namespace属性值相当于该mapper的唯一标识 -->	
<mapper namespace="org.itcheng.app.dao.NewsMapper"><!-- SQL的id需要与Mapper接口的方法名相同  --><insert id="saveNews">insert into news_inf values (null, #{title}, #{content})</insert><!-- 定义SQL语句 --><update id="updateNews">update news_inf set news_title = #{title}, news_content = #{content}where news_id=#{id}</update><!-- SQL的id需要与Mapper接口的方法名相同  --><delete id="deleteNews">delete from news_inf where news_id = #{xyz} </delete><!-- 使用resultMap来完成结果集与Java对象之间的映射关系--><select id="findNews" resultMap="newsMap">select * from news_inf where news_id > #{id}		</select><!-- resultMap专门定义一个映射规则:完成结果集与Java对象之间的映射关系 --><resultMap type="news" id="newsMap" ><id column="news_id" property="id" /><!-- 映射映射标识属性 数据库的表字段news_id对应news对象的 id属性 --><result column="news_title" property="title"/><!-- 映射映射普通属性 --><result column="news_content" property="content"/><!-- 映射映射普通属性 --></resultMap></mapper>

@Results注解

<resultMap…/>元素对应于@Results注解
该注解的value属性可指定多个@Result注解
@Result相当于 <id…/>和<result…/>的综合体,当它的id属性为true,代表<id…/>元素。

项目0502resultMap注解(@Results)

package org.itcheng.app.dao;import java.util.List;import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.itcheng.app.domain.News;// Mapper组件相当于DAO组件
public interface NewsMapper
{@Insert("insert into news_inf values (null, #{title}, #{content})")int saveNews(News news);@Update("update news_inf set news_title = #{title}, news_content = #{content}\r\n" + "where news_id=#{id}")int updateNews(News news);@Delete("delete from news_inf where news_id = #{xyz}")void deleteNews(Integer id);@Select("select * from news_inf where news_id > #{id}")@Results({// id = "true"相当于<id.../>子元素@Result(column = "news_id", property = "id", id = true),//表字段news_id对应news对象的 id属性@Result(column = "news_title", property = "title"),@Result(column = "news_content", property = "content")})List<News> findNews(Integer id);}

【总结】

<resultMap…/>等同于@Results注解
<id…/>或 <result …/>等同于@Result注解

http://www.lryc.cn/news/281560.html

相关文章:

  • 【蓝桥杯选拔赛真题31】C++数位求和 第十三届蓝桥杯青少年创意编程大赛C++编程选拔赛真题解析
  • 牛客网-css继承属性
  • OpenCV-19图像的仿射变换
  • 数据结构与算法教程,数据结构C语言版教程!(第四部分、字符串,数据结构中的串存储结构)二
  • 第七在线荣获百灵奖 Buylink Awards 2023零售圈年度卓越服务商品牌
  • 通过myBatis将sql语句返回的值自动包装成一个java对象(3)
  • 基于SSM的驾校信息管理系统设计与实现
  • 矩阵行列式的四大应用
  • 【小笔记】时序数据分类算法最新小结
  • 使用Python+pygame实现贪吃蛇小游戏
  • SpringBoot 全局异常统一处理:BindException(绑定异常)
  • ucloud轻量云(wordpress)配置ssl
  • 电脑/设备网络共享给其他设备上网
  • vue之虚拟滚动
  • Redis学习指南(11)-Redis的有序集合数据类型介绍
  • Spring的纯注解配置
  • numpy 筛选多段数据
  • 【Kotlin】协程的字节码原理
  • 区间预测 | Matlab实现LSSVM-ABKDE的最小二乘支持向量机结合自适应带宽核密度估计多变量回归区间预测
  • 基于深度学习的实例分割的Web应用
  • 20240115如何在线识别俄语字幕?
  • Flink 处理函数(1)—— 基本处理函数
  • Linux系统下编译MPlayer
  • 事务的ACID属性是什么?为什么它们很重要?
  • 计算机毕业设计 基于Java的手机销售网站的设计与实现 Java实战项目 附源码+文档+视频讲解
  • Redis相关命令详解及其原理
  • go语言中的GoMock
  • DIFFWAVE: A VERSATILE DIFFUSION MODEL FOR AUDIO SYNTHESIS (Paper reading)
  • 排序算法8----归并排序(非递归)(C)
  • Golang 里的 context