目录
mysql数据库表 t_hibernate_order、t_hibernate_order_item
表数据
首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model于mapper
generatorConfig.xml
- "1.0" encoding="UTF-8" ?>
- 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"/>
-
- <classPathEntry location="F:\\Courseware\\SoftwareInstallPath\\maven\\apache-maven-3.5.0-bin\\apache-maven-3.5.0-bin\\mvn_repository\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/>
-
- <context id="infoGuardian">
-
- <commentGenerator>
- <property name="suppressAllComments" value="true"/>
- <property name="suppressDate" value="true"/>
- commentGenerator>
-
-
- <jdbcConnection driverClass="${jdbc.driver}"
- connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>
-
-
- <javaTypeResolver>
-
- <property name="forceBigDecimals" value="false"/>
- javaTypeResolver>
-
-
-
-
- <javaModelGenerator targetPackage="com.zking.ssm.model"
- targetProject="src/main/java">
-
- <property name="enableSubPackages" value="false"/>
-
- <property name="constructorBased" value="true"/>
-
- <property name="trimStrings" value="false"/>
-
- <property name="immutable" value="false"/>
- javaModelGenerator>
-
-
- <sqlMapGenerator targetPackage="com.zking.ssm.mapper"
- targetProject="src/main/java">
-
- <property name="enableSubPackages" value="false"/>
- sqlMapGenerator>
-
-
-
-
-
- <javaClientGenerator targetPackage="com.zking.ssm.mapper"
- targetProject="src/main/java" type="XMLMAPPER">
-
- <property name="enableSubPackages" value="false"/>
- javaClientGenerator>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- <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>
-
- context>
- generatorConfiguration>
生成好了之后注意如图所示:
需求1:通过订单号查询本次订单的详细信息及对应的所以订单信息
select * from t_hibernate_order o,t_hibernate_order_item oi
where o.order_id = oi.oid and o.order_id = 9数据库里查询出的结果如图所示:
OrderVo.java
- package com.zking.ssm.model.vo;
-
- import com.zking.ssm.model.Order;
- import com.zking.ssm.model.OrderItem;
-
- import java.util.List;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 21:40
- *
- * vo类不仅仅包含当前表的信息,还能包含相关联表的信息
- *
- * 当前订单会议一个或者多个订单项,多个订单项必须要集合进行接收
- */
- public class OrderVo extends Order {
-
- // 通过订单号查询本次订单的详细信息及对应的所以订单信息
- private List
orderItems; -
- public List
getOrderItems() { - return orderItems;
- }
-
- public void setOrderItems(List
orderItems) { - this.orderItems = orderItems;
- }
- }
OrderItemVo.java
- package com.zking.ssm.model.vo;
-
- import com.zking.ssm.model.Order;
- import com.zking.ssm.model.OrderItem;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 21:43
- *
- * 某一个订单项一定属于某个订单的
- */
- public class OrderItemVo extends OrderItem {
- private Order order;
-
- public Order getOrder() {
- return order;
- }
-
- public void setOrder(Order order) {
- this.order = order;
- }
- }
OrderMapper.xml
- "1.0" encoding="UTF-8" ?>
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.zking.ssm.mapper.OrderMapper" >
- <resultMap id="BaseResultMap" type="com.zking.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.zking.ssm.model.vo.OrderVo" >
-
- <result column="order_id" property="orderId">result>
- <result column="order_no" property="orderNo">result>
-
- <collection property="orderItems" ofType="com.zking.ssm.model.OrderItem">
- <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>
- collection>
- resultMap>
- <sql id="Base_Column_List" >
- order_id, order_no
- sql>
-
- <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>
-
-
- <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.zking.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.zking.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.zking.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.zking.ssm.model.Order" >
- update t_hibernate_order
- set order_no = #{orderNo,jdbcType=VARCHAR}
- where order_id = #{orderId,jdbcType=INTEGER}
- update>
- mapper>
OrderMapper.java
- package com.zking.ssm.mapper;
-
- import com.zking.ssm.model.Order;
- import com.zking.ssm.model.vo.OrderVo;
- import org.apache.ibatis.annotations.Param;
-
- import java.util.List;
-
- 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);
-
- OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
-
- }
OrderBiz.java
- package com.zking.ssm.biz;
-
- import com.zking.ssm.model.Order;
- import com.zking.ssm.model.vo.OrderVo;
- import org.apache.ibatis.annotations.Param;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 22:00
- */
- public interface OrderBiz {
-
- OrderVo queryOrderVoByOrderId(Integer orderId);
-
- }
OrderBizImpl.java
- package com.zking.ssm.biz;
-
- import com.zking.ssm.mapper.OrderMapper;
- import com.zking.ssm.model.Order;
- import com.zking.ssm.model.vo.OrderVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 22:01
- */
- @Service
- public class OrderBizImpl implements OrderBiz {
- @Autowired
- private OrderMapper orderMapper;
- @Override
- public OrderVo queryOrderVoByOrderId(Integer orderId) {
- return orderMapper.queryOrderVoByOrderId(orderId);
- }
- }
测试类:OrderBizImplTest.java
- package com.zking.ssm.biz;
-
- import com.zking.ssm.model.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;
-
- import static org.junit.Assert.*;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 22:02
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
- public class OrderBizImplTest {
- @Autowired
- private OrderBiz orderBiz;
-
- @Test
- public void queryOrderVoByOrderId() {
- OrderVo orderVo = orderBiz.queryOrderVoByOrderId(9);
- System.out.println(orderVo);
- orderVo.getOrderItems().forEach(System.out::println);
- }
- }
运行结果如图所示:
我们在pom.xml引入了这个
我们把实体类的Order.java所以的方法给删除,我们在这里面加一个@Data
- package com.zking.ssm.model;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- //t_hibernate_order
- //相当于添加了set/get方法,同时添加了tostring方法
- @Data
- //一个是有参构造器一个是无参构造器
- //@NoArgsConstructor
- //@AllArgsConstructor
- public class Order {
- private Integer orderId;
-
- private String orderNo;
-
- }
现在在去运行一下如图所示:把之前的地址换成了数据
需求2:通过订单项ID查询出订单项详细信息及所属订单
select * from t_hibernate_order o,t_hibernate_order_item oi
where o.order_id = oi.oid and oi.order_item_id=43
数据库查询出的结果如图所示:
OrderItemMapper.xml
- "1.0" encoding="UTF-8" ?>
- mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
- <mapper namespace="com.zking.ssm.mapper.OrderItemMapper" >
- <resultMap id="BaseResultMap" type="com.zking.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.zking.ssm.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.zking.ssm.model.Order">
- <result column="order_id" property="orderId">result>
- <result column="order_no" property="orderNo">result>
- association>
- resultMap>
-
- <sql id="Base_Column_List" >
- order_item_id, product_id, quantity, oid
- sql>
-
- <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>
-
- <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.zking.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.zking.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.zking.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.zking.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>
- mapper>
OrderItemMapper.java
- package com.zking.ssm.mapper;
-
- import com.zking.ssm.model.OrderItem;
- import com.zking.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);
-
- }
OrderItemBiz.java
- package com.zking.ssm.biz;
-
- import com.zking.ssm.model.vo.OrderItemVo;
- import org.apache.ibatis.annotations.Param;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 22:36
- */
- public interface OrderItemBiz {
-
- OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId);
-
- }
OrderItemBizImpl.java
- package com.zking.ssm.biz.impl;
-
- import com.zking.ssm.biz.OrderItemBiz;
- import com.zking.ssm.mapper.OrderItemMapper;
- import com.zking.ssm.model.OrderItem;
- import com.zking.ssm.model.vo.OrderItemVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 22:38
- */
- @Service
- public class OrderItemBizImpl implements OrderItemBiz {
- @Autowired
- private OrderItemMapper orderItemMapper;
- @Override
- public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
- return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
- }
- }
在我们的OrderItem.java里面加一个@ToString就好了,这样就不会是地址了
- package com.zking.ssm.model;
-
- import lombok.ToString;
-
- @ToString
- public class OrderItem {
- private Integer orderItemId;
-
- private Integer productId;
-
- private Integer quantity;
-
- private Integer oid;
-
- public OrderItem(Integer orderItemId, Integer productId, Integer quantity, Integer oid) {
- this.orderItemId = orderItemId;
- this.productId = productId;
- this.quantity = quantity;
- this.oid = oid;
- }
-
- public OrderItem() {
- super();
- }
-
- public Integer getOrderItemId() {
- return orderItemId;
- }
-
- public void setOrderItemId(Integer orderItemId) {
- this.orderItemId = orderItemId;
- }
-
- public Integer getProductId() {
- return productId;
- }
-
- public void setProductId(Integer productId) {
- this.productId = productId;
- }
-
- public Integer getQuantity() {
- return quantity;
- }
-
- public void setQuantity(Integer quantity) {
- this.quantity = quantity;
- }
-
- public Integer getOid() {
- return oid;
- }
-
- public void setOid(Integer oid) {
- this.oid = oid;
- }
- }
测试类OrderItemBizTest.java
- package com.zking.ssm.biz;
-
- import com.zking.ssm.model.vo.OrderItemVo;
- 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;
-
- import static org.junit.Assert.*;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 22:45
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
- public class OrderItemBizImplTest {
- @Autowired
- private OrderItemBiz orderItemBiz;
-
- @Test
- public void queryOrderItemVoByOrderItemId() {
- OrderItemVo orderItemVo = orderItemBiz.queryOrderItemVoByOrderItemId(43);
- System.out.println(orderItemVo);
- System.out.println(orderItemVo.getOrder());
- }
- }
运行结果如图所示:
多对多就是两个一对多
首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这两张表对应的model与mapper
generatorConfig.xml
- <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>
需求1:根据书籍ID,查询书籍信息及所属所有类别信息
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 = 8需求2:根据类别ID,查询类别信息及所包含书籍信息
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=8
HbookVo.java
- package com.zking.ssm.model.vo;
-
- import com.zking.ssm.model.Category;
- import com.zking.ssm.model.Hbook;
-
- import java.util.List;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 23:18
- */
- public class HbookVo extends Hbook {
- private List
categories; -
- public List
getCategories() { - return categories;
- }
-
- public void setCategories(List
categories) { - this.categories = categories;
- }
- }
CategoryVo.java
- package com.zking.ssm.model.vo;
-
- import com.zking.ssm.model.Category;
- import com.zking.ssm.model.Hbook;
-
- import java.util.List;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 23:19
- */
- public class CategoryVo extends Category {
- private List
hbooks; -
- public List
getHbooks() { - return hbooks;
- }
-
- public void setHbooks(List
hbooks) { - this.hbooks = hbooks;
- }
- }
加到中间表HbookCategoryMapper.xml,要不然你两边表都要加
- <resultMap id="HbookVoMap" type="com.zking.ssm.model.vo.HbookVo">
- <result property="bookId" column="book_id">result>
- <result property="bookName" column="book_name">result>
- <collection property="categories" ofType="com.javaxl.ssm.model.Category">
- <result property="categoryId" column="category_id">result>
- <result property="categoryName" column="category_name">result>
- collection>
- resultMap>
-
- <resultMap id="CategoryVoMap" type="com.zking.ssm.model.vo.CategoryVo">
- <result property="categoryId" column="category_id">result>
- <result property="categoryName" column="category_name">result>
- <collection property="hbooks" ofType="com.javaxl.ssm.model.Hbook">
- <result property="bookId" column="book_id">result>
- <result property="bookName" column="book_name">result>
- collection>
- resultMap>
-
- <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>
HbookCategoryMapper.java
- package com.zking.ssm.mapper;
-
- import com.zking.ssm.model.HbookCategory;
- import com.zking.ssm.model.vo.CategoryVo;
- import com.zking.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);
- }
HbookCategoryBiz.java
- package com.zking.ssm.biz;
-
- import com.zking.ssm.model.vo.CategoryVo;
- import com.zking.ssm.model.vo.HbookVo;
- import org.apache.ibatis.annotations.Param;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 23:27
- */
- public interface HbookCategoryBiz {
-
- HbookVo queryByBookId(Integer bookId);
-
- CategoryVo queryByCid(Integer cid);
- }
HbookCategoryBizImpl.java
- package com.zking.ssm.biz.impl;
-
- import com.zking.ssm.biz.HbookCategoryBiz;
- import com.zking.ssm.mapper.HbookCategoryMapper;
- import com.zking.ssm.model.vo.CategoryVo;
- import com.zking.ssm.model.vo.HbookVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 23:28
- */
- @Service
- public class HbookCategoryBizImpl implements HbookCategoryBiz {
- @Autowired
- private HbookCategoryMapper hbookCategoryMapper;
- @Override
- public HbookVo queryByBookId(Integer bookId) {
- return hbookCategoryMapper.queryByBookId(bookId);
- }
-
- @Override
- public CategoryVo queryByCid(Integer cid) {
- return hbookCategoryMapper.queryByCid(cid);
- }
- }
HbookVo.java 加一个ToString不然生成的就是地址
- package com.zking.ssm.model.vo;
-
- import com.zking.ssm.model.Category;
- import com.zking.ssm.model.Hbook;
- import lombok.ToString;
-
- import java.util.List;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 23:18
- */
- @ToString
- public class HbookVo extends Hbook {
- private List
categories; -
- public List
getCategories() { - return categories;
- }
-
- public void setCategories(List
categories) { - this.categories = categories;
- }
- }
Category.java 也生成一个@ToString
- package com.zking.ssm.model;
-
- import lombok.ToString;
-
- @ToString
- public class Category {
- private Integer categoryId;
-
- private String categoryName;
-
- public Category(Integer categoryId, String categoryName) {
- this.categoryId = categoryId;
- this.categoryName = categoryName;
- }
-
- public Category() {
- super();
- }
-
- public Integer getCategoryId() {
- return categoryId;
- }
-
- public void setCategoryId(Integer categoryId) {
- this.categoryId = categoryId;
- }
-
- public String getCategoryName() {
- return categoryName;
- }
-
- public void setCategoryName(String categoryName) {
- this.categoryName = categoryName;
- }
- }
生成一个测试类 HbookCategoryBizImplTest.java
- package com.zking.ssm.biz.impl;
-
- import com.zking.ssm.biz.HbookCategoryBiz;
- import com.zking.ssm.model.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;
-
- import static org.junit.Assert.*;
-
- /**
- * @author 敢敢
- * @site www.javajwj.com
- * @company xxx公司
- * @create 2022-08-16 23:30
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
- public class HbookCategoryBizImplTest {
- @Autowired
- private HbookCategoryBiz hbookCategoryBiz;
-
- @Test
- public void queryByBookId() {
- HbookVo hbookVo = hbookCategoryBiz.queryByBookId(8);
- System.out.println(hbookVo);
- // 书籍所关联的类别
- hbookVo.getCategories().forEach(System.out::println);
- }
-
- @Test
- public void queryByCid() {
- CategoryVo categoryVo = hbookCategoryBiz.queryByCid(8);
- System.out.println(categoryVo);
- categoryVo.getHbooks().forEach(System.out::println);
- }
- }
第一个方法运行结果如图所示:
第二个方法运行结果如图所示:
一对多
一方:Collection:属性ofType
多方:associate:属性JavaType
多对多:就是两个一对多
vo类:view Object
entity/model描述数据库表