0
点赞
收藏
分享

微信扫一扫

Mybatis--resultType和resultMap

程序员知识圈 2022-03-23 阅读 73


简介       

        使用MyBatis查询数据库记录时,返回类型常用的有两种:resultType和resultMap。

        MyBatis的每一个查询映射的返回类型都是ResultMap,只是当我们提供的返回类型属性是resultType的时候,MyBatis对自动的给我们把对应的值赋给resultType所指定对象的属性,而当我们提供的返回类型是resultMap的时候,将数据库中列数据复制到对象的相应属性上,可以用于复制查询,两者不能同时用。

返回一个值

dao

@Mapper
public interface UserDao {
// 获取用户
public String getUser(Long id);
}

xml

String

<select id="getUserName" paramTyp="java.lang.Long" resultType="java.lang.String" >
select user_name from t_users
</select>

int

若上边参数类型为int,则这样写

<select id="getUserName" parameterType="int" resultType="java.lang.String" >
select user_name from t_users
</select>

返回一个对象

实体类对象

@Data
public class User {
private Long id;
private String userName;
private Integer sex;
}


dao

@Mapper
public interface UserDao {
// 获取用户
public User getUser(Long id);
}

xml

resultType:

<select id="getUsersType" parameterType="java.lang.Long" resultType="com.example.pojo.User">
select user_name userName, sex from t_users where id=#{id}
</select>

resultMap:

<resultMap id="userMap" type="com.zkzong.mybatis.domain.Users">
<id column="id" property="id"/>
<result column="user_name" property="userName"/>
<result column="sex" property="sex" />
</resultMap>

<select id="getUsersMap" parameterType="java.lang.Long" resultMap="userMap">
select id, user_name, sex from t_users where id=#{id}
</select>
  • column 字段对应的应当是数据库查询结果字段,而不是数据库中的字段
  • property 与Javabean中属性值一致
  • 查询标签中的resultMap要与表映射的resultMap标签中的id一致。

id标签与result标签的区别

id标签:         代表resultMap的主键,只能有一个

result标签:   代表resultMap的属性,可以有多个

id是作为唯一标识的,当和其他对象实例对比的时候,这个id很有用,尤其是应用到缓存和内嵌的结果映射。

多对一

        多对一在实际项目中经常用到,比如多个学生对应一个学校多个工人对应一个工厂。可以通过在数据库中进行外键的关联物理上的实现也可以通过sql语句实现逻辑上的关联,在此不再赘述。无论通过那种关联在Mybatis的映射文件配置基本上是一置的。

User.java

public class User {
private Integer id;
private String username;
private String password;
private Integer sid;

private School school;
}

实现方式一

UserMapping.xml

<resultMap type="cn.softjx.modle.User" id="UserMap">
<result column="t_id" property="id"/>
<result column="t_username" property="username"/>
<result column="t_password" property="password"/>
<result column="t_sid" property="sid"/>
<result column="s_name" property="school.name"/>
</resultMap>
<select id="getSchoolName" resultMap="UserMap" >
select t_id,t_username,t_password,s_name FROM t_user,school where t_sid=s_id AND s_id=1;
</select>

        可以看到在resultMap中不配置了User自身的属性还多了<result column="s_name" property="school.name"/>,即我们需要哪个附表的值就要在resultMap中配置相应的附表映射。

实现方式二

<resultMap type="cn.softjx.modle.User" id="UserMap">
<result column="t_id" property="id"/>
<result column="t_username" property="username"/>
<result column="t_password" property="password"/>
<result column="t_sid" property="sid"/>
<association property="school" javaType="cn.softjx.modle.School">
<result column="s_name" property="name" />
</association>
</resultMap>
<select id="getSchoolName" resultMap="UserMap" >
select t_id,t_username,t_password,s_name FROM t_user,school where t_sid=s_id AND s_id=1;
</select>

        引入了association标签,不再在原有user映射关系上进行补充,此标签中的property属性值为UserBean中附表对象的引用名,javaType即为School类的的全路径。sql语句不变

一对多

一对多与多对一的配置有许多相似点,一对多返回的类型是一个集合所以要在SchoolBean中加入学生的集合。

School.java

public class School {
private Integer id;
private String name;

private List<User> user;
}

学校映射文件配置

<resultMap type="cn.softjx.modle.School" id="SchoolMap">
<result column="s_id" property="id"/>
<result column="s_name" property="name"/>
<collection property="user" ofType="cn.softjx.modle.User">
<result column="t_id" property="id"/>
<result column="t_username" property="username"/>
<result column="t_password" property="password"/>
<result column="t_sid" property="sid"/>
</collection>

</resultMap>

<select id="getSchool" resultMap="SchoolMap">
select s_id,s_name,t_id,t_username,t_password
FROM school,t_user WHERE s_id=t_sid AND s_id=1;
</select>


举报

相关推荐

0 条评论