目录
resultType
特性 | 描述 | 用法示例 |
---|---|---|
resultType | 指定查询结果的返回类型为com.example.User 实体类。 | <select id="selectUser" resultType="com.example.User"> SELECT id, username, email FROM user WHERE id = #{id} </select> |
resultType | 返回单个字符串类型的结果。 | <select id="selectUserName" resultType="java.lang.String"> SELECT name FROM user WHERE id = #{id} </select> |
resultType | 返回一个List 集合,集合元素类型为查询上下文中的实体类。 | <select id="selectAllUsers" resultType="java.util.List"> SELECT * FROM user </select> |
resultType | 返回一个Map ,键是列名,值是对应的值。 | <select id="selectUserAsMap" resultType="java.util.Map"> SELECT * FROM user WHERE id = #{id} </select> |
MyBatis 在定义 SQL 映射语句时,可以通过 `resultType` 属性指定查询结果集的返回类型。以下是一些常见的 `resultType` 返回值类型及其用法的例子:
返回基本类型或其包装类
<!-- 返回单个整数 -->
<select id="countUsers" resultType="int">
SELECT COUNT(*) FROM user
</select>
<!-- 返回单个字符串 -->
<select id="selectUserName" resultType="java.lang.String">
SELECT name FROM user WHERE id = #{id}
</select>
返回实体类对象
<!-- 返回用户实体 -->
<select id="selectUser" parameterType="int" resultType="com.example.User">
SELECT id, name, email FROM user WHERE id = #{id}
</select>
在这个例子中,`com.example.User` 是一个完整的类路径,MyBatis 会自动将结果集的列映射到 `User` 类的属性中。
返回集合类型
<!-- 返回用户实体列表 -->
<select id="selectAllUsers" resultType="java.util.List">
SELECT * FROM user
</select>
在这个例子中,MyBatis 会默认将结果集映射到一个 `List` 集合中,集合中的元素类型依赖于查询的上下文。
返回 Map 类型
<!-- 返回一条记录,其中键是列名,值是对应的值 -->
<select id="selectUserAsMap" resultType="java.util.Map">
SELECT * FROM user WHERE id = #{id}
</select>
<!-- 返回多条记录的 Map 列表 -->
<select id="selectAllUsersAsMaps" resultType="java.util.List">
SELECT * FROM user
</select>
在第一个例子中,结果集将被映射到一个 `Map` 对象中,其中键是列名,值是对应的值。在第二个例子中,每一行都会被映射到一个 `Map` 中,并且所有的 `Map` 将会被收集到一个 `List` 中。
注意
- 当返回单行结果集时,`resultType` 可以是任何 Java 类型。
- 当返回多行结果集时,`resultType` 通常是集合类型,如 `List`、`Set` 等,或者是数组类型。
- MyBatis 会自动处理基本类型和包装类之间的转换。
- 当使用自定义的实体类时,需要确保结果集中的列名和实体类中的属性名能够对应上,或者使用 `resultMap` 来进行详细的映射配置。
使用正确的 `resultType` 是确保 MyBatis 能够正确地将 SQL 查询结果映射到 Java 类型的关键。
resultMap
特性 | 描述 | 用法示例 |
---|---|---|
resultMap | 定义了如何将查询结果映射到com.example.User 类的实例中。 | <resultMap id="UserResultMap" type="com.example.User"> <id column="id" property="id" /> <result column="username" property="userName" /> <result column="email_address" property="email" /> </resultMap> |
resultMap | 映射了一个包含Author 对象的Blog 对象,使用association 元素处理对象关联。 | <resultMap id="BlogResultMap" type="Blog"> <association property="author" javaType="Author"> <id column="author_id" property="id" /> <result column="author_name" property="name" /> </association> </resultMap> |
resultMap | 映射了一个User 对象,其中包含一个Blog 对象的列表,使用collection 元素处理一对多关系。 | <resultMap id="UserResultMap" type="com.example.User"> <collection property="blogs" ofType="Blog"> <id column="blog_id" property="id" /> <result column="blog_title" property="title" /> </collection> </resultMap> |
resultMap | 使用构造器映射,允许通过数据库列直接调用实体类的构造函数创建实例。 | <resultMap id="UserResultMap" type="com.example.User"> <constructor> <idArg column="id" javaType="int"/> <arg column="username" javaType="String"/> </constructor> <result column="email_address" property="email" /> </resultMap> |
在MyBatis中,`resultMap`是一个非常强大的映射工具,它允许你详细地指定如何从数据库结果集中映射列到Java对象的属性。下面是一些`resultMap`的用法示例:
基本的ResultMap
<resultMap id="UserResultMap" type="com.example.User">
<id column="id" property="id" />
<result column="username" property="userName" />
<result column="email_address" property="email" />
</resultMap>
<select id="selectUser" resultMap="UserResultMap">
SELECT id, username, email_address FROM user WHERE id = #{id}
</select>
在这个例子中,`UserResultMap` 定义了如何将查询结果映射到 `com.example.User` 类的实例中。`id` 元素用于标识主键字段,`result` 元素用于普通字段。
高级ResultMap - 关联映射
<resultMap id="BlogResultMap" type="Blog">
<id column="blog_id" property="id" />
<result column="title" property="title" />
<!-- 关联一个作者对象 -->
<association property="author" javaType="Author">
<id column="author_id" property="id" />
<result column="author_name" property="name" />
<result column="author_email" property="email" />
</association>
</resultMap>
<select id="selectBlog" resultMap="BlogResultMap">
SELECT b.id AS blog_id, b.title, a.id AS author_id, a.name AS author_name, a.email AS author_email
FROM Blog b
LEFT JOIN Author a ON b.author_id = a.id
WHERE b.id = #{id}
</select>
在这个例子中,`BlogResultMap` 映射了一个包含 `Author` 对象的 `Blog` 对象。`association` 元素用于处理对象之间的关联。
高级ResultMap - 集合映射
<resultMap id="UserResultMap" type="com.example.User">
<id column="user_id" property="id" />
<result column="username" property="userName" />
<result column="email_address" property="email" />
<!-- 用户的博客列表 -->
<collection property="blogs" ofType="Blog">
<id column="blog_id" property="id" />
<result column="blog_title" property="title" />
</collection>
</resultMap>
<select id="selectUserWithBlogs" resultMap="UserResultMap">
SELECT u.id AS user_id, u.username, u.email_address,
b.id AS blog_id, b.title AS blog_title
FROM user u
LEFT JOIN blog b ON u.id = b.user_id
WHERE u.id = #{id}
</select>
在这个例子中,`UserResultMap` 映射了一个 `User` 对象,其中包含一个 `Blog` 对象的列表。`collection` 元素用于处理一对多的关系。
使用构造器
<resultMap id="UserResultMap" type="com.example.User">
<constructor>
<idArg column="id" javaType="int"/>
<arg column="username" javaType="String"/>
</constructor>
<result column="email_address" property="email" />
</resultMap>
<select id="selectUser" resultMap="UserResultMap">
SELECT id, username, email_address FROM user
</select>
在这个例子中,`UserResultMap` 使用了构造器映射,允许你通过数据库列直接
有用请点赞,养成良好习惯!
疑问、交流、鼓励请留言!