ResutType
resultType 可以把查询结果封装到pojo类型中,但必须 pojo 类的属性名和查询到的数据库表的字段名一致。
如果 sql 查询到的字段与 pojo 的属性名不一致,则需要使用 resultMap。
返回的类型有:
一般数据类型
<!-- 指定 resultType 返回值类型时 String类型的,
int在这里是一个别名,代表的是 java.lang.String
对于引用数据类型,都是将大写字母转小写,比如 HashMap 对应的别名是 'hashmap'
基本数据类型考虑到重复的问题,会在其前面加上 '_',比如 byte 对应的别名是 '_byte'
-->
<select id="queryUserNameById" resultType="string">
select username from user where id = #{id}
</select>
Dao接口
String queryUserNameById(Integer id);
JavaBean 类型,注意这里实体类中的属性名需要和数据库中的字段名一样。
<select id="queryUserById" resultType="com.demo.pojo.User">
select * from user where id = #{id}
</select>
Dao接口
User queryUserById(Integer id);
List 集合类型,这里和上面一样的就好
<select id="queryUser" resultType="com.demo.pojo.User">
select * from user
</select>
Dao接口
List<User> queryUser(Integer id);
Map类型
<select id="queryUserAsMap" resultType="map">
select * from user where id = #{id}
</select>
Dao接口
Map<String, Object> queryUserAsMap(Integer id);
ResutMap
resultMap 将字段名和属性名对应起来,进行手动配置封装,将结果映射到 pojo 中, resultMap 是 Mybatis 最强大的元素,它可以将查询到的复杂数据(比如查询到几个表中数据)映射到一个结果集当中。
ResultMap标签的结构如下
<!--column不做限制,可以为任意表的字段,而property须为type 定义的pojo属性-->
<resultMap id="唯一的标识" type="映射的pojo对象">
<id column="表的主键字段,或者可以为查询语句中的别名字段" jdbcType="字段类型" property="映射pojo对象的主键属性" />
<result column="表的一个字段(可以为任意表的一个字段)" jdbcType="字段类型" property="映射到pojo对象的一个属性(须为type定义的pojo对象中的一个属性)"/>
<association property="pojo的一个对象属性" javaType="pojo关联的pojo对象">
<id column="关联pojo对象对应表的主键字段" jdbcType="字段类型" property="关联pojo对象的主席属性"/>
<result column="任意表的字段" jdbcType="字段类型" property="关联pojo对象的属性"/>
</association>
<!-- 集合中的property须为oftype定义的pojo对象的属性-->
<collection property="pojo的集合属性" ofType="集合中的pojo对象">
<id column="集合中pojo对象对应的表的主键字段" jdbcType="字段类型" property="集合中pojo对象的主键属性" />
<result column="可以为任意表的字段" jdbcType="字段类型" property="集合中的pojo对象的属性" />
</collection>
</resultMap>
一、基本使用
· 假设我们数据库有一张用户表(user),有如下字段
_id int32
username varchar(25)
password varchar(25)
那么该表(User)对应的实体类
public class User{
Integer id;
String username;
String password;
getter and setter........
}
//1. 实体类中属性名尽量和数据表中的列名有点联系,不是必须,只是为了避免自己搞混了。
//2. 实体类必须要有无参构造函数,如果你添加了带参数的构造函数的话。
比如我们要查询指定id的用户信息,可以通过下面的sql语句
<select id="queryUserById" parameterType="java.lang.Integer" resultMap="UserResultMap">
select * from user where _id = #{id, jdbcType=INTEGER};
</select>
然后查询的结果通过resultMap映射到实体类返回
<!-- resultMap最终还是要将结果映射到pojo上,type就是指定映射到哪一个pojo -->
<!-- id:设置ResultMap的id -->
<resultMap type="User" id="UserResultMap">
<!-- <id />标签 定义主键 ,非常重要。
property:主键在pojo中的属性名
column:主键在数据库中的列名
jdbcType: 字段类型-->
<id property="id" column="_id" jdbcType="VARCHAR"/>
<!-- <result />标签 定义普通属性
property:pojo中的属性名
column:数据库中的列名
jdbcType: 字段类型-->
<result property="username" column="username" />
<result property="password" column="password" />
</resultMap>
resultMap 的 type 属性指向映射的实体类,注意:这里是使用了别名后的写法,如果没有在配置文件中配置别名,会报找不到类的的错误。没有配置的时候需要写完全的路径名称。比如:type=“com.demo.pojo.User” 。属性 id 是唯一的,方便调用。
对应的接口方法为
User queryUserById(Integer id);
二、一对一
假设我们需要查询用户的时候顺便把用户家庭住址也查询出来。
在数据库中用户表(user)的字段如下,
_id INT32
username varchar(25)
password varchar(25)
address_id int32 //加多一个地址表的主键映射
地址表(address)
_id INT32
address_name varchar(255)
remark varchar(255)
对应 User 表的实体类,在用户类中添加 Address 属性, Address 属性是一个引用类型,用于存储关联查询的地址信息,因为关联关系是一对一,所以只需要添加单个属性即可
public class User{
Integer id;
String username;
String password;
Address address; //
getter and setter........
}
Address 表的实体类
public class Address{
Integer id;
String address_name;
String remark ;
getter and setter ....
}
查询的sql语句如下:
<select id="queryUserById" parameterType="java.lang.Integer" resultMap="UserAddressResultMap">
select u._id uid,u.username,u.password,a._id aid,a.address_name,a.remark
from user u,address a
where u.address_id =a._id
and u._id =#{id, jdbcType=INTEGER};
</select>
对应的的Dao接口
public interface UserDao{
User queryUserById(Integer id);
}
配置ResultMap,使用 association 映射关联对象Address
<resultMap type="com.demo.pojo.User" id="UserAddressResultMap">
<id property="id" column="uid" jdbcType="VARCHAR"/>
<result property="username" column="username" />
<result property="password" column="password" />
<!-- association :配置一对一属性
property:User里面的address属性名
javaType:属性类型 -->
<association property="address" javaType="com.demo.pojo.Address">
<!-- <id/>:声明主键,表示aid是关联查询对象的唯一标识,-->
<id property="id" column="aid" />
<result property="address_name" column="address_name" />
<result property="remark " column="remark " />
</association>
</resultMap>
也可以在 association 上使用ResultMap来进行重用,提高代码可读性。比如这样
............................
<association property="address" resultMap="AddressResult">
</association>
..................................
<resultMap type="com.demo.pojo.Address" id="AddressResult">
<id property="id" column="aid" />
<result property="address_name" column="address_name" />
<result property="remark " column="remark " />
</resultMap>
............................
上面中使用了 association 这个元素,定义关联对象的封装规则 ,实现关联属性的查询,使用 association 定义关联的单个对象的封装结果。如果你关联的不只是一个对象,那你可以添加多个 associaton 来关联 。
association 有两种使用形式,上面使用的是 嵌套结果,还有一种是 嵌套查询 的。还是上面的例子,修改一下
太多了
链接如下:
https://blog.csdn.net/weixin_40340362/article/details/93128692