1、概念
resultMap属于mybatis返回操作结果的一个标签,可以用来映射select查询出来结果的集合,主要作用是将实体类中的字段与数据库表中的字段进行关联映射。并且支持复杂的返回结果类型。
2、使用场景
2.1 属性映射
当数据库字段和项目中的实体属性不一致时,可以使resultMap进行数据库字段和实体类属性的映射关系
比如:
column="id" jdbcType="integer" property="id" /> column="user_name" jdbcType="VARCHAR" property="userName" />
说明:如果数据库字段和实体类属性一致的情况下,可以省略不写。
2.2 实现Java复杂实体类用法
a.初始化脚本
DROP TABLE IF EXISTS `t_user`;CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(255) CHARACTER SET armscii8 DEFAULT NULL, `password` varchar(255) CHARACTER SET armscii8 DEFAULT NULL, `last_login_time` datetime DEFAULT NULL, `sex` tinyint(4) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;-- ------------------------------ Records of t_user-- ----------------------------INSERT INTO `t_user` VALUES ('1', 'xiaoxin', '123', '2019-07-27 16:01:21', '1');INSERT INTO `t_user` VALUES ('2', 'jack jo', '123', '2019-07-24 16:01:37', '1');INSERT INTO `t_user` VALUES ('4', 'landengdeng', '123', '2019-07-24 16:01:37', '1');INSERT INTO `t_user` VALUES ('5', 'max', '123', '2019-07-24 16:01:37', '1');INSERT INTO `t_user` VALUES ('6', 'liua11', '123456', null, '1');INSERT INTO `t_user` VALUES ('7', 'xiaozhang', '888888', null, '1');DROP TABLE IF EXISTS `t_hobby`;CREATE TABLE `t_hobby` ( `id` int(11) NOT NULL, `hobbyName` varchar(50) CHARACTER SET utf8 DEFAULT NULL, `userId` int(11) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;-- ------------------------------ Records of t_hobby-- ----------------------------INSERT INTO `t_hobby` VALUES ('0', '音乐', '2');INSERT INTO `t_hobby` VALUES ('1', '篮球', '1');INSERT INTO `t_hobby` VALUES ('2', '读书', '1');
b.定义实体类
定义实体 UserDO.java
public class UserDO {private Integer id;private String userName;private String password;private Integer sex;private Date lastLoginTime;public Integer getId() {return id; }public void setId(Integer id) {this.id = id; }public String getUserName() {return userName; }public void setUserName(String userName) {this.userName = userName; }public String getPassword() {return password; }public void setPassword(String password) {this.password = password; }public Integer getSex() {return sex; }public void setSex(Integer sex) {this.sex = sex; }public Date getLastLoginTime() {return lastLoginTime; }public void setLastLoginTime(Date lastLoginTime) {this.lastLoginTime = lastLoginTime; }@Override public String toString() {return "UserDO{" +"id=" + id +", userName='" + userName + '\'' +", password='" + password + '\'' +", sex=" + sex +", lastLoginTime=" + lastLoginTime +'}'; }
定义实体类 HobbyDO.java
public class HobbyDO {private Integer id;private String hobbyName;private Integer userId;public Integer getId() {return id; }public void setId(Integer id) {this.id = id; }public String getHobbyName() {return hobbyName; }public void setHobbyName(String hobbyName) {this.hobbyName = hobbyName; }public Integer getUserId() {return userId; }public void setUserId(Integer userId) {this.userId = userId; }@Override public String toString() {return "HobbyDO{" +"id=" + id +", hobbyName='" + hobbyName + '\'' +", userId=" + userId +'}'; }
c.定义model类
定义类 HobbyVO.java 用来演示一对一
public class HobbyVo extends HobbyDO {private UserDO user ;public UserDO getUser() {return user; }public void setUser(UserDO user) {this.user = user; }}定义类 UserVO.java 用来演示1对多public class UserVO extends UserDO {private Listlist;public ListgetList() {return list; }public void setList(Listlist) {this.list = list; }}
d.定义mapper类文件
定义UserInfoMapper.java文件
package my.springboot.mybatis.dao;import java.util.List;import java.util.Map;import my.springboot.mybatis.entity.HobbyDO;import my.springboot.mybatis.entity.UserDO;import my.springboot.mybatis.model.UserVO;import org.apache.ibatis.annotations.Mapper;@Mapperpublic interface UserInfoMapper { UserDO get(Integer id); List getUserVOMap(Integer id); List getHobbyByUserId(Integer userId);}
定义HobbyMapper.java文件
package my.springboot.mybatis.dao;import my.springboot.mybatis.entity.HobbyDO;import my.springboot.mybatis.entity.UserDO;import my.springboot.mybatis.model.HobbyVo;import org.apache.ibatis.annotations.Mapper;import java.util.List;import java.util.Map;@Mapperpublic interface HobbyMapper { List getHobbyByUserId(Integer userId); List getHobbyVOMap();}
e.定义service服务
定义IUserInfoService.java
package my.springboot.mybatis.service;import my.springboot.mybatis.entity.UserDO;import my.springboot.mybatis.model.HobbyVo;import my.springboot.mybatis.model.UserVO;import java.util.List;public interface IUserInfoService { List getUserVOMap(Integer userId); List getHobbyMap();}
定义UserInfoService.java
package my.springboot.mybatis.service.impl;import my.springboot.mybatis.dao.HobbyMapper;import my.springboot.mybatis.dao.UserInfoMapper;import my.springboot.mybatis.entity.UserDO;import my.springboot.mybatis.model.HobbyVo;import my.springboot.mybatis.model.UserVO;import my.springboot.mybatis.service.IUserInfoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Servicepublic class UserInfoService implements IUserInfoService {@Autowired private UserInfoMapper mapper;@Autowired private HobbyMapper hobbyMapper;@Override public List getUserVOMap(Integer id) {return this.mapper.getUserVOMap(id); }@Override public ListgetHobbyMap() {return hobbyMapper.getHobbyVOMap(); }}
f.定义测试控制器
HomeController.java
package my.springboot.mybatis.controller;import my.springboot.mybatis.entity.UserDO;import my.springboot.mybatis.model.HobbyVo;import my.springboot.mybatis.model.UserVO;import my.springboot.mybatis.service.IUserInfoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import javax.jws.soap.SOAPBinding;import java.util.Date;import java.util.List;@Controllerpublic class HomeController {@Autowired private IUserInfoService userInfoService;@RequestMapping("index") //注解映射请求路径 @ResponseBody //可以将java对象转为json格式的数据 public String index() { List vo=userInfoService.getUserVOMap(1); List list=userInfoService.getHobbyMap(); return "Hello World !"; }}
g.定义xml文件
定义HobbyMapper.xml文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="my.springboot.mybatis.dao.HobbyMapper"><select id="getHobbyByUserId" resultType="my.springboot.mybatis.entity.HobbyDO" parameterType="integer"> select * from t_hobby where userId= #{id}</select><resultMap id="hobbyVOMap" type="my.springboot.mybatis.model.HobbyVo"><id property="id" column="id"/><association property="user" column="userId" javaType="my.springboot.mybatis.entity.HobbyDO" select="my.springboot.mybatis.dao.UserInfoMapper.get"></association></resultMap><select id="getHobbyVOMap" resultMap="hobbyVOMap" parameterType="integer"> select * from t_hobby</select></mapper>
定义UserInfoMapper.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="my.springboot.mybatis.dao.UserInfoMapper"><select id="get" resultType="my.springboot.mybatis.entity.UserDO"> select `id`,`user_name`,`password`,`last_login_time`,`sex` from t_user where id = #{value}</select><!--跨xml文件写法 my.springboot.mybatis.dao.HobbyMapper.getHobbyByUserId--> <resultMap id="userVOMap" type="my.springboot.mybatis.model.UserVO"><id property="id" column="id"/><collection property="list" column="id" ofType="my.springboot.mybatis.entity.HobbyDO" select="my.springboot.mybatis.dao.HobbyMapper.getHobbyByUserId"></collection></resultMap><select id="getUserVOMap" resultMap="userVOMap" parameterType="integer"> select * from t_user</select><select id="getHobbyByUserId" resultType="my.springboot.mybatis.entity.HobbyDO" parameterType="integer"> select * from t_hobby where userId= #{id}</select>
</mapper>
访问地址:http://localhost:8090/index
项目结构:
3、总结
resultMap用途主要有一下两点:
1、数据库字段和Java类属性映射
2、实现复杂的model类的查询
model中包含实体类使用关键字:association
<select id="getHobbyByUserId" resultType="my.springboot.mybatis.entity.HobbyDO" parameterType="integer">select * from t_hobby where userId= #{id}
</select>
<resultMap id="hobbyVOMap" type="my.springboot.mybatis.model.HobbyVo">
<id property="id" column="id"/>
<association property="user" column="userId" javaType="my.springboot.mybatis.entity.HobbyDO"select="my.springboot.mybatis.dao.UserInfoMapper.get">
<!-- 说明:如果调用的方法不在当前xml里面,需要指明完整的mapper路径 my.springboot.mybatis.dao.UserInfoMapper.get --> </association>
</resultMap>
<select id="getHobbyVOMap" resultMap="hobbyVOMap" parameterType="integer">select * from t_hobby
</select>
model中包含集合使用:collection 关键代码
<resultMap id="userVOMap" type="my.springboot.mybatis.model.UserVO">
<id property="id" column="id"/>
<collection property="list" column="id" ofType="my.springboot.mybatis.entity.HobbyDO" select="my.springboot.mybatis.dao.HobbyMapper.getHobbyByUserId">
</collection>
</resultMap>
<select id="getUserVOMap" resultMap="userVOMap" parameterType="integer">select * from t_user
</select>
<select id="getHobbyByUserId" resultType="my.springboot.mybatis.entity.HobbyDO" parameterType="integer">select * from t_hobby where userId= #{id}
</select>
注意:实际的查询过程当中,并不是必须要保证数据库字段名称和实体的属性名称保持一致,如果查询语句查询字段指定别名,也是可以正常做映射的,要会灵活使用。
IT技术分享社区
个人博客网站:https://programmerblog.xyz
文章推荐程序员效率:画流程图常用的工具程序员效率:整理常用的在线笔记软件远程办公:常用的远程协助软件,你都知道吗?51单片机程序下载、ISP及串口基础知识硬件:断路器、接触器、继电器基础知识