昆明制作企业网站,无锡做网站企业,嘉兴做美食图片的网站,高爆传奇手机版目录 一、关联关系映射
1.1 一对一
1.2 一对多 1.3 多对多
二、处理关联关系的方式
2.1 嵌套查询
2.2 嵌套结果
三、一对一关联映射
3.1 建表
编辑 3.2 配置文件
3.3 代码生成
3.4 编写测试 四、一对多关联映射
五、多对多关联映射
六、小结 一、关联关系映射
…目录 一、关联关系映射
1.1 一对一
1.2 一对多 1.3 多对多
二、处理关联关系的方式
2.1 嵌套查询
2.2 嵌套结果
三、一对一关联映射
3.1 建表
编辑 3.2 配置文件
3.3 代码生成
3.4 编写测试 四、一对多关联映射
五、多对多关联映射
六、小结 一、关联关系映射
MyBatis是一款优秀的持久层框架它提供了强大的关联关系映射能力。在MyBatis中我们可以使用XML或注解来定义关联关系映射。
1.1 一对一
一对一关联映射 在XML中我们可以使用resultMap元素来定义一对一关联映射可以通过association来指定关联对象的映射规则。注解中可以使用One注解来定义一对一关联映射。 1.2 一对多
一对多关联映射 在XML中我们可以使用resultMap元素来定义一对多关联映射可以通过collection来指定关联对象集合的映射规则。注解中可以使用Many注解来定义一对多关联映射 1.3 多对多
多对多关联映射 在XML中我们可以使用resultMap元素来定义多对多关联映射可以通过collection来指定中间表关联对象的映射规则。注解中可以使用Many注解来定义多对多关联映射 同时MyBatis还提供了一些常见的关联关系映射的配置选项例如延迟加载、级联操作等可以根据具体需求进行配置。
总的来说MyBatis的关联关系映射定义非常灵活可以根据需求使用XML或注解来进行配置提供了丰富的选项来满足不同场景下的需求。
二、处理关联关系的方式
在MyBatis中处理关联关系的方式主要有两种嵌套查询和嵌套结果。
2.1 嵌套查询
嵌套查询Nested Selects 这种方式是通过在SQL语句中使用子查询来实现关联查询。我们可以使用collection或association元素来配置关联关系。例如在一个订单表中可以通过子查询查询与订单相关的用户信息。 2.2 嵌套结果
嵌套结果Nested Results 这种方式是通过在查询结果中嵌套关联对象的结果集来实现关联查询。我们可以使用resultMap元素中的association或collection元素来配置关联关系。例如在一个订单表和订单项表的关系中可以将订单项作为订单对象的一个属性。 关联查询的处理方式取决于具体的需求和数据模型。嵌套查询适用于复杂的关联关系和需要在各部分之间添加额外条件的情况而嵌套结果适用于简单的关联关系和需要携带完整对象信息的情况。
无论使用哪种方式MyBatis都提供了灵活的配置选项来满足各种关联查询需求。使用MyBatis的关联映射机制我们可以轻松地处理对象之间的关系并在数据库操作中提供更强大的功能和效率。
三、一对一关联映射
3.1 建表 创建名为 t_hibernate_book (书籍表) 数据表 创建名为 t_hibernate_book_category (书籍类别表) 数据表 其中名为 bid 的属性字段为 t_hibernate_book (书籍表) 的 bid(主键) 的外键 其中名为 cid 的属性字段为 t_hibernate_category (类别表) 的 category_id (主键) 的外键 创建名为 t_hibernate_category (类别表) 数据表 创建名为 t_hibernate_order (订单表) 数据表 创建名为 t_hibernate_order_item (订单详情表) 数据表 其中名为 order_id 的属性字段为 t_hibernate_order (订单表) 的 oid (主键) 的外键 3.2 配置文件 修改名为 generatorConfig.xml 的 配置文件修改后的所有代码如下 : ?xml version1.0 encodingUTF-8 ?
!DOCTYPE generatorConfiguration PUBLIC -//mybatis.org//DTD MyBatis Generator Configuration 1.0//ENhttp://mybatis.org/dtd/mybatis-generator-config_1_0.dtd
generatorConfiguration!-- 引入配置文件 --properties resourcejdbc.properties/!--指定数据库jdbc驱动jar包的位置--classPathEntry locationD:\\temp2\\mvn_repository\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar/!-- 一个数据库一个context --context idinfoGuardian!-- 注释 --commentGeneratorproperty namesuppressAllComments valuetrue/!-- 是否取消注释 --property namesuppressDate valuetrue/ !-- 是否生成注释代时间戳 --/commentGenerator!-- jdbc连接 --jdbcConnection driverClass${jdbc.driver}connectionURL${jdbc.url} userId${jdbc.username} password${jdbc.password}/!-- 类型转换 --javaTypeResolver!-- 是否使用bigDecimal false可自动转化以下类型Long, Integer, Short, etc. --property nameforceBigDecimals valuefalse//javaTypeResolver!-- 01 指定javaBean生成的位置 --!-- targetPackage指定生成的model生成所在的包名 --!-- targetProject指定在该项目下所在的路径 --javaModelGenerator targetPackagecom.Kissship.modeltargetProjectsrc/main/java!-- 是否允许子包即targetPackage.schemaName.tableName --property nameenableSubPackages valuefalse/!-- 是否对model添加构造函数 --property nameconstructorBased valuetrue/!-- 是否针对string类型的字段在set的时候进行trim调用 --property nametrimStrings valuefalse/!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法只有构造方法 --property nameimmutable valuefalse//javaModelGenerator!-- 02 指定sql映射文件生成的位置 --sqlMapGenerator targetPackagecom.Kissship.mappertargetProjectsrc/main/java!-- 是否允许子包即targetPackage.schemaName.tableName --property nameenableSubPackages valuefalse//sqlMapGenerator!-- 03 生成XxxMapper接口 --!-- typeANNOTATEDMAPPER,生成Java Model 和基于注解的Mapper对象 --!-- typeMIXEDMAPPER,生成基于注解的Java Model 和相应的Mapper对象 --!-- typeXMLMAPPER,生成SQLMap XML文件和独立的Mapper接口 --javaClientGenerator targetPackagecom.Kissship.mappertargetProjectsrc/main/java typeXMLMAPPER!-- 是否在当前路径下新加一层schema,false路径com.oop.eksp.user.model true:com.oop.eksp.user.model.[schemaName] --property nameenableSubPackages valuefalse//javaClientGenerator!-- 配置表信息 --!-- schema即为数据库名 --!-- tableName为对应的数据库表 --!-- domainObjectName是要生成的实体类 --!-- enable*ByExample是否生成 example类 --!--table schema tableNamet_book domainObjectNameBook--!--enableCountByExamplefalse enableDeleteByExamplefalse--!--enableSelectByExamplefalse enableUpdateByExamplefalse--!--lt;!ndash; 忽略列不生成bean 字段 ndash;gt;--!--lt;!ndash; ignoreColumn columnFRED / ndash;gt;--!--lt;!ndash; 指定列的java数据类型 ndash;gt;--!--lt;!ndash; columnOverride columnLONG_VARCHAR_FIELD jdbcTypeVARCHAR / ndash;gt;--!--/table--table schema tableNamet_hibernate_book domainObjectNameHBookenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/tabletable schema tableNamet_hibernate_category domainObjectNameCategoryenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/tabletable schema tableNamet_hibernate_book_category domainObjectNameHBookCategoryenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/tabletable schema tableNamet_hibernate_order domainObjectNameOrderenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/tabletable schema tableNamet_hibernate_order_item domainObjectNameOrderItemenableCountByExamplefalse enableDeleteByExamplefalseenableSelectByExamplefalse enableUpdateByExamplefalse/table/context
/generatorConfiguration
3.3 代码生成 自动生成代码的目录列表如下 然后将每个实体类对象里面重写一下toString()方法即可。 3.4 编写测试 创建一个 名为 OrderItemVo 的类继承OrderItem类及属性有Order对象 package com.Kissship.vo;import com.Kissship.model.Order;
import com.Kissship.model.OrderItem;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-04-10:24*/
public class OrderItemVo extends OrderItem {private Order order;public Order getOrder() {return order;}public void setOrder(Order order) {this.order order;}
} 在自动生成的OrderItemMapper.xml配置文件中进行增加以下配置 resultMap idOrderItemMap typecom.Kissship.vo.OrderItemVo result columnorder_item_id propertyorderItemId /resultresult columnproduct_id propertyproductId /resultresult columnquantity propertyquantity /resultresult columnoid propertyoid /resultassociation propertyorder javaTypecom.Kissship.model.Orderresult columnorder_id propertyorderId /resultresult columnorder_no propertyorderNo /result/association/resultMapselect idselectByBiid resultMapOrderItemMap parameterTypejava.lang.Integer SELECT * FROMt_hibernate_order o ,t_hibernate_order_item oiWHERE o.order_id oi.oidAND oi.order_item_id #{oiid}
/select 在自动生成的 OrderItemMapper 接口中进行增加以下代码 OrderItemVo selectByBiid(Param(oiid) Integer oiid); 创建一个接口名为 : OrderItemBiz 接口 package com.Kissship.biz;import com.Kissship.vo.OrderItemVo;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-04-10:31*/
public interface OrderItemBiz {OrderItemVo selectByBiid(Integer oiid);}实现以上创建的接口创建一个实现了名为 OrderItemBizImpl 类 package com.Kissship.biz;import com.Kissship.mapper.OrderItemMapper;
import com.Kissship.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-04-10:42*/
Service
public class OrderItemBizImpl implements OrderItemBiz {Autowiredprivate OrderItemMapper orderItemMapper;Overridepublic OrderItemVo selectByBiid(Integer oiid) {return orderItemMapper.selectByBiid(oiid);}
}创建一个测试类 名为 Test01 用来进行方法测试 package com.Kissship.biz;import com.Kissship.vo.OrderItemVo;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-04-10:42*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(locations{classpath:spring-context.xml})
public class Test01 {Autowiredprivate OrderItemBiz orderItemBiz;Beforepublic void setUp() throws Exception {}Afterpublic void tearDown() throws Exception {}Testpublic void selectByBiid() {OrderItemVo orderItemVo orderItemBiz.selectByBiid(27);System.out.println(orderItemVo);System.out.println(orderItemVo.getOrder());}}执行测试类中selectByBiid()的方法结果如图 : 四、一对多关联映射 创建一个 名为 OrdeVo 的类继承Order类及属性有ListOrderItem集合 用来存储查询到的OrderItem对象因为是一对多所有查询出有多个对象如下 package com.Kissship.vo;import com.Kissship.model.Order;
import com.Kissship.model.OrderItem;import java.util.ArrayList;
import java.util.List;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-04-10:24*/
public class OrderVo extends Order {private ListOrderItem orderItems new ArrayList();public ListOrderItem getOrderItems() {return orderItems;}public void setOrderItems(ListOrderItem orderItems) {this.orderItems orderItems;}
} 在自动生成的 OrderMapper.xml 配置文件中增加以下配置如下 resultMap idOrderVoMap typecom.Kissship.vo.OrderVoresult columnorder_id propertyorderId /resultresult columnorder_no propertyorderNo /resultcollection propertyorderItems ofTypecom.Kissship.model.OrderItemresult columnorder_item_id propertyorderItemId /resultresult columnproduct_id propertyproductId /resultresult columnquantity propertyquantity /resultresult columnoid propertyoid /result/collection/resultMapselect idselectByOid resultMapOrderVoMap parameterTypejava.lang.Integer SELECT * FROMt_hibernate_order o ,t_hibernate_order_item oiWHERE o.order_id oi.oidAND o.order_id #{oid}/select 在自动生成的 OrderMapper接口中进行增加以下代码如下 OrderVo selectByOid(Param(oid) Integer oid); 创建一个接口名为 : OrderBiz 接口 package com.Kissship.biz;import com.Kissship.vo.OrderVo;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-04-11:12*/
public interface OrderBiz {OrderVo selectByOid(Integer oid);} 在实现以上 OrderBiz 接口创建一个实现类名为 OrderBizImpl package com.Kissship.biz;import com.Kissship.mapper.OrderMapper;
import com.Kissship.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-04-11:14*/
Service
public class OrderBizImpl implements OrderBiz {Autowiredprivate OrderMapper orderMapper;Overridepublic OrderVo selectByOid(Integer oid) {return orderMapper.selectByOid(oid);}
}在测试类( Test01 )中增加以下测试方法及接口 Autowiredprivate OrderBiz orderBiz;Testpublic void selectByOid() {OrderVo orderVo orderBiz.selectByOid(7);System.out.println(orderVo);orderVo.getOrderItems().forEach(System.out::println);} 执行其中的方法进行测试结果为如图 : 五、多对多关联映射 在自动生成的 HBookMapper.xml 配置文件中增加以下配置 resultMap idCategoryVoMap typecom.Kissship.vo.CategoryVoresult columncategory_id propertycategoryId/resultresult columncategory_name propertycategoryName/resultcollection propertyhbooks ofTypecom.Kissship.model.HBookresult columnbook_id propertybookId/resultresult columnbook_name propertybookName/resultresult columnprice propertyprice/result/collection/resultMapselect idselectByCategoryId resultMapCategoryVoMap parameterTypejava.lang.Integer SELECT * FROMt_hibernate_book b,t_hibernate_book_category bc ,t_hibernate_category cWHERE b.book_id bc.bidAND bc.cid c.category_idAND c.category_id #{cid}/select 在自动生成的 HBookMapper 接口 中增加以下方法 HbookVo selectByBookId(Param(bid) Integer bid); 创建一个接口名为 HBookBiz package com.Kissship.biz;import com.Kissship.vo.HbookVo;
import org.apache.ibatis.annotations.Param;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-04-11:25*/
public interface HBookBiz {HbookVo selecByBookId(Param(bid) Integer bid);} 然后再实现以上 HBookBiz 接口创建一个实现类名为 HBookBizImpl package com.Kissship.biz;import com.Kissship.mapper.HBookMapper;
import com.Kissship.vo.HbookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-04-11:26*/
Service
public class HBookBizImpl implements HBookBiz {Autowiredprivate HBookMapper hBookMapper;Overridepublic HbookVo selecByBookId(Integer bid) {return hBookMapper.selectByBookId(bid);}
} 在测试类( Test01 )中增加以下测试方法及接口 Autowiredprivate HBookBiz hbookBiz;Testpublic void selectByBookId() {HbookVo hBookVo hbookBiz.selecByBookId(8);System.out.println(hBookVo);hBookVo.getCategories().forEach(System.out::println);} 执行其中的方法进行测试结果为如图 : 在自动生成的 CategoryMapper.xml 配置文件中增加以下配置 resultMap idCategoryVoMap typecom.Kissship.vo.CategoryVoresult columncategory_id propertycategoryId/resultresult columncategory_name propertycategoryName/resultcollection propertybooks ofTypecom.Kissship.model.HBookresult columnbook_id propertybookId/resultresult columnbook_name propertybookName/resultresult columnprice propertyprice/result/collection/resultMapselect idselectByCategoryId resultMapCategoryVoMap parameterTypejava.lang.Integer SELECT * FROMt_hibernate_book b,t_hibernate_book_category bc ,t_hibernate_category cWHERE b.book_id bc.bidAND bc.cid c.category_idAND c.category_id #{cid}/select 在自动生成的 CategoryMapper 接口 中增加以下方法 CategoryVo selectByCategoryId(Param(cid) Integer cid); 创建一个接口名为 CategoryBiz package com.Kissship.biz;import com.Kissship.vo.CategoryVo;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-04-11:37*/
public interface CategoryBiz {CategoryVo selectByCategoryId(Integer cid);
}然后再实现以上 CategoryBiz 接口创建一个实现类名为 CategoryBizImpl package com.Kissship.biz;import com.Kissship.mapper.CategoryMapper;
import com.Kissship.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** author Kissship* site www.Kissship.com* company xxx公司* create 2023-09-04-11:38*/
Service
public class CategoryBizImpl implements CategoryBiz {Autowiredprivate CategoryMapper categoryMapper;Overridepublic CategoryVo selectByCategoryId(Integer cid) {return categoryMapper.selectByCategoryId(cid);}} 在测试类( Test01 )中增加以下测试方法及接口 Autowiredprivate CategoryBiz categoryBiz;Testpublic void selectByCategoryId() {CategoryVo categoryVo categoryBiz.selectByCategoryId(8);System.out.println(categoryVo);categoryVo.getHbooks().forEach(System.out::println);} 执行其中的方法进行测试结果为如图 : 六、小结 使用 resultMap 元素配置结果集的映射关系指定数据库列和 Java 对象属性之间的映射。可以使用 result 元素将数据库列映射到 Java 对象的属性并指定属性的类型、映射关系及相关配置。可以使用 association 元素配置关联对象的映射关系用于映射复杂对象之间的关系。可以使用 collection 元素配置集合类型对象的映射关系用于映射一对多或多对多的关系。使用 sql 元素定义可重用的 SQL 片段提供了组织和共享 SQL 语句的能力。 最后Myvatis关联关系映射与表对象之间的关系就到这里祝大家在敲代码的路上一路通畅!
感谢大家的观看 !