关联关系映射 mybatis中的一对一,一对多关联关系配置及多对多

简介: 关联关系映射 mybatis中的一对一,一对多关联关系配置及多对多
  • mybatis中的一对一和一对多关联关系配置
  • mybatis中多对多的关联关系配置

1.mybatis中的一对一和一对多关联关系配置

一对多案例:selectByOid,先在数据库准备好五张表,并在generatorConfig.xml中一一对应好这五张表,然后按照,自动生成Order->OrderMapper.xml->OrderVo->OrderMapper->OrderBiz->OrderBizImpl->OrderBizImplTest进行测试(关于配置联合上一篇文档)

package com.zlj.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;
    }
}
//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:\\software\\apache-maven-3.5.0-bin\\apache-maven-3.5.0-bin\\apache-maven-3.5.0\\conf\\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.zlj.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.zlj.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.zlj.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">-->
        <!--&lt;!&ndash; 忽略列,不生成bean 字段 &ndash;&gt;-->
        <!--&lt;!&ndash; <ignoreColumn column="FRED" /> &ndash;&gt;-->
        <!--&lt;!&ndash; 指定列的java数据类型 &ndash;&gt;-->
        <!--&lt;!&ndash; <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> &ndash;&gt;-->
        <!--</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>
<?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.zlj.mapper.OrderMapper" >
  <resultMap id="BaseResultMap" type="com.zlj.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.zlj.vo.OrderVo" >
    <result column="order_id" property="orderId"></result>
    <result column="order_no" property="orderNo"></result>
    <collection property="orderItems" ofType="com.zlj.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.zlj.model.Order" >
    insert into t_hibernate_order (order_id, order_no)
    values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.zlj.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.zlj.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.zlj.model.Order" >
    update t_hibernate_order
    set order_no = #{orderNo,jdbcType=VARCHAR}
    where order_id = #{orderId,jdbcType=INTEGER}
  </update>
</mapper>
package com.zlj.vo;
import com.zlj.model.Order;
import com.zlj.model.OrderItem;
import java.util.ArrayList;
import java.util.List;
/**
 * @author zlj
 * @create 2023-08-29 20:53
 */
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;
    }
}
package com.zlj.biz.impl;
import com.zlj.biz.OrderBiz;
import com.zlj.mapper.OrderMapper;
import com.zlj.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @author zlj
 * @create 2023-08-30 15:37
 */
@Service
public class OrderBizImpl implements OrderBiz {
    @Autowired
    private OrderMapper orderMapper;
    @Override
    public OrderVo selectByOid(Integer oid) {
        return orderMapper.selectByOid(oid);
    }
}
package com.zlj.biz.impl;
import com.zlj.biz.OrderBiz;
import com.zlj.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 zlj
 * @create 2023-08-30 15:42
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-content.xml"})
public class OrderBizImplTest {
@Autowired
private OrderBiz orderBiz;
    @Test
    public void selectByOid() {
        OrderVo orderVo = orderBiz.selectByOid(10);
        System.out.println(orderVo);
//        orderVo.getOrderItems().forEach(System.out::println);
    }
}

一对一案例:selectByOrderItemId,先在数据库准备好五张表,并在generatorConfig.xml中一一对应好这五张表,然后按照,自动生成OrderItem->OrderItemMapper.xml->OrderItemVo->OrderItemMapper->OrderItemBiz->OrderItemBizImpl->OrderItemBizImplTest进行测试

package com.zlj.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;
    }
}
<?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.zlj.mapper.OrderItemMapper" >
  <resultMap id="BaseResultMap" type="com.zlj.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.zlj.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.zlj.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.zlj.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.zlj.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.zlj.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.zlj.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>
package com.zlj.vo;
import com.zlj.model.Order;
import com.zlj.model.OrderItem;
/**
 * @author zlj
 * @create 2023-08-31 22:28
 */
public class OrderItemVo extends OrderItem {
    private Order order;
    public Order getOrder() {
        return order;
    }
    public void setOrder(Order order) {
        this.order = order;
    }
}
package com.zlj.mapper;
import com.zlj.model.OrderItem;
import com.zlj.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 selectByOrderItemId(@Param("oiid") Integer oiid);
}
package com.zlj.biz;
import com.zlj.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
/**
 * @author zlj
 * @create 2023-08-31 22:51
 */
public interface OrderItemBiz {
    OrderItemVo selectByOrderItemId(@Param("oiid") Integer oiid);
}
package com.zlj.biz.impl;
import com.zlj.biz.OrderItemBiz;
import com.zlj.mapper.OrderItemMapper;
import com.zlj.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @author zlj
 * @create 2023-08-31 22:52
 */
@Service
public class OrderItemImpl implements OrderItemBiz {
@Autowired
private OrderItemMapper orderItemMapper;
    @Override
    public OrderItemVo selectByOrderItemId(Integer oiid) {
        return orderItemMapper.selectByOrderItemId(oiid);
    }
}
package com.zlj.biz.impl;
import com.zlj.biz.OrderItemBiz;
import com.zlj.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;
/**
 * @author zlj
 * @create 2023-08-31 22:57
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-content.xml"})
public class OrderItemImplTest {
    @Autowired
    private OrderItemBiz orderItemBiz;
    @Test
    public void selectByOrderItemId() {
        OrderItemVo orderItemVo = orderItemBiz.selectByOrderItemId(27);
        System.out.println(orderItemVo);
        System.out.println(orderItemVo.getOrder());
    }
}

2.mybatis中多对多的关联关系配置

多对多案例:selectByBookId和selectByCategoryId

package com.zlj.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class HBook {
    private Integer bookId;
    private String bookName;
    private Float price;
}
<?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.zlj.mapper.HBookMapper">
    <resultMap id="BaseResultMap" type="com.zlj.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.zlj.vo.HBookVo">
        <result column="book_id" property="bookId"></result>
        <result column="book_name" property="bookName"></result>
        <result column="price" property="price"></result>
        <collection property="hBookCategories" ofType="com.zlj.model.HCategory">
            <result column="category_id" property="categoryId"></result>
            <result column="category_name" property="categoryName"></result>
        </collection>
    </resultMap>
    <!--根据书籍的id查询出书籍信息及属性类别-->
    <select id="selectByBookId" 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_category-->
<!--        where bcid = #{bcid,jdbcType=INTEGER}-->
<!--    </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.zlj.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.zlj.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.zlj.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.zlj.model.HBook">
    update t_hibernate_book
    set book_name = #{bookName,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where book_id = #{bookId,jdbcType=INTEGER}
  </update>
</mapper>
package com.zlj.vo;
import com.zlj.model.HBook;
import com.zlj.model.HCategory;
import java.util.ArrayList;
import java.util.List;
/**
 * @author zlj
 * @create 2023-09-01 22:56
 */
public class HBookVo extends HBook {
    private List<HCategory> hBookCategories = new ArrayList<>();
    public List<HCategory> gethBookCategories() {
        return hBookCategories;
    }
    public void sethBookCategories(List<HCategory> hBookCategories) {
        this.hBookCategories = hBookCategories;
    }
}
package com.zlj.mapper;
import com.zlj.model.HBook;
import com.zlj.vo.HBookVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface HBookMapper {
    int deleteByPrimaryKey(Integer bookId);
    int insert(HBook record);
    int insertSelective(HBook record);
    HBook selectByPrimaryKey(Integer bookId);
    int updateByPrimaryKeySelective(HBook record);
    int updateByPrimaryKey(HBook record);
    HBookVo selectByBookId(@Param("bid") Integer bid);
}
package com.zlj.biz;
import com.zlj.vo.HBookVo;
import org.springframework.stereotype.Service;
/**
 * @author zlj
 * @create 2023-09-01 23:27
 */
@Service
public interface HBookBiz {
    HBookVo selectByBookId(Integer bid);
}
package com.zlj.biz.impl;
import com.zlj.biz.HBookBiz;
import com.zlj.mapper.HBookMapper;
import com.zlj.vo.HBookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @author zlj
 * @create 2023-09-01 23:29
 */
@Service
public class HBookBizImpl implements HBookBiz {
    @Autowired
    private HBookMapper hBookMapper;
    @Override
    public HBookVo selectByBookId(Integer bid) {
        return hBookMapper.selectByBookId(bid);
    }
}
package com.zlj.biz.impl;
import com.zlj.biz.HBookBiz;
import com.zlj.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 zlj
 * @create 2023-09-01 23:30
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-content.xml"})
public class HBookBizImplTest {
  @Autowired
  private HBookBiz hBookBiz;
    @Test
    public void selectByBookId() {
        HBookVo hbookVo = this.hBookBiz.selectByBookId(8);
        System.out.println(hbookVo);
        hbookVo.gethBookCategories().forEach(System.out::println);
    }
}

<?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.zlj.mapper.HCategoryMapper" >
  <resultMap id="BaseResultMap" type="com.zlj.model.HCategory" >
    <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>
  <resultMap id="CategoryVoMap" type="com.zlj.vo.CategoryVo">
    <result column="category_id" property="categoryId"></result>
    <result column="category_name" property="categoryName"></result>
   <collection property="books" ofType="com.zlj.model.HBook">
     <result column="book_id" property="bookId"></result>
     <result column="book_name" property="bookName"></result>
     <result column="price" property="price"></result>
   </collection>
  </resultMap>
  <select id="selectByCategoryId" 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>
  <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.zlj.model.HCategory" >
    insert into t_hibernate_category (category_id, category_name)
    values (#{categoryId,jdbcType=INTEGER}, #{categoryName,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.zlj.model.HCategory" >
    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.zlj.model.HCategory" >
    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.zlj.model.HCategory" >
    update t_hibernate_category
    set category_name = #{categoryName,jdbcType=VARCHAR}
    where category_id = #{categoryId,jdbcType=INTEGER}
  </update>
</mapper>
<?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.zlj.mapper.OrderItemMapper" >
  <resultMap id="BaseResultMap" type="com.zlj.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.zlj.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.zlj.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.zlj.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.zlj.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.zlj.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.zlj.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>
package com.zlj.vo;
import com.zlj.model.HBook;
import com.zlj.model.HCategory;
import java.util.ArrayList;
import java.util.List;
/**
 * @author zlj
 * @create 2023-09-03 17:38
 */
public class CategoryVo extends HCategory {
    private List<HBook> books=new ArrayList<>();
    public List<HBook> getBooks() {
        return books;
    }
    public void setBooks(List<HBook> books) {
        this.books = books;
    }
}
package com.zlj.biz;
import com.zlj.vo.CategoryVo;
/**
 * @author zlj
 * @create 2023-09-03 19:06
 */
public interface HCategoryBiz {
    CategoryVo selectByCategoryId(Integer cid);
}
package com.zlj.biz.impl;
import com.zlj.biz.HCategoryBiz;
import com.zlj.mapper.HCategoryMapper;
import com.zlj.vo.CategoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @author zlj
 * @create 2023-09-03 19:14
 */
@Service
public class HCategoryIdBizImpl implements HCategoryBiz {
    @Autowired
    private HCategoryMapper hCategoryMapper;
    @Override
    public CategoryVo selectByCategoryId(Integer cid) {
        return hCategoryMapper.selectByCategoryId(cid);
    }
}
package com.zlj.biz.impl;
import com.zlj.biz.HCategoryBiz;
import com.zlj.vo.CategoryVo;
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 zlj
 * @create 2023-09-03 19:17
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-content.xml"})
public class HCategoryIdBizImplTest {
@Autowired
private HCategoryBiz hCategoryBiz;
    @Test
    public void selectByCategoryId() {
        CategoryVo categoryVo = hCategoryBiz.selectByCategoryId(8);
        System.out.println(categoryVo);
        categoryVo.getBooks().forEach(System.out::println);
    }
}

目录
相关文章
|
15天前
|
前端开发 Java 数据库连接
Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin)
Springboot-MyBatis配置-配置端口号与服务路径(idea社区版2023.1.4+apache-maven-3.9.3-bin)
12 0
|
21天前
|
缓存 Java 数据库连接
|
1月前
|
算法 Java 数据库连接
实现 MyBatis-Plus 中的配置加密功能(使用 AES 算法)
实现 MyBatis-Plus 中的配置加密功能(使用 AES 算法)
38 0
|
1月前
|
缓存 Java 数据库连接
Mybatis-1.常见配置
Mybatis-1.常见配置
|
1月前
|
Java 数据库连接 数据库
【Spring技术专题】「实战开发系列」保姆级教你SpringBoot整合Mybatis框架实现多数据源的静态数据源和动态数据源配置落地
Mybatis是一个基于JDBC实现的,支持普通 SQL 查询、存储过程和高级映射的优秀持久层框架,去掉了几乎所有的 JDBC 代码和参数的手工设置以及对结果集的检索封装。 Mybatis主要思想是将程序中大量的 SQL 语句剥离出来,配置在配置文件中,以实现 SQL 的灵活配置。在所有 ORM 框架中都有一个非常重要的媒介——PO(持久化对象),PO 的作用就是完成持久化操作,通过该对象对数据库执行增删改的操作,以面向对象的方式操作数据库。
30 1
【Spring技术专题】「实战开发系列」保姆级教你SpringBoot整合Mybatis框架实现多数据源的静态数据源和动态数据源配置落地
|
1月前
|
Java 数据库连接 mybatis
一文彻底搞懂Mybatis系列(十三)之MyBatis一对多映射查询
一文彻底搞懂Mybatis系列(十三)之MyBatis一对多映射查询
|
2月前
|
Java 关系型数据库 数据库连接
Hasor【环境搭建 02】Dataway接口配置服务使用DataQL聚合查询引擎(Mybatis执行器实现分页查询举例说明+问题分析)
Hasor【环境搭建 02】Dataway接口配置服务使用DataQL聚合查询引擎(Mybatis执行器实现分页查询举例说明+问题分析)
20 0
|
Java 数据库连接 mybatis
MyBatis-Spring配置简单了解
在基本的 MyBatis 中,session 工厂可以使用 SqlSessionFactoryBuilder 来创建。而在 MyBatis-spring 中,则使用 SqlSessionFactoryBean 来替代。
827 0
|
23天前
|
SQL Oracle 关系型数据库
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作
整合Mybatis-Plus高级,Oracle 主键Sequence,Sql 注入器实现自定义全局操作
74 0
|
2天前
|
存储 SQL 数据安全/隐私保护
Mybatis-Plus 拦截SQL语句实现加解密存储
Mybatis-Plus 拦截SQL语句实现加解密存储
8 0