resultMap属于mybatis返回操作结果的一个标签,可以用来映射select查询出来结果的集合,主要作用是将实体类中的字段与数据库表中的字段进行关联映射。并且支持复杂的返回结果类型。
当数据库字段和项目中的实体属性不一致时,可以使resultMap进行数据库字段和实体类属性的映射关系比如:
- column="id" jdbcType="integer" property="id" />
- column="user_name" jdbcType="VARCHAR" property="userName" />
说明:如果数据库字段和实体类属性一致的情况下,可以省略不写。
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.定义实体类
- 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 +
- '}';
- }
- 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类
- 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类文件
- 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;
- @Mapper
- public interface UserInfoMapper {
- UserDO get(Integer id);
- List getUserVOMap(Integer id);
- List getHobbyByUserId(Integer userId);
- }
- 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;
- @Mapper
- public interface HobbyMapper {
- List getHobbyByUserId(Integer userId);
- List getHobbyVOMap();
- }
e.定义service服务
- 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();
- }
- 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;
- @Service
- public 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.定义测试控制器
- 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;
- @Controller
- public 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文件
- select * from t_hobby where userId= #{id}
- select="my.springboot.mybatis.dao.UserInfoMapper.get">
- select * from t_hobby
- select `id`,`user_name`,`password`,`last_login_time`,`sex` from t_user where id = #{value}
- select * from t_user
- select * from t_hobby where userId= #{id}
访问地址:http://localhost:8090/index项目结构:
resultMap用途主要有一下两点:
1、数据库字段和Java类属性映射
2、实现复杂的model类的查询
model中包含实体类使用关键字:association
- select * from t_hobby where userId= #{id}
- select="my.springboot.mybatis.dao.UserInfoMapper.get">
- select * from t_hobby
model中包含集合使用:collection 关键代码
- select * from t_user
- select * from t_hobby where userId= #{id}
注意:实际的查询过程当中,并不是必须要保证数据库字段名称和实体的属性名称保持一致,如果查询语句查询字段指定别名,也是可以正常做映射的,要会灵活使用。
分享题目:后端技术:Mybatis中resultMap用法示例笔记
网站网址:http://www.shufengxianlan.com/qtweb/news14/435114.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联