zoukankan      html  css  js  c++  java
  • 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();
        }
    }

    希望能帮助大家,谢谢。

  • 相关阅读:
    Echarts markPoint 动态数据添加,选择性查询
    echarts timeline点击以后 蓝色的checkpoint位置不跟当前点击的节点重合
    Echarts 动态添加到map显示
    tomcat 下不在tomcat发布项目,引用外部链接
    SQL Server 2008安装
    eclipce 安装 svn插件(百度知道)
    迅为IMX6ULL开发板Linux蜂鸣器实验
    4412开发板-Android4.4典型功能相关源码修改及定制
    IMX6ULL开发平台Linux-LED实验
    迅为iTOP4418开发板运行Android7.1/Qt5.7/Ubuntu12.04系统源码开源
  • 原文地址:https://www.cnblogs.com/nongzihong/p/10275523.html
Copyright © 2011-2022 走看看