一对多
首先先把逆向生成的几个包弄出来
generatorConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
<!-- 引入配置文件 -->
<properties resource="jdbc.properties"/>
<!--指定数据库jdbc驱动jar包的位置-->
<classPathEntry location="C:\\mvn\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/>
<!-- 一个数据库一个context -->
<context id="infoGuardian">
<!-- 注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
<property name="suppressDate" value="true"/> <!-- 是否生成注释代时间戳 -->
</commentGenerator>
<!-- jdbc连接 -->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
<!-- 类型转换 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 01 指定javaBean生成的位置 -->
<!-- targetPackage:指定生成的model生成所在的包名 -->
<!-- targetProject:指定在该项目下所在的路径 -->
<javaModelGenerator targetPackage="com.yq.ssm.model"
targetProject="src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
<!-- 是否对model添加构造函数 -->
<property name="constructorBased" value="true"/>
<!-- 是否针对string类型的字段在set的时候进行trim调用 -->
<property name="trimStrings" value="false"/>
<!-- 建立的Model对象是否 不可改变 即生成的Model对象不会有 setter方法,只有构造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- 02 指定sql映射文件生成的位置 -->
<sqlMapGenerator targetPackage="com.yq.ssm.mapper"
targetProject="src/main/java">
<!-- 是否允许子包,即targetPackage.schemaName.tableName -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 03 生成XxxMapper接口 -->
<!-- type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 -->
<!-- type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 -->
<!-- type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
<javaClientGenerator targetPackage="com.yq.ssm.mapper"
targetProject="src/main/java" type="XMLMAPPER">
<!-- 是否在当前路径下新加一层schema,false路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 配置表信息 -->
<!-- schema即为数据库名 -->
<!-- tableName为对应的数据库表 -->
<!-- domainObjectName是要生成的实体类 -->
<!-- enable*ByExample是否生成 example类 -->
<!--<table schema="" tableName="t_book" domainObjectName="Book"-->
<!--enableCountByExample="false" enableDeleteByExample="false"-->
<!--enableSelectByExample="false" enableUpdateByExample="false">-->
<!--<!– 忽略列,不生成bean 字段 –>-->
<!--<!– <ignoreColumn column="FRED" /> –>-->
<!--<!– 指定列的java数据类型 –>-->
<!--<!– <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> –>-->
<!--</table>-->
<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>
<table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
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>
<table schema="" tableName="t_hibernate_book_category" domainObjectName="HbookCategory"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="false" enableUpdateByExample="false">
</table>
</context>
</generatorConfiguration>
OredrVo
package com.yq.ssm.model.vo;
import com.yq.ssm.model.Order;
import com.yq.ssm.model.OrderItem;
import java.util.List;
/**
* @author 陌陌
* @create 2019-11-22 10:53
*/
public class OredrVo extends Order {
private List<OrderItem> orderItems;
public List<OrderItem> getOrderItems() {
return orderItems;
}
public void setOrderItems(List<OrderItem> orderItems) {
this.orderItems = orderItems;
}
}
OrderItemVo
package com.yq.ssm.model.vo;
import com.yq.ssm.model.Order;
import com.yq.ssm.model.OrderItem;
/**
* @author 陌陌
* @create 2019-11-22 10:55
*/
public class OrderItemVo extends OrderItem {
private Order order;
public Order getOrder() {
return order;
}
public void setOrder(Order order) {
this.order = order;
}
}
OrderMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yq.ssm.mapper.OrderMapper" >
<resultMap id="BaseResultMap" type="com.yq.ssm.model.Order" >
<constructor >
<idArg column="order_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="order_no" jdbcType="VARCHAR" javaType="java.lang.String" />
</constructor>
</resultMap>
<resultMap id="OrderVoMap" type="com.yq.ssm.model.vo.OredrVo" >
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<collection property="orderItems" ofType="com.yq.ssm.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>
<sql id="Base_Column_List" >
order_id, order_no
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_hibernate_order
where order_id = #{orderId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_hibernate_order
where order_id = #{orderId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yq.ssm.model.Order" >
insert into t_hibernate_order (order_id, order_no)
values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.yq.ssm.model.Order" >
insert into t_hibernate_order
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="orderId != null" >
order_id,
</if>
<if test="orderNo != null" >
order_no,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="orderId != null" >
#{orderId,jdbcType=INTEGER},
</if>
<if test="orderNo != null" >
#{orderNo,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yq.ssm.model.Order" >
update t_hibernate_order
<set >
<if test="orderNo != null" >
order_no = #{orderNo,jdbcType=VARCHAR},
</if>
</set>
where order_id = #{orderId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yq.ssm.model.Order" >
update t_hibernate_order
set order_no = #{orderNo,jdbcType=VARCHAR}
where order_id = #{orderId,jdbcType=INTEGER}
</update>
<select id="queryOrderVoByOrderId" 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 = #{orderId}
</select>
</mapper>
OrderItemMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yq.ssm.mapper.OrderItemMapper" >
<resultMap id="BaseResultMap" type="com.yq.ssm.model.OrderItem" >
<constructor >
<idArg column="order_item_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="product_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="quantity" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="oid" jdbcType="INTEGER" javaType="java.lang.Integer" />
</constructor>
</resultMap>
<resultMap id="OrderItemVoMap" type="com.yq.ssm.model.vo.OrderItemVo" >
<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>
<association
property="order" javaType="com.yq.ssm.model.Order">
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
</association>
</resultMap>
<sql id="Base_Column_List" >
order_item_id, product_id, quantity, oid
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_hibernate_order_item
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_hibernate_order_item
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yq.ssm.model.OrderItem" >
insert into t_hibernate_order_item (order_item_id, product_id, quantity,
oid)
values (#{orderItemId,jdbcType=INTEGER}, #{productId,jdbcType=INTEGER}, #{quantity,jdbcType=INTEGER},
#{oid,jdbcType=INTEGER})
</insert>
<insert id="insertSelective" parameterType="com.yq.ssm.model.OrderItem" >
insert into t_hibernate_order_item
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="orderItemId != null" >
order_item_id,
</if>
<if test="productId != null" >
product_id,
</if>
<if test="quantity != null" >
quantity,
</if>
<if test="oid != null" >
oid,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="orderItemId != null" >
#{orderItemId,jdbcType=INTEGER},
</if>
<if test="productId != null" >
#{productId,jdbcType=INTEGER},
</if>
<if test="quantity != null" >
#{quantity,jdbcType=INTEGER},
</if>
<if test="oid != null" >
#{oid,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yq.ssm.model.OrderItem" >
update t_hibernate_order_item
<set >
<if test="productId != null" >
product_id = #{productId,jdbcType=INTEGER},
</if>
<if test="quantity != null" >
quantity = #{quantity,jdbcType=INTEGER},
</if>
<if test="oid != null" >
oid = #{oid,jdbcType=INTEGER},
</if>
</set>
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yq.ssm.model.OrderItem" >
update t_hibernate_order_item
set product_id = #{productId,jdbcType=INTEGER},
quantity = #{quantity,jdbcType=INTEGER},
oid = #{oid,jdbcType=INTEGER}
where order_item_id = #{orderItemId,jdbcType=INTEGER}
</update>
<select id="queryOrderItemVoByOrderItemId" resultMap="OrderItemVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_order o,t_hibernate_order_item oi
where o.order_id = oi.oid
and oi.order_item_id = #{orderItemId}
</select>
</mapper>
OrderMapper
package com.yq.ssm.mapper;
import com.yq.ssm.model.Order;
import com.yq.ssm.model.vo.OredrVo;
import org.apache.ibatis.annotations.Param;
public interface OrderMapper {
int deleteByPrimaryKey(Integer orderId);
int insert(Order record);
int insertSelective(Order record);
Order selectByPrimaryKey(Integer orderId);
int updateByPrimaryKeySelective(Order record);
int updateByPrimaryKey(Order record);
OredrVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
}
OrderItemMapper
package com.yq.ssm.mapper;
import com.yq.ssm.model.OrderItem;
import com.yq.ssm.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
public interface OrderItemMapper {
int deleteByPrimaryKey(Integer orderItemId);
int insert(OrderItem record);
int insertSelective(OrderItem record);
OrderItem selectByPrimaryKey(Integer orderItemId);
int updateByPrimaryKeySelective(OrderItem record);
int updateByPrimaryKey(OrderItem record);
OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
}
One2ManyService
package com.yq.ssm.service;
import com.yq.ssm.model.vo.OrderItemVo;
import com.yq.ssm.model.vo.OredrVo;
/**
* @author 陌陌
* @create 2019-11-22 12:07
*/
public interface One2ManyService {
OredrVo queryOrderVoByOrderId(Integer orderId);
OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId);
}
One2ManyServiceImpl
package com.yq.ssm.service;
import com.yq.ssm.mapper.OrderItemMapper;
import com.yq.ssm.mapper.OrderMapper;
import com.yq.ssm.model.vo.OrderItemVo;
import com.yq.ssm.model.vo.OredrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author 陌陌
* @create 2019-11-22 12:09
*/
@Service
public class One2ManyServiceImpl implements One2ManyService {
@Autowired
private OrderMapper orderMapper;
@Autowired
private OrderItemMapper orderItemMapper;
@Override
public OredrVo queryOrderVoByOrderId(Integer orderId) {
return orderMapper.queryOrderVoByOrderId(orderId);
}
@Override
public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
}
}
One2ManyServiceImplTest
package com.yq.ssm.service.impl;
import com.yq.ssm.SpringBaseTest;
import com.yq.ssm.model.OrderItem;
import com.yq.ssm.model.vo.OrderItemVo;
import com.yq.ssm.model.vo.OredrVo;
import com.yq.ssm.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* @author 陌陌
* @create 2019-11-22 12:11
*/
public class One2ManyServiceImplTest extends SpringBaseTest {
@Autowired
private One2ManyService one2ManyService;
@Test
public void queryOrderVoByOrderId() {
OredrVo oredrVo = one2ManyService.queryOrderVoByOrderId(8);
System.out.println(oredrVo.getOrderNo());
for (OrderItem orderItem : oredrVo.getOrderItems()) {
System.out.println(orderItem);
}
}
@Test
public void queryOrderItemVoByOrderItemId(){
OrderItemVo orderItemVo=one2ManyService.queryOrderItemVoByOrderItemId(36);
System.out.println(orderItemVo);
System.out.println(orderItemVo.getOrder().getOrderNo());
}
}
多对多
HbookVo
package com.yq.ssm.model.vo;
import com.yq.ssm.model.Category;
import com.yq.ssm.model.Hbook;
import java.util.List;
/**
* @author 陌陌
* @create 2019-11-23 14:08
*/
public class HbookVo extends Hbook {
private List<Category> categories;
public List<Category> getCategories() {
return categories;
}
public void setCategories(List<Category> categories) {
this.categories = categories;
}
}
CategoryVo
package com.yq.ssm.model.vo;
import com.yq.ssm.model.Category;
import com.yq.ssm.model.Hbook;
import java.util.List;
/**
* @author 陌陌
* @create 2019-11-23 14:09
*/
public class CategoryVo extends Category {
private List<Hbook> hbooks;
public List<Hbook> getHbooks() {
return hbooks;
}
public void setHbooks(List<Hbook> hbooks) {
this.hbooks = hbooks;
}
}
HbookCategoryMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yq.ssm.mapper.HbookCategoryMapper" >
<resultMap id="BaseResultMap" type="com.yq.ssm.model.HbookCategory" >
<constructor >
<idArg column="bcid" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="cid" jdbcType="INTEGER" javaType="java.lang.Integer" />
</constructor>
</resultMap>
<resultMap id="HbookVoMap" type="com.yq.ssm.model.vo.HbookVo" >
<result property="bookId" column="book_id"></result>
<result property="bookName" column="book_name"></result>
<result property="price" column="price"></result>
<collection property="categories" ofType="com.yq.ssm.model.Category">
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
</collection>
</resultMap>
<resultMap id="CategoryVoMap" type="com.yq.ssm.model.vo.CategoryVo" >
<result property="categoryId" column="category_id"></result>
<result property="categoryName" column="category_name"></result>
<collection property="hbooks" ofType="com.yq.ssm.model.Hbook">
<result property="bookId" column="book_id"></result>
<result property="bookName" column="book_name"></result>
<result property="price" column="price"></result>
</collection>
</resultMap>
<sql id="Base_Column_List" >
bcid, bid, cid
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_hibernate_book_category
where bcid = #{bcid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_hibernate_book_category
where bcid = #{bcid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yq.ssm.model.HbookCategory" >
insert into t_hibernate_book_category (bcid, bid, cid
)
values (#{bcid,jdbcType=INTEGER}, #{bid,jdbcType=INTEGER}, #{cid,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.yq.ssm.model.HbookCategory" >
insert into t_hibernate_book_category
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="bcid != null" >
bcid,
</if>
<if test="bid != null" >
bid,
</if>
<if test="cid != null" >
cid,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="bcid != null" >
#{bcid,jdbcType=INTEGER},
</if>
<if test="bid != null" >
#{bid,jdbcType=INTEGER},
</if>
<if test="cid != null" >
#{cid,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yq.ssm.model.HbookCategory" >
update t_hibernate_book_category
<set >
<if test="bid != null" >
bid = #{bid,jdbcType=INTEGER},
</if>
<if test="cid != null" >
cid = #{cid,jdbcType=INTEGER},
</if>
</set>
where bcid = #{bcid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yq.ssm.model.HbookCategory" >
update t_hibernate_book_category
set bid = #{bid,jdbcType=INTEGER},
cid = #{cid,jdbcType=INTEGER}
where bcid = #{bcid,jdbcType=INTEGER}
</update>
<select id="queryByBookId" 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 = #{bookId}
</select>
<select id="queryByCid" 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>
</mapper>
CategoryMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yq.ssm.mapper.CategoryMapper" >
<resultMap id="BaseResultMap" type="com.yq.ssm.model.Category" >
<constructor >
<idArg column="category_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="category_name" jdbcType="VARCHAR" javaType="java.lang.String" />
</constructor>
</resultMap>
<sql id="Base_Column_List" >
category_id, category_name
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from t_hibernate_category
where category_id = #{categoryId,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
delete from t_hibernate_category
where category_id = #{categoryId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yq.ssm.model.Category" >
insert into t_hibernate_category (category_id, category_name)
values (#{categoryId,jdbcType=INTEGER}, #{categoryName,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.yq.ssm.model.Category" >
insert into t_hibernate_category
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="categoryId != null" >
category_id,
</if>
<if test="categoryName != null" >
category_name,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="categoryId != null" >
#{categoryId,jdbcType=INTEGER},
</if>
<if test="categoryName != null" >
#{categoryName,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yq.ssm.model.Category" >
update t_hibernate_category
<set >
<if test="categoryName != null" >
category_name = #{categoryName,jdbcType=VARCHAR},
</if>
</set>
where category_id = #{categoryId,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yq.ssm.model.Category" >
update t_hibernate_category
set category_name = #{categoryName,jdbcType=VARCHAR}
where category_id = #{categoryId,jdbcType=INTEGER}
</update>
</mapper>
HbookCategoryMapper
package com.yq.ssm.mapper;
import com.yq.ssm.model.Category;
import com.yq.ssm.model.Hbook;
import com.yq.ssm.model.HbookCategory;
import com.yq.ssm.model.vo.CategoryVo;
import com.yq.ssm.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
public interface HbookCategoryMapper {
int deleteByPrimaryKey(Integer bcid);
int insert(HbookCategory record);
int insertSelective(HbookCategory record);
HbookCategory selectByPrimaryKey(Integer bcid);
int updateByPrimaryKeySelective(HbookCategory record);
int updateByPrimaryKey(HbookCategory record);
HbookVo queryByBookId(@Param("bookId") Integer bookId);
CategoryVo queryByCid(@Param("cid") Integer cid);
}
Many2ManyService
package com.yq.ssm.service;
import com.yq.ssm.model.vo.CategoryVo;
import com.yq.ssm.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
/**
* @author 陌陌
* @create 2019-11-23 14:29
*/
public interface Many2ManyService {
HbookVo queryByBookId(Integer bookId);
CategoryVo queryByCid(Integer cid);
}
Many2ManyServiceImpl
package com.yq.ssm.service.impl;
import com.yq.ssm.mapper.HbookCategoryMapper;
import com.yq.ssm.model.vo.CategoryVo;
import com.yq.ssm.model.vo.HbookVo;
import com.yq.ssm.service.Many2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author 陌陌
* @create 2019-11-23 14:30
*/
@Service
public class Many2ManyServiceImpl implements Many2ManyService {
@Autowired
private HbookCategoryMapper hbookCategoryMapper;
@Override
public HbookVo queryByBookId(Integer bookId) {
return hbookCategoryMapper.queryByBookId(bookId);
}
@Override
public CategoryVo queryByCid(Integer cid) {
return hbookCategoryMapper.queryByCid(cid);
}
}
Many2ManyServiceImplTest
package com.yq.ssm.service.impl;
import com.yq.ssm.SpringBaseTest;
import com.yq.ssm.model.Category;
import com.yq.ssm.model.Hbook;
import com.yq.ssm.model.vo.CategoryVo;
import com.yq.ssm.model.vo.HbookVo;
import com.yq.ssm.service.Many2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import static org.junit.Assert.*;
/**
* @author 陌陌
* @create 2019-11-23 14:32
*/
public class Many2ManyServiceImplTest extends SpringBaseTest {
@Autowired
private Many2ManyService many2ManyService;
@Test
public void queryByBookId() {
HbookVo hbookVo=many2ManyService.queryByBookId(8);
System.out.println(hbookVo.getBookName());
for (Category category : hbookVo.getCategories()) {
System.out.println(category.getCategoryName());
}
}
@Test
public void queryByCid() {
CategoryVo categoryVo=many2ManyService.queryByCid(8);
System.out.println(categoryVo.getCategoryName());
for (Hbook hbook : categoryVo.getHbooks()) {
System.out.println(hbook.getBookName());
}
}
}