赞
踩
目录
关联关系映射是指在数据库中,通过定义表之间的关联关系,将多个表的数据进行关联查询和映射的过程。通过关联关系映射,可以方便地获取相关联表的数据,并将其映射到对应的实体类中。
Mybatis是一种Java持久层框架,它提供了一种将数据库表和Java对象之间进行关联关系映射的方式。在Mybatis中,可以通过配置文件或注解的方式,定义表之间的关联关系,并将查询结果映射到对应的Java对象中。通过Mybatis的关联关系映射,可以方便地进行复杂的数据库查询操作。
在Mybatis中,可以通过配置映射文件来实现关联关系的映射。通过定义实体类和映射文件,可以将数据库中的多个表进行关联查询,并将查询结果映射到实体类的属性上。
在进行数据库查询操作时,通过关联查询获取到关联表中的相关数据,并将其映射到实体类中的关联属性上。
总结来说,关联关系映射是指通过配置映射文件,将数据库中的关联表进行关联查询,并将查询结果映射到实体类的关联属性上。通过扩展关联关系映射,可以方便地进行复杂的数据库查询操作,提高查询效率和简化数据操作的代码。
( 2 ) 关联关系映射
关联关系映射在Mybatis中主要通过三种方式实现:一对一关联和一对多关联及多对多关联。
一对一关联:
在一对一关联中,两个表之间存在一对一的关系,例如学生表和身份证表,一个学生只有一个身份证,而一个身份证也只属于一个学生。在Mybatis中,可以通过在实体类中定义关联属性,然后在映射文件中使用<association>标签来定义关联关系。通过配置映射关系,可以查询到学生表和身份证表的数据,并将其映射到对应的实体类中。一对多关联:
在一对多关联中,两个表之间存在一对多的关系,例如部门表和员工表,一个部门可以有多个员工,而一个员工只属于一个部门。在Mybatis中,可以通过在实体类中定义关联属性,然后在映射文件中使用<collection>标签来定义关联关系。通过配置映射关系,可以查询到部门表和员工表的数据,并将其映射到对应的实体类中。多对多关联:
是指两个表之间存在多对多的关联关系,即一个表的一条记录可以对应另一个表的多条记录,反之亦然。在数据库中,多对多关系通常需要通过中间表来实现。
在Mybatis中,可以通过定义中间表来映射多对多关系。假设有两个表,学生表和课程表,一个学生可以选择多门课程,而一门课程也可以被多个学生选择。为了映射这种多对多关系,需要创建一个中间表,例如选课表,用来记录学生和课程的关联关系。
修改名为 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="D:\\maven\\mvn_repository\\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.sy.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.sy.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.sy.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_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="HCategory"
- enableCountByExample="false" enableDeleteByExample="false"
- enableSelectByExample="false" enableUpdateByExample="false">
- </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>
- </context>
- </generatorConfiguration>
配置好 generatorConfig.xml 的配置文件后进行,自动生成代码
代码生成后就会出现以下的实体类对象,配置文件,接口类
然后将每个实体类对象里面重写一下toString()方法
创建一个 名为 OrderItemVo 的类,继承OrderItem类,及属性有Order对象
- package com.sy.vo;
-
- import com.sy.model.Order;
- import com.sy.model.OrderItem;
-
- /**
- * @author 谌艳
- * @site www.shenyan.com
- * @create 2023-08-26 16:40
- */
- public class OrderItemVo extends OrderItem {
-
- private Order order;
-
- public Order getOrder() {
- return order;
- }
-
- public void setOrder(Order order) {
- this.order = order;
- }
- }
在自动生成的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.sy.mapper.OrderItemMapper" >
- <resultMap id="BaseResultMap" type="com.sy.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>
- <sql id="Base_Column_List" >
- order_item_id, product_id, quantity, oid
- </sql>
-
- <resultMap id="OrderItemVoMap" type="com.sy.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.sy.model.Order">
- <result column="order_id" property="orderId" ></result>
- <result column="order_no" property="orderNo" ></result>
- </association>
- </resultMap>
-
-
- <select id="selectByOrderItemId" 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 =#{oiid}
- </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.sy.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.sy.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.sy.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.sy.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 接口中进行增加以下代码
OrderItemVo selectByOrderItemId(@Param("oiid") Integer oiid);
创建一个接口名为 : OrderItemBiz 接口
- package com.sy.biz;
-
- import com.sy.vo.OrderItemVo;
-
- public interface OrderItemBiz {
-
-
- OrderItemVo selectByOrderItemId(Integer oiid);
- }
实现以上创建的接口,创建一个实现了名为 OrderItemBizImpl 类
- package com.sy.biz.impl;
-
- import com.sy.biz.OrderItemBiz;
- import com.sy.mapper.OrderItemMapper;
- import com.sy.vo.OrderItemVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- /**
- * @author 谌艳
- * @site www.shenyan.com
- * @create 2023-08-26 16:53
- */
- @Service
- public class OrderItemBizImpl implements OrderItemBiz {
- @Autowired
- private OrderItemMapper orderItemMapper;
- @Override
- public OrderItemVo selectByOrderItemId(Integer oiid) {
- return orderItemMapper.selectByOrderItemId(oiid);
- }
- }
创建一个测试类进行方法测试
- package com.sy.biz.impl;
-
- import com.sy.biz.OrderBiz;
- import com.sy.biz.OrderItemBiz;
- import com.sy.vo.OrderItemVo;
- import com.sy.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 www.shenyan.com
- * @create 2023-08-26 15:56
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:spring-context.xml"})
- public class OrderBizImplTest {
- @Autowired
- private OrderBiz orderBiz;
- @Autowired
- private OrderItemBiz orderItemBiz;
-
-
- @Test
- public void selsctByOrderItemId() {
- OrderItemVo orderItemVo = orderItemBiz.selectByOrderItemId(27);
- System.out.println(orderItemVo);
- System.out.println(orderItemVo.getOrder());
- }
-
-
-
-
- @Test
- public void selsctByOid() {
- OrderVo orderVo = orderBiz.selsctByOid(7);
- System.out.println(orderVo);
- orderVo.getOrderItems().forEach(System.out::println);
- }
- }
结果展示:
创建一个 名为 OrdeVo 的类,继承Order类,及属性有List<OrderItem>集合
用来存储查询到的OrderItem对象,因为是一对多所有查询出有多个对象
- package com.sy.vo;
-
- import com.sy.model.Order;
- import com.sy.model.OrderItem;
-
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * @author 谌艳
- * @site www.shenyan.com
- * @create 2023-08-26 15:29
- */
- public class OrderVo extends Order {
-
- private List<OrderItem> orderItems=new ArrayList<OrderItem>();
-
-
- public List<OrderItem> getOrderItems() {
- return orderItems;
- }
-
- public void setOrderItems(List<OrderItem> orderItems) {
- this.orderItems = orderItems;
- }
-
- }
在自动生成的 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.sy.mapper.OrderMapper" >
- <resultMap id="BaseResultMap" type="com.sy.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>
- <sql id="Base_Column_List" >
- order_id, order_no
- </sql>
-
- <resultMap id="OrderVoMap" type="com.sy.vo.OrderVo" >
- <result column="order_id" property="orderId" ></result>
- <result column="order_no" property="orderNo" ></result>
- <collection property="orderItems" ofType="com.sy.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>
-
-
- <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>
-
-
- <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.sy.model.Order" >
- insert into t_hibernate_order (order_id, order_no)
- values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
- </insert>
- <insert id="insertSelective" parameterType="com.sy.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.sy.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.sy.model.Order" >
- update t_hibernate_order
- set order_no = #{orderNo,jdbcType=VARCHAR}
- where order_id = #{orderId,jdbcType=INTEGER}
- </update>
- </mapper>
在自动生成的 OrderMapper接口中进行增加以下代码
OrderVo selectByOid(@Param("oid") Integer Oid);
创建一个接口名为 : OrderBiz 接口
- package com.sy.biz;
-
- import com.sy.vo.OrderVo;
-
- public interface OrderBiz {
-
- OrderVo selsctByOid(Integer Oid);
- }
在实现以上 OrderBiz 接口,创建一个实现类,名为 OrderBizImpl
- package com.sy.biz.impl;
-
- import com.sy.biz.OrderBiz;
- import com.sy.mapper.OrderMapper;
- import com.sy.vo.OrderVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- /**
- * @author 谌艳
- * @site www.shenyan.com
- * @create 2023-08-26 15:53
- */
- @Service
- public class OrderBizImpl implements OrderBiz {
- @Autowired
- private OrderMapper orderMapper;
-
- @Override
- public OrderVo selsctByOid(Integer Oid) {
- return orderMapper.selectByOid(Oid);
- }
- }
在测试类中测试方法及接口
在自动生成的 HBookMapper.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.sy.mapper.HBookMapper" >
- <resultMap id="BaseResultMap" type="com.sy.model.HBook" >
- <constructor >
- <idArg column="book_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
- <arg column="book_name" jdbcType="VARCHAR" javaType="java.lang.String" />
- <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
- </constructor>
- </resultMap>
- <sql id="Base_Column_List" >
- book_id, book_name, price
- </sql>
-
- <resultMap id="HbookVoMap" type="com.sy.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.sy.model.HCategory">
- <result column="category_id" property="categoryId"></result>
- <result column="category_name" property="categoryName"></result>
- </collection>
- </resultMap>
-
- <!-- 根据书籍id查询出书籍信息及所属类别-->
- <select id="selectByBidMap" 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="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
- select
- <include refid="Base_Column_List" />
- from t_hibernate_book
- where book_id = #{bookId,jdbcType=INTEGER}
- </select>
- <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
- delete from t_hibernate_book
- where book_id = #{bookId,jdbcType=INTEGER}
- </delete>
- <insert id="insert" parameterType="com.sy.model.HBook" >
- insert into t_hibernate_book (book_id, book_name, price
- )
- values (#{bookId,jdbcType=INTEGER}, #{bookName,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
- )
- </insert>
- <insert id="insertSelective" parameterType="com.sy.model.HBook" >
- insert into t_hibernate_book
- <trim prefix="(" suffix=")" suffixOverrides="," >
- <if test="bookId != null" >
- book_id,
- </if>
- <if test="bookName != null" >
- book_name,
- </if>
- <if test="price != null" >
- price,
- </if>
- </trim>
- <trim prefix="values (" suffix=")" suffixOverrides="," >
- <if test="bookId != null" >
- #{bookId,jdbcType=INTEGER},
- </if>
- <if test="bookName != null" >
- #{bookName,jdbcType=VARCHAR},
- </if>
- <if test="price != null" >
- #{price,jdbcType=REAL},
- </if>
- </trim>
- </insert>
- <update id="updateByPrimaryKeySelective" parameterType="com.sy.model.HBook" >
- update t_hibernate_book
- <set >
- <if test="bookName != null" >
- book_name = #{bookName,jdbcType=VARCHAR},
- </if>
- <if test="price != null" >
- price = #{price,jdbcType=REAL},
- </if>
- </set>
- where book_id = #{bookId,jdbcType=INTEGER}
- </update>
- <update id="updateByPrimaryKey" parameterType="com.sy.model.HBook" >
- update t_hibernate_book
- set book_name = #{bookName,jdbcType=VARCHAR},
- price = #{price,jdbcType=REAL}
- where book_id = #{bookId,jdbcType=INTEGER}
- </update>
- </mapper>
在自动生成的 HBookMapper 接口 中增加以下方法
HbookVo selectByBidMap(@Param("bid") Integer bid);
创建一个接口名为 HBookBiz
- package com.sy.biz;
-
- import com.sy.vo.HbookVo;
- import org.apache.ibatis.annotations.Param;
-
- public interface HBookBiz {
-
- HbookVo selectByBidMap(@Param("bid") Integer bid);
-
- }
在实现以上 HBookBiz 接口,创建一个实现类,名为 HBookBizImpl
- package com.sy.biz.impl;
-
- import com.sy.biz.HBookBiz;
- import com.sy.mapper.HBookMapper;
- import com.sy.vo.HbookVo;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- /**
- * @author 谌艳
- * @site www.shenyan.com
- * @create 2023-08-26 22:09
- */
- @Service
- public class HBookBizImpl implements HBookBiz {
-
- @Autowired
- private HBookMapper hbookMapper;
-
- @Override
- public HbookVo selectByBidMap(Integer bid) {
- return hbookMapper.selectByBidMap(bid);
- }
- }
在测试类中下测试方法及接口
- package com.sy.biz.impl;
-
- import com.sy.biz.HBookBiz;
- import com.sy.biz.HCategoryBiz;
- import com.sy.vo.CategoryVo;
- import com.sy.vo.HbookVo;
- 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 谌艳
- * @site www.shenyan.com
- * @create 2023-08-26 22:11
- */
- @RunWith(SpringJUnit4ClassRunner.class)
- @ContextConfiguration(locations={"classpath:spring-context.xml"})
- public class HBookBizImplTest {
-
- @Autowired
- private HBookBiz hbookBiz;
- @Autowired
- private HCategoryBiz hcategoryBiz;
-
-
- @Before
- public void setUp() throws Exception {
- }
-
- @After
- public void tearDown() throws Exception {
- }
-
- @Test
- public void selectByBidMap() {
- HbookVo hbookVo = hbookBiz.selectByBidMap(8);
- System.out.println(hbookVo);
- hbookVo.gethCategories().forEach(System.out::println);
- }
-
- @Test
- public void selectByCategorYId() {
- CategoryVo categoryVo = hcategoryBiz.selectByCategoryId(8);
- System.out.println(categoryVo);
- categoryVo.getBooks().forEach(System.out::println);
- }
-
-
-
- }
结果展示:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。