网页升级访问网页导航,优化防疫措施,微信开放平台应用签名,竞价点击软件工具文章目录 映射关系多对一映射关系-官方文档映射关系多对1-基本介绍基本介绍注意细节 映射关系多对1-映射方式映射方式配置Mapper.xml 方式-应用实例注解实现多对1 映射-应用实例 映射关系多对一
映射关系-官方文档
文档地址: https://mybatis.org/mybatis-3/zh/sqlmap-xml.ht… 文章目录 映射关系多对一映射关系-官方文档映射关系多对1-基本介绍基本介绍注意细节 映射关系多对1-映射方式映射方式配置Mapper.xml 方式-应用实例注解实现多对1 映射-应用实例 映射关系多对一
映射关系-官方文档
文档地址: https://mybatis.org/mybatis-3/zh/sqlmap-xml.html
映射关系多对1-基本介绍
基本介绍
项目中多对1 的关系是一个基本的映射关系, 多对1, 也可以理解成是1 对多.User — Pet 一个用户可以养多只宠物Dep —Emp : 一个部门可以有多个员工
注意细节
我们直接讲双向的多对一的关系单向的多对一比双向的多对一简单。在实际的项目开发中, 要求会使用双向的多对一的映射关系什么是双向的多对一的关系: 比如通过User 可以查询到对应的Pet, 反过来通过Pet 也可以级联查询到对应的User 信息.多对多的关系是在多对1 的基础上扩展.
映射关系多对1-映射方式
映射方式
方式1通过配置XxxMapper.xml 实现多对1
方式2通过注解的方式实现多对1
配置Mapper.xml 方式-应用实例
需求说明: 实现级联查询,通过user 的id 可以查询到用户信息并可以查询到关联的pet信息
反过来通过Pet 的id 可以查询到Pet 的信息并且可以级联查询到它的主人User对象信息。
创建mybatis_user 和mybatis_pet 表
CREATE TABLE mybatis_user
(id INT PRIMARY KEY AUTO_INCREMENT,NAME VARCHAR(32) NOT NULL DEFAULT
)CHARSETutf8 ;CREATE TABLE mybatis_pet
(id INT PRIMARY KEY AUTO_INCREMENT,nickname VARCHAR(32) NOT NULL DEFAULT ,user_id INT ,FOREIGN KEY (user_id) REFERENCES mybatis_user(id)
)CHARSETutf8 ;INSERT INTO mybatis_user
VALUES(NULL,宋江),(NULL,张飞);
INSERT INTO mybatis_pet
VALUES(1,黑背,1),(2,小哈,1);
INSERT INTO mybatis_pet
VALUES(3,波斯猫,2),(4,贵妃猫,2);SELECT * FROM mybatis_user;
SELECT * FROM mybatis_pet;创建src\main\java\com\nlc\entity\Pet.java
public class Pet {private Integer id;private String nickname;//一个pet对应一个主人 User对象private User user;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getNickname() {return nickname;}public void setNickname(String nickname) {this.nickname nickname;}public User getUser() {return user;}public void setUser(User user) {this.user user;}}
创建src\main\java\com\nlc\entity\User.java
public class User {private Integer id;private String name;//因为一个user可以养多个宠物,mybatis 使用集合ListPet体现这个关系private ListPet pets;public Integer getId() {return id;}public void setId(Integer id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}public ListPet getPets() {return pets;}public void setPets(ListPet pets) {this.pets pets;}//这toString会带来麻烦会造成StackOverFlow//Override//public String toString() {// return User{ // id id // , name name \ // , pets pets // };//}
}创建PetMapper.java public interface PetMapper {//通过User的id来获取pet对象可能有多个因此使用List接收public ListPet getPetByUserId(Integer userId);//通过pet的id获取Pet对象, 同时会查询到pet对象关联的user对象public Pet getPetById(Integer id);
}创建UserMapper.java
public interface UserMapper {//通过id获取User对象public User getUserById(Integer id);}创建UserMapper.xml ?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper nameNlcacecom.nlc.mapper.UserMapper!--解读1、一定要想一想我们前面1-1是如何实现2、配置/实现 public User getUserById(Integer id);3、思路(1) 先通过user-id 查询得到user信息 (2) 再根据user-id查询对应的pet信息并映射到User-ListPet pets--resultMap idUserResultMap typeUserid propertyid columnid/result propertyname columnname/!--解读因为pets属性是集合因此这里需要是collection标签来处理1. ofTypePet 指定返回的集合中存放的数据类型Pet2. collection 表示 pets 是一个集合3. propertypets 是返回的user对象的属性 pets4. columnid SELECT * FROM mybatis_user WHERE id #{id} 返回的id字段对应的值--collection propertypets columnid ofTypePetselectcom.nlc.mapper.PetMapper.getPetByUserId//resultMapselect idgetUserById parameterTypeInteger resultMapUserResultMapSELECT * FROM mybatis_user WHERE id #{id}/select/mapper创建PetMapper.xml
?xml version1.0 encodingUTF-8 ?
!DOCTYPE mapperPUBLIC -//mybatis.org//DTD Mapper 3.0//ENhttp://mybatis.org/dtd/mybatis-3-mapper.dtd
mapper nameNlcacecom.nlc.mapper.PetMapper!--1、通过User的id来获取pet对象可能有多个因此使用List接收2、public ListPet getPetByUserId(Integer userId);3. 完成的思路和前面大体相同.--resultMap idPetResultMap typePetid propertyid columnid/result propertynickname columnnickname/association propertyuser columnuser_idselectcom.nlc.mapper.UserMapper.getUserById //resultMapselect idgetPetByUserId parameterTypeInteger resultMapPetResultMapSELECT * FROM mybatis_pet WHERE user_id #{userId}/select!--说明1. 注意体会resultMap带来好处 直接复用2. 实现/配置public Pet getPetById(Integer id);3. 通过pet的id获取Pet对象--select idgetPetById parameterTypeInteger resultMapPetResultMapSELECT * FROM mybatis_pet WHERE id #{id}/select
/mapper创建PetMapperTest.java 完成测试 public class PetMapperTest {//属性private SqlSession sqlSession;private PetMapper petMapper;//初始化Beforepublic void init() {//获取到sqlSessionsqlSession MyBatisUtils.getSqlSession();petMapper sqlSession.getMapper(PetMapper.class);}Testpublic void getPetByUserId() {ListPet pets petMapper.getPetByUserId(2);for (Pet pet : pets) {System.out.println(pet信息- pet.getId() - pet.getNickname());User user pet.getUser();System.out.println(user信息 name- user.getName());}if(sqlSession ! null) {sqlSession.close();}}Testpublic void getPetById() {Pet pet petMapper.getPetById(2);System.out.println(pet信息- pet.getId() - pet.getNickname());User user pet.getUser();System.out.println(user信息- user.getId() - user.getName());if(sqlSession ! null) {sqlSession.close();}}
}创建UserMapperTest.java 完成测试 public class UserMapperTest {//属性private SqlSession sqlSession;private UserMapper userMapper;//初始化Beforepublic void init() {//获取到sqlSessionsqlSession MyBatisUtils.getSqlSession();userMapper sqlSession.getMapper(UserMapper.class);}Testpublic void getUserById() {User user userMapper.getUserById(2);System.out.println(user信息- user.getId() - user.getName());ListPet pets user.getPets();for (Pet pet : pets) {System.out.println(养的宠物信息- pet.getId() - pet.getNickname());}if(sqlSession ! null) {sqlSession.close();}}
}注解实现多对1 映射-应用实例
需求说明: 通过注解的方式来实现下面的多对1 的映射关系实现级联查询,
完成前面完成的任务通过User–Pet 也可Pet-User 在实际开发中推荐使用配置方式来做
创建UserMapperAnnotation.java
// UserMapperAnnotation以注解的方式来配置多对一
public interface UserMapperAnnotation {//通过id获取User对象/*** 1. 注解的配置就是对应的Mapper.xml文件配置的改写* 2.* 1、一定要想一想我们前面1-1是如何实现* 2、配置/实现 public User getUserById(Integer id);* 3、思路(1) 先通过user-id 查询得到user信息 (2) 再根据user-id查询对应的pet信息* 并映射到User-ListPet pets* resultMap idUserResultMap typeUser* id propertyid columnid/* result propertyname columnname/* 1. ofTypePet 指定返回的集合中存放的数据类型Pet* 2. collection 表示 pets 是一个集合* 3. propertypets 是返回的user对象的属性 pets* 4. columnid SELECT * FROM mybatis_user WHERE id #{id} 返回的id字段对应的值* collection propertypets columnid ofTypePet* selectcom.nlc.mapper.PetMapper.getPetByUserId/* /resultMap* select idgetUserById parameterTypeInteger resultMapUserResultMap* SELECT * FROM mybatis_user WHERE id #{id}* /select*/Select(SELECT * FROM mybatis_user WHERE id #{id})Results({Result(id true, property id, column id),Result(property name, column name),//这里注意pets属性对应的是集合Result(property pets,column id,many Many(select com.nlc.mapper.PetMapperAnnotation.getPetByUserId))})public User getUserById(Integer id);
}创建PetMapperAnnotation.java
public interface PetMapperAnnotation {//通过User的id来获取pet对象可能有多个因此使用List接收/*** 1、通过User的id来获取pet对象可能有多个因此使用List接收* 2、public ListPet getPetByUserId(Integer userId);* 3. 完成的思路和前面大体相同.* resultMap idPetResultMap typePet* id propertyid columnid/* result propertynickname columnnickname/* association propertyuser columnuser_id* selectcom.nlc.mapper.UserMapper.getUserById /* /resultMap* select idgetPetByUserId parameterTypeInteger resultMapPetResultMap* SELECT * FROM mybatis_pet WHERE user_id #{userId}* /select*///id PetResultMap 就是给我们的Results[Result Map] 指定一个名字//目的是为了后面复用Select(SELECT * FROM mybatis_pet WHERE user_id #{userId})Results(id PetResultMap, value {Result(id true, property id, column id),Result(property nickname, column nickname),Result(property user,column user_id,one One(select com.nlc.mapper.UserMapperAnnotation.getUserById))})public ListPet getPetByUserId(Integer userId);//通过pet的id获取Pet对象, 同时会查询到pet对象关联的user对象/*** select idgetPetById parameterTypeInteger resultMapPetResultMap* SELECT * FROM mybatis_pet WHERE id #{id}* /select* ResultMap(PetResultMap) 使用/引用我们上面定义的 Results[ResultMap]*/Select(SELECT * FROM mybatis_pet WHERE id #{id})ResultMap(PetResultMap)public Pet getPetById(Integer id);
}
创建UserMapperAnnotationTest.java 完成测试
public class UserMapperAnnotationTest {//属性private SqlSession sqlSession;private UserMapperAnnotation userMapperAnnotation;//初始化Beforepublic void init() {//获取到sqlSessionsqlSession MyBatisUtils.getSqlSession();userMapperAnnotation sqlSession.getMapper(UserMapperAnnotation.class);}Testpublic void getUserById() {User user userMapperAnnotation.getUserById(2);System.out.println(user信息- user.getId() - user.getName());ListPet pets user.getPets();for (Pet pet : pets) {System.out.println(宠物信息- pet.getId() - pet.getNickname());}if(sqlSession ! null) {sqlSession.close();}}
}创建PetMapperAnnotationTest.java 完成测试
public class PetMapperAnnotationTest {//属性private SqlSession sqlSession;private PetMapperAnnotation petMapperAnnotation;//初始化Beforepublic void init() {//获取到sqlSessionsqlSession MyBatisUtils.getSqlSession();petMapperAnnotation sqlSession.getMapper(PetMapperAnnotation.class);}Testpublic void getPetByUserId() {ListPet pets petMapperAnnotation.getPetByUserId(1);for (Pet pet : pets) {System.out.println(宠物信息- pet.getId() - pet.getNickname());}if(sqlSession ! null) {sqlSession.close();}}Testpublic void getPetById() {Pet pet petMapperAnnotation.getPetById(1);System.out.println(pet信息- pet.getId() - pet.getNickname());User user pet.getUser();System.out.println(user信息- user.getId() - user.getName());if(sqlSession ! null) {sqlSession.close();}}
}
文章转载自: http://www.morning.nqbkb.cn.gov.cn.nqbkb.cn http://www.morning.wbfg.cn.gov.cn.wbfg.cn http://www.morning.ryrpq.cn.gov.cn.ryrpq.cn http://www.morning.wcyr.cn.gov.cn.wcyr.cn http://www.morning.yfmxn.cn.gov.cn.yfmxn.cn http://www.morning.bgpch.cn.gov.cn.bgpch.cn http://www.morning.jrplk.cn.gov.cn.jrplk.cn http://www.morning.grxbw.cn.gov.cn.grxbw.cn http://www.morning.wprxm.cn.gov.cn.wprxm.cn http://www.morning.chzbq.cn.gov.cn.chzbq.cn http://www.morning.ksggr.cn.gov.cn.ksggr.cn http://www.morning.nkqrq.cn.gov.cn.nkqrq.cn http://www.morning.skscy.cn.gov.cn.skscy.cn http://www.morning.yxgqr.cn.gov.cn.yxgqr.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.hqzmz.cn.gov.cn.hqzmz.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.jksgy.cn.gov.cn.jksgy.cn http://www.morning.ryglh.cn.gov.cn.ryglh.cn http://www.morning.rwrn.cn.gov.cn.rwrn.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.fktlg.cn.gov.cn.fktlg.cn http://www.morning.jxpwr.cn.gov.cn.jxpwr.cn http://www.morning.zzhqs.cn.gov.cn.zzhqs.cn http://www.morning.xnqjs.cn.gov.cn.xnqjs.cn http://www.morning.pxlql.cn.gov.cn.pxlql.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.mtmph.cn.gov.cn.mtmph.cn http://www.morning.zdxinxi.com.gov.cn.zdxinxi.com http://www.morning.bnpn.cn.gov.cn.bnpn.cn http://www.morning.rdnkx.cn.gov.cn.rdnkx.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.fjkkx.cn.gov.cn.fjkkx.cn http://www.morning.yckwt.cn.gov.cn.yckwt.cn http://www.morning.27asw.cn.gov.cn.27asw.cn http://www.morning.jwxmn.cn.gov.cn.jwxmn.cn http://www.morning.ptslx.cn.gov.cn.ptslx.cn http://www.morning.kxqpm.cn.gov.cn.kxqpm.cn http://www.morning.qnbsx.cn.gov.cn.qnbsx.cn http://www.morning.zcnwg.cn.gov.cn.zcnwg.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.rycd.cn.gov.cn.rycd.cn http://www.morning.syqtt.cn.gov.cn.syqtt.cn http://www.morning.lpsjs.com.gov.cn.lpsjs.com http://www.morning.jkcnq.cn.gov.cn.jkcnq.cn http://www.morning.wqpsf.cn.gov.cn.wqpsf.cn http://www.morning.wnhgb.cn.gov.cn.wnhgb.cn http://www.morning.tklqs.cn.gov.cn.tklqs.cn http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.kbdrq.cn.gov.cn.kbdrq.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.plfrk.cn.gov.cn.plfrk.cn http://www.morning.qxlgt.cn.gov.cn.qxlgt.cn http://www.morning.skdrp.cn.gov.cn.skdrp.cn http://www.morning.xkqjw.cn.gov.cn.xkqjw.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.cxsdl.cn.gov.cn.cxsdl.cn http://www.morning.pqypt.cn.gov.cn.pqypt.cn http://www.morning.gbwfx.cn.gov.cn.gbwfx.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.tfznk.cn.gov.cn.tfznk.cn http://www.morning.xmxbm.cn.gov.cn.xmxbm.cn http://www.morning.qwwhs.cn.gov.cn.qwwhs.cn http://www.morning.hmtft.cn.gov.cn.hmtft.cn http://www.morning.hclqy.cn.gov.cn.hclqy.cn http://www.morning.wklhn.cn.gov.cn.wklhn.cn http://www.morning.rsszk.cn.gov.cn.rsszk.cn http://www.morning.ygmw.cn.gov.cn.ygmw.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.gnwpg.cn.gov.cn.gnwpg.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.yptwn.cn.gov.cn.yptwn.cn http://www.morning.gidmag.com.gov.cn.gidmag.com http://www.morning.kwqwp.cn.gov.cn.kwqwp.cn http://www.morning.kwqqs.cn.gov.cn.kwqqs.cn http://www.morning.zrgsg.cn.gov.cn.zrgsg.cn http://www.morning.rzmkl.cn.gov.cn.rzmkl.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.wfpmt.cn.gov.cn.wfpmt.cn