1。概念resultMap属于mybatis返回操作结果的一个标签,可以用来映射select查询结果的集合。主要作用是将实体类中的字段与数据库表中的字段结合起来进行关联映射。并支持复杂的返回结果类型。2、使用场景2.1属性映射当项目中数据库字段与实体类属性不一致时,可以使用resultMap来映射数据库字段和实体类属性。例如:column="id"jdbcType="integer"property="id"/>column="user_name"jdbcType="VARCHAR"property="userName"/>注意:如果数据库字段与属性一致实体类,可以省略。2.2实现java复杂复杂用法`datetimeDEFAULTNULL,`sex`tinyint(4)DEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBAUTO_INCREMENT=8DEFAULTCHARSET=utf8;----------------------------------Recordsoft_user----------------------------INSERTINTO`t_user`VALUES('1','xiaoxin','123','2019-07-2716:01:21','1');INSERTINTO`t_user`VALUES('2','jackjo','123','2019-07-2416:01:37','1');INSERTINTO`t_user`VALUES('4','landengdeng','123','2019-07-2416:01:37','1');INSERTINTO`t_user`VALUES('5','max','123','2019-07-2416:01:37','1');INSERTINTO`t_user`VALUES('6','liua11','123456',null,'1');INSERTINTO`t_user`VALUES('7','xiaozhang','888888',null,'1');DROPTABLEIFEXISTS`t_hobby`;CREATETABLE`t_hobby`(`id`int(11)NOTNULL,`hobbyName`varchar(50)CHARACTERSETutf8DEFAULTNULL,`userId`int(11)DEFAULTNULL,PRIMARYKEY(`id`))ENGINE=InnoDBDEFAULTCHARSET=latin1;--------------------------------Recordsoft_hobby----------------------------INSERTINTO`t_hobby`VALUES('0','音乐','2');INSERTINTO`t_hobby`VALUES('1','篮球','1');INSERTINTO`t_hobby`VALUES('2','读书','1');b.确定实体类确定实体UserDO.javapublicclassUserDO{privateIntegerid;privateStringuserName;privateStringpassword;privateIntegersex;privateDatelastLoginTime;publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}publicStringgetUserName(){returnuserName;}publicvoidsetUserName(StringuserName){this.userName=userName;}publicStringgetPassword(){returnpassword;}publicvoidsetPassword(Stringpassword){this.password=password;}publicIntegergetSex(){returnsex;}publicvoidsetSex(Integersex){this.sex=sex;}publicDategetLastLoginTime(){returnlastLoginTime;}publicvoidsetLastLoginTime(DatelastLoginTime){this.lastLoginTime=lastLoginTime;}@OverridepublicStringtoString(){return"UserDO{"+"id="+id+",userName='"+userName+'\''+",password='"+password+'\''+",sex="+sex+",lastLoginTime="+lastLoginTime+'}';}定义实体类业余爱好DO{returnuserId;}publicvoidsetUserId(IntegeruserId){this.userId=userId;}@OverridepublicStringtoString(){return"HobbyDO{"+"id="+id+",hobbyName='"+hobbyName+'\''+",userId="+userId+'}';}c.定义模型类定义类HobbyVO.java来演示一对一}定义类UserVO.java用于演示1对多publicclassUserVOextendsUserDO{privateListlist;publicListgetList(){returnlist;}publicvoidsetList(Listlist){this.list=list;}}d.定义mapper类定义UserInfoMapper.java文件packagemy.springboot.mybatis.dao;importjava.util.List;importjava.util.Map;importmy.springboot.mybatis.entity.HobbyDO;importmy.springboot.mybatis.entity.UserDO;importmy.springboot.mybatis.model.UserVO;importorg.apache.ibatis.annotations.Mapper;@MapperpublicinterfaceUserInfoMapper{UserDOget(Integerid);ListgetUserVOMap(Integerid);ListgetHobbyByUserId(IntegeruserId);}定义HobbyMapper.java文件packagemy.springboot.mybatis.dao;importmy.springboot.mybatis.entity.HobbyDO;importmy.springboot.mybatis.entity.UserDO;importmy.springboot.mybatis.model.HobbyVo;importorg.apache.ibatis.annotations。Mapper;importjava.util.List;importjava.util.Map;@MapperpublicinterfaceHobbyMapper{ListgetHobbyByUserId(IntegeruserId);ListgetHobbyVOMap();}e.定义服务定义IUserInfoService.javapackagemy.springboot.mybatis.service;importmy.springboot.mybatis.entity.UserDO;导入my.springboot.mybatis.model.HobbyVo;导入my.springboot.mybatis。型号.dao.UserInfoMapper;importmy.springboot.mybatis.entity.UserDO;importmy.springboot.mybatis.model.HobbyVo;importmy.springboot.mybatis.model.UserVO;importmy.springboot.mybatis.service.IUserInfoService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importjava.util.List;@ServicepublicclassUserInfoServiceimplementsIUserInfoService{@AutowiredprivateUserInfoMappermapper;@AutowiredprivateHobbyMapperhobbyMapper;@OverridepublicListgetUserVOMap(Integerid){returnthis.mapper.getUserVOMap(id);}@OverridepublicListgetHobbyMap(){returnhobbyMapper.getHobbyVOMap();}}f.定义测试控制器HomeController.javapackagemy.springboot.mybatis.controller;importmy.springboot.mybatis。entity.UserDO;importmy.springboot.mybatis.model.HobbyVo;importmy.springboot.mybatis.model.UserVO;importmy.springboot.mybatis.service.IUserInfoService;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework。stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.ResponseBody;importjavax.jws.soap.SOAPBinding;importjava.util.Date;importjava.util.List;@ControllerpublicclassHomeController{@AutowiredprivateIUserInfoServiceuserInfoService;@RequestMapping("index")//注解映射请求路径@ResponseBody//可以将java对象转成json格式的数据publicStringindex(){Listvo=userInfoService.getUserVOMap(1);Listlist=userInfoService.getHobbyMap();返回“HelloWorld!”;}}g。定义xml文件定义HobbyMapper.xml文件
