mybatis一对多关联关系、
generatorConfig.xml
<table schema="" tableName="t_hibernate_order" domainObjectName="Order"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
<table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
接下来就是写Vo类了 V:视图 O:Object 视图对象
纯粹的实体类是直接与数据库相对应的列段,不包含其他列段,就是纯粹的来描述表的,
Vo类就是专门用来开发一个展示的视图,存放与数据库无关的字段,这样达到了解耦的效果,增加了可读性。
OrderVo 继承会把注解继承过来,但是如果不想要注解的话,直接把它有的东西copy过来
package com.xhh.model.vo;
import com.xhh.model.Order;
import com.xhh.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
* @author 林耀东
* @site www.baidu.com
* @company
* @create 2019-11-21 20:30
*
*/
public class OrderVo extends Order {
private List<OrderItem> orderItems = new ArrayList<>();
public List<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<OrderItem> orderItems) {
this.orderItems = orderItems;
}
}
OrderItemVo
package com.xhh.model.vo;
import com.xhh.model.Order;
import com.xhh.model.OrderItem;
/**
* @author 林耀东
* @site www.baidu.com
* @company
* @create 2019-11-21 20:50
*
*/
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
配置映射文件
OrederMapper.xml
<resultMap id="OrderVoMap" type="com.xhh.model.vo.OrderVo" >
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<!--描述关联属性(对应多个订单项)-->
<collection property="orderItems" javaType="com.xhh.model.OrderItem">
<result property="orderItemId" column="order_item_id"></result>
<result property="productId" column="product_id"></result>
<result property="quantity" column="quantity"></result>
<result property="oid" column="oid"></result>
</collection>
</resultMap>
OrderItemMapper.xml配置
<resultMap id="OrderItemVoMap" type="com.xhh.model.vo.OrderItemVo" >
<result column="order_item_id" property="orderItemId"></result>
<result column="product_id" property="productId"></result>
<result column="quantity" property="quantity"></result>
<result column="oid" property="oid"></result>
<association property="order" javaType="com.xhh.model.Order">
<result column="order_id" property="orderId"></result>
<result column="order_no" property="orderNo"></result>
</association>
</resultMap>
OrderMapper
// 通过订单的id查出订单的详情,以及该订单的订单项
OrderVo selectByOid(@Param("bid") Integer oid);
直接选中方法名 alt+回车 选 Generate Mapper Select Statement 它会跳到下面那个xml文件配置
OrderMapper.xml配置
property指的是类属性,column指的是表字段,javaType表示关联属性类型。
<!--resultMap 传出参数 parameterType 传入参数-->
<select id="selectByOid" resultMap="OrderVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_order o,t_hibernate_order_item oi
where o.order_id = oi.oid
and o.order_id = #{oid}
</select>
OrderItemMapper
OrderItemVo selectByOrderItemId(@Param("orderItemId") Integer orderItemId);
OrderItemMapper.xml配置
<select id="selectByOid" resultMap="OrderVoMap" parameterType="java.lang.Integer" >
select * from t_hibernate_order o,t_hibernate_order_item oi
where o.order_id = oi.oid
and o.order_id = #{oid}
</select>
test
@Service
public class OneTestImpl implements OneTest {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Override
public OrderVo OrderByOid(Integer oid) {
return orderMapper.OrderByOid(oid);
}
@Override
public OrderItemVo OrderItemByoid(Integer oid) {
return orderItemMapper.OrderItemByoid(oid);
}
}
mybatis多对多关联关系
首先也是通过逆向生成工具生成Hbook,HbookCategory,Category
<table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
<table schema="" tableName="t_hibernate_book_category" domainObjectName="HbookCategory"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
<table schema="" tableName="t_hibernate_category" domainObjectName="Category"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
Vo类
HbookVo
package com.xhh.model.vo;
import com.xhh.model.Category;
import com.xhh.model.Hbook;
import java.util.ArrayList;
import java.util.List;
/**
* @author 林耀东
* @site www.baidu.com
* @company
* @create 2019-11-21 21:40
*/
public class HbookVo extends Hbook {
List<Category> categories = new ArrayList<>();
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
}
CategoryVo
package com.xhh.model.vo;
import com.xhh.model.Category;
import com.xhh.model.Hbook;
import java.util.ArrayList;
import java.util.List;
/**
* @author 林耀东
* @site www.baidu.com
* @company
* @create 2019-11-21 21:50
*/
public class CategoryVo extends Category {
List<Hbook> hbooks = new ArrayList<>();
public List<Hbook> getHbooks() {
return hbooks;
}
public void setHbooks(List<Hbook> hbooks) {
this.hbooks = hbooks;
}
}
HbookCategory.xml配置
<resultMap id="HbookVoMap" type="com.xhh.model.vo.HbookVo">
<result column="book_id" property="bookId" ></result>
<result column="book_name" property="bookName" ></result>
<result column="price" property="price" ></result>
<collection property="categories" ofType="com.xhh.model.Category">
<result column="category_id" property="categoryId"></result>
<result column="category_name" property="categoryName"></result>
</collection>
</resultMap>
<resultMap id="CategoryVoMap" type="com.xhh.model.vo.CategoryVo">
<result column="category_id" property="categoryId"></result>
<result column="category_name" property="categoryName"></result>
<collection property="hbooks" ofType="com.xhh.model.Hbook">
<result column="book_id" property="bookId" ></result>
<result column="book_name" property="bookName" ></result>
<result column="price" property="price" ></result>
</collection>
</resultMap>
接口
HBookVo HbookBybid(@Param("bid") Integer bid);
CategoryVo HbookBybid(@Param("cid") Integer cid);
HbookCategoryMapper.xml
<select id="HbookBybid" resultMap="HbookVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
where b.book_id = bc.bid and bc.cid = c.category_id and b.book_id = #{bid}
</select>
<select id="CategoryBycid" resultMap="CategoryVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
where b.book_id = bc.bid and bc.cid = c.category_id and c.category_id = #{cid}
</select>
测试
ManyToManyService
public interface ManyToManyService {
HBookVo HbookBybid(Integer bid);
CategoryVo CategoryBycid(Integer cid);
}
test
public class ManyToManyServiceImplTest extends SpringBaseTest {
@Autowired
private ManyToManyService manyToManyService;
@Test
public void HbookBybid() {
HBookVo hBooks = manyToManyService.HbookBybid(1);
System.out.println(hBooks);
List<Category> list = hBooks.getCategoryList();
for (Category c : list) {
System.out.println(c);
}
}
@Test
public void CategoryBycid() {
CategoryVo categorys = manyToManyService.CategoryBycid(1);
System.out.println(categorys);
List<HBook> hBookList = categorys.getHBookList();
for (HBook hBook : hBookList) {
System.out.println(hBook);
}
}
}