厦门手机建站,sem竞价专员,ipa文件自己网站怎么做下载,权重查询目录
一、一对一映射#xff08;One-to-One#xff09;
1.1 表关系
1.2 resultMap设置自定义映射
二、一对多映射#xff08;One-to-Many#xff09;
2.1 创建实体
2.2 级联方式处理映射关系
2.3 定义SQL
2.4 OrderMapper接口
2.5 编写业务逻辑层
2.6 Junit测试…目录
一、一对一映射One-to-One
1.1 表关系
1.2 resultMap设置自定义映射
二、一对多映射One-to-Many
2.1 创建实体
2.2 级联方式处理映射关系
2.3 定义SQL
2.4 OrderMapper接口
2.5 编写业务逻辑层
2.6 Junit测试
三、多对多映射Many-to-Many
3.1 表关系
3.2 创建实体
3.3 处理映射关系、定义sql
3.4 HBookMapper 接口
3.5 编写业务逻辑层
3.6 Junit测试 一、一对一映射One-to-One
1.1 表关系 一对一映射是指一个对象与另一个对象具有一对一的关系。例如一个用户User与一个地址Address之间的关系。假设我们有以下表结构
user 表
id (int)
name (varchar)
address_id (int)address 表
id (int)
street (varchar)
city (varchar)首先创建 User 和 Address 实体类
User.java
public class User {private int id;private String name;private Address address;// getters and setters
}Address.java
public class Address {private int id;private String street;private String city;// getters and setters
}接下来创建一个 resultMap 进行一对一映射 1.2 resultMap设置自定义映射
UserMapper.xml
resultMap idUserAddressResultMap typeUserid propertyid columnid/result propertyname columnname///一对一关系使用associationassociation propertyaddress javaTypeAddressid propertyid columnaddress_id/result propertystreet columnstreet/result propertycity columncity//association
/resultMap属性
id表示自定义映射的唯一标识type查询的数据要映射的实体类的类型
子标签
id设置主键的映射关系result设置普通字段的映射关系association 设置多对一的映射关系collection设置一对多的映射关系
子标签属性
property设置映射关系中实体类中的属性名column设置映射关系中表中的字段名 最后编写一个查询方法来使用这个 resultMap
UserMapper.xml
select idfindUserWithAddress resultMapUserAddressResultMapSELECT u.id, u.name, a.id as address_id, a.street, a.cityFROM user uINNER JOIN address a ON u.address_id a.idWHERE u.id #{id}
/select 最后实现接口findUserWithAddress方法测试即可通过以上简单的案例我相信你已经明白自定义关系映射了。往往实际开发中数据和表是要复杂的多进阶用法请看以下示例 二、一对多映射One-to-Many 一对多映射是指一个对象与多个对象具有一对多的关系。例如一个订单Oeder与一个订单详情OrderItem之间的关系。假设我们有以下表结构 2.1 创建实体 利用mybatis逆向工程generatorConfig.xml生成模型层代码 : 创建 OrderVo 它是一个值对象Value Object继承 Order 类并添加了一个额外的属性 orderItems。该类用于在应用程序的各个层之间传递数据尤其是在表示层和业务逻辑层之间。在这个例子中OrderVo 类用于表示一个订单及其关联的订单项。
package com.ycxw.vo;import com.ycxw.model.Order;
import com.ycxw.model.OrderItem;
import lombok.Data;import java.util.ArrayList;
import java.util.List;/*** author 云村小威* site blog.csdn.net/Justw320* create 2023-08-26 16:30*/
Data
public class OrderVo extends Order {private ListOrderItem orderItems new ArrayList();
}2.2 级联方式处理映射关系 resultMap idOrderVoMap typecom.ycxw.vo.OrderVoresult columnorder_id propertyorderId/resultresult columnorder_no propertyorderNo/result//多关系使用collectioncollection propertyorderItems ofTypecom.ycxw.model.OrderItemresult columnorder_item_id propertyorderItemId/resultresult columnproduct_id propertyproductId/resultresult columnquantity propertyquantity/resultresult columnoid propertyoid/result/collection/resultMap 2.3 定义SQL select idSelectByOid resultMapOrderVoMap parameterTypejava.lang.IntegerSELECT *FROM t_hibernate_order o,t_hibernate_order_item oiWHERE o.order_id oi.oidAND o.order_id #{oid}/select 2.4 OrderMapper接口
package com.ycxw.mapper;import com.ycxw.model.Order;
import com.ycxw.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;Repository
public interface OrderMapper {OrderVo SelectByOid(Param(oid) Integer oid);
} 2.5 编写业务逻辑层
OrderItmeBiz 接口
package com.ycxw.biz;import com.ycxw.vo.OrderItemVo;/*** author 云村小威* site blog.csdn.net/Justw320* create 2023-08-26 21:48*/
public interface OrderItmeBiz {OrderItemVo SelectByOitemId(Integer oiid);
}OrderBizImpl 接口实现类
package com.ycxw.biz.impl;import com.ycxw.biz.OrderBiz;
import com.ycxw.mapper.OrderMapper;
import com.ycxw.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** author 云村小威* site blog.csdn.net/Justw320* create 2023-08-26 16:46*/
Service
public class OrderBizImpl implements OrderBiz {Autowiredprivate OrderMapper orderMapper;Overridepublic OrderVo SelectByOid(Integer oid) {return orderMapper.SelectByOid(oid);}
}2.6 Junit测试
package com.ycxw.biz.impl;import com.ycxw.biz.OrderBiz;
import com.ycxw.vo.OrderVo;
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 云村小威* site blog.csdn.net/Justw320* create 2023-08-26 16:48*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(locations {classpath:spring-mybatis.xml})
public class OrderBizImplTest {Autowiredprivate OrderBiz orderBiz;Testpublic void selectByOid() {OrderVo orderVo orderBiz.SelectByOid(8);//获取订单System.out.println(orderVo);//获取订单项信息orderVo.getOrderItems().forEach(System.out::println);}}
运行结果 三、多对多映射Many-to-Many
3.1 表关系 多对多映射是指多个对象与多个对象具有多对多的关系。表之间的多对多关系稍微复杂需要一个中间表来表示 中间表有三个字段其中两个字段分别作为外键指向各自一方的主键由此通过中间表来表示多对多关系通过一个表联合另一个中间表可以表示为一对多关系。 如根据书籍id查找关联属性类别
3.2 创建实体
利用mybatis逆向工程generatorConfig.xml生成模型层代码 : 创建 HBookVo 值对象Value Object
package com.ycxw.vo;import com.ycxw.model.BookCategory;
import com.ycxw.model.HBook;
import lombok.Data;import java.util.List;/*** author 云村小威* site blog.csdn.net/Justw320* create 2023-08-27 21:03*/
Data
public class HBookVo extends HBook {private ListBookCategory bookc new ArrayList();
}3.3 处理映射关系、定义sql resultMap idHBookVoMap typecom.ycxw.vo.HBookVo result columnbook_id propertybookId/resultresult columnbook_name propertybookName/resultresult columnprice propertyprice/resultcollection propertybookc ofTypecom.ycxw.model.Categoryresult columncategory_id propertycategoryId/resultresult columncategory_name propertycategoryName/result/collection/resultMap!--根据书籍的id查询书籍的信息及所属属性--select idselectByBookId resultMapHBookVoMap parameterTypejava.lang.IntegerSELECT*FROMt_hibernate_book b,t_hibernate_category c,t_hibernate_book_category bcWHEREb.book_id bc.bidAND c.category_id bc.bcidAND b.book_id #{bid}/select 3.4 HBookMapper 接口
HBookVo selectByBookId(Param(bid) Integer bid); 3.5 编写业务逻辑层
HBookBiz 接口
package com.ycxw.biz;import com.ycxw.vo.HBookVo;/*** author 云村小威* site blog.csdn.net/Justw320* create 2023-08-29 12:47*/
public interface HBookBiz {HBookVo selectByBookId(Integer bid);
}HBookBizImpl 接口实现类
package com.ycxw.biz.impl;import com.ycxw.biz.HBookBiz;
import com.ycxw.mapper.HBookMapper;
import com.ycxw.vo.HBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** author 云村小威* site blog.csdn.net/Justw320* create 2023-08-29 12:48*/
Service
public class HBookBizImpl implements HBookBiz {Autowiredprivate HBookMapper hBookMapper;Overridepublic HBookVo selectByBookId(Integer bid) {return hBookMapper.selectByBookId(bid);}
}3.6 Junit测试
package com.ycxw.biz.impl;import com.ycxw.biz.HBookBiz;
import com.ycxw.vo.HBookVo;
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 云村小威* site blog.csdn.net/Justw320* create 2023-08-26 16:48*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(locations {classpath:spring-mybatis.xml})
public class OrderBizImplTest {Autowiredprivate HBookBiz hBookBiz;Testpublic void selectByBookId(){HBookVo hBookVo hBookBiz.selectByBookId(22);//数据所有信息System.out.println(hBookVo);//书籍有关的类别System.out.println(hBookVo.getBookc());}
} 反之亦然如根据类别id查找书籍信息 resultMap idCategotyVoMap typecom.ycxw.vo.CategoryVoresult columncategory_id propertycategoryId/resultresult columncategory_name propertycategoryName/resultcollection propertybooks ofTypecom.ycxw.model.HBookresult columnbook_id propertybookId/resultresult columnbook_name propertybookName/resultresult columnprice propertyprice/result/collection/resultMapselect idselectCategoryId resultMapCategotyVoMap parameterTypejava.lang.IntegerSELECT*FROMt_hibernate_book b,t_hibernate_category c,t_hibernate_book_category bcWHEREb.book_id bc.bidAND c.category_id bc.bcidAND c.category_id #{cid}/select
测试运行