• Mybatis, 实现一对多


    我这里是拿商品做为例子

    不多说直接上代码

    Mapper.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.nf147.mall.dao.CommodityMapper">
        <resultMap id="BaseResultMap" type="com.nf147.mall.entity.Commodity">
            <id column="product_id" jdbcType="INTEGER" property="productId"/>
            <result column="category_id" jdbcType="INTEGER" property="categoryId"/>
            <result column="product_code" jdbcType="VARCHAR" property="productCode"/>
            <result column="product_name" jdbcType="VARCHAR" property="productName"/>
            <result column="product_content" jdbcType="LONGVARCHAR" property="productContent"/>
    
         <!--这里还需要标明关系  property属性对应是实体类的字段名-->
            <association property="standard" resultMap="StandardResultMap"></association>
            <association property="dommodityattribute" resultMap="DommodityattributeResultMap"></association>
        </resultMap>
             
        <!--这个是要查询的表1  总之你直接去要查询的表复制就好 -->
    <resultMap id="DommodityattributeResultMap" type="com.nf147.mall.entity.Dommodityattribute">
            <id column="id" jdbcType="INTEGER" property="id"/>
            <result column="product_id" jdbcType="INTEGER" property="productId"/>
            <result column="product_simg" jdbcType="VARCHAR" property="productSimg"/>
    </resultMap>
            <!--这个是要查询的表2-->
    <resultMap id="StandardResultMap" type="com.nf147.mall.entity.Standard">
            <id column="specs_id" jdbcType="INTEGER" property="specsId"/>
            <result column="product_id" jdbcType="INTEGER" property="productId"/>
            <result column="product_specs" jdbcType="VARCHAR" property="productSpecs"/>
            <result column="product_price" jdbcType="DECIMAL" property="productPrice"/>
    </resultMap>

    实体类:

    public class Commodity {
        private Integer productId;
    
        private Integer categoryId;
    
        private String productCode;
    
        private String productName;
    
        private String productContent;
    
        private Standard standard;      //第一个表的实体类
    
        private Dommodityattribute dommodityattribute;  //第二个表的实体类  然后再提供 get 和 set 的方法
    
        public Standard getStandard() {
            return standard;
        }
    
        public void setStandard(Standard standard) {
            this.standard = standard;
        }
    
        public Dommodityattribute getDommodityattribute() {
            return dommodityattribute;
        }
    
        public void setDommodityattribute(Dommodityattribute dommodityattribute) {
            this.dommodityattribute = dommodityattribute;
        }
    
        public Integer getProductId() {
            return productId;
        }
    
        public void setProductId(Integer productId) {
            this.productId = productId;
        }
    
        public Integer getCategoryId() {
            return categoryId;
        }
    
        public void setCategoryId(Integer categoryId) {
            this.categoryId = categoryId;
        }
    
        public String getProductCode() {
            return productCode;
        }
    
        public void setProductCode(String productCode) {
            this.productCode = productCode == null ? null : productCode.trim();
        }
    
        public String getProductName() {
            return productName;
        }
    
        public void setProductName(String productName) {
            this.productName = productName == null ? null : productName.trim();
        }
    
        public String getProductContent() {
            return productContent;
        }
    
        public void setProductContent(String productContent) {
            this.productContent = productContent == null ? null : productContent.trim();
        }
    }

    希望能帮助大家,谢谢。

  • 相关阅读:
    响应式布局
    Fiddler2汉化版使用说明
    nonmember,nonfriend替换member函数
    Java回顾之Spring基础
    dudu,想在cnblogs首页看很久以前的文章不行。
    基于Nios II内核的项目程序为什么越优化越慢?
    学习 easyui:禁用 linkbutton 问题
    Socket编程 (异步通讯,解决Tcp粘包) 3
    .NET:可扩展的单据编号生成器 之 基于缓冲区的顺序号
    淘宝API应用开发
  • 原文地址:https://www.cnblogs.com/nongzihong/p/10275523.html
Copyright © 2020-2023  润新知