用户表 user                                 

Mybatis多对多高级映射

package com.dh.model;

import java.util.Date;
import java.util.List;

public class User {
    private String uid;

    private String username;

    private String password;

    private String name;

    private String email;

    private String telephone;

    private Date birthday;

    private String sex;

    private Integer state;

    private String code;
    
    private List<Orders> orders;
    
    

	public List<Orders> getOrders() {
		return orders;
	}

	public void setOrders(List<Orders> orders) {
		this.orders = orders;
	}

	public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid == null ? null : uid.trim();
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone == null ? null : telephone.trim();
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code == null ? null : code.trim();
    }
}

订单表 orders

Mybatis多对多高级映射

package com.dh.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Orders {
    private String oid;

    private Date ordertime;

    private Double total;

    private Integer state;

    private String address;

    private String name;


	private String telephone;
	
	private String uid;


     //该订单中有多少订单项
  	List<Orderitem> orderItems = new ArrayList<Orderitem>();

    public String getUid() {
		return uid;
	}

	public void setUid(String uid) {
		this.uid = uid;
	}

	private User user; 


  	
    public User getUser() {
    	return user;
    }
    
    public void setUser(User user) {
    	this.user = user;
    }
  	
    public List<Orderitem> getOrderItems() {
		return orderItems;
	}

	public void setOrderItems(List<Orderitem> orderItems) {
		this.orderItems = orderItems;
	}

	public String getOid() {
        return oid;
    }

    public void setOid(String oid) {
        this.oid = oid == null ? null : oid.trim();
    }

    public Date getOrdertime() {
        return ordertime;
    }

    public void setOrdertime(Date ordertime) {
        this.ordertime = ordertime;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address == null ? null : address.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone == null ? null : telephone.trim();
    }

    
}

订单明细表 orderitem

Mybatis多对多高级映射

package com.dh.model;

public class Orderitem {
    private String itemid;

    private Integer count;

    private Double subtotal;

    private Product product;
    
    private String pid;

    private String oid;
    
    
    public String getOid() {
		return oid;
	}

	public void setOid(String oid) {
		this.oid = oid;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}


    public String getItemid() {
        return itemid;
    }

    public void setItemid(String itemid) {
        this.itemid = itemid == null ? null : itemid.trim();
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Double getSubtotal() {
        return subtotal;
    }

    public void setSubtotal(Double subtotal) {
        this.subtotal = subtotal;
    }

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}



   
}

种类表 category

Mybatis多对多高级映射

package com.dh.model;

public class Category {
    private String cid;

    private String cname;

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid == null ? null : cid.trim();
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname == null ? null : cname.trim();
    }
}

商品表 product

Mybatis多对多高级映射

package com.dh.model;

import java.util.Date;

public class Product {
    private String pid;

    private String pname;

    private Double marketPrice;

    private Double shopPrice;

    private String pimage;

    private Date pdate;

    private Integer isHot;

    private String pdesc;

    private Integer pflag;

    private String cid;

    public String getPid() {
        return pid;
    }

    public void setPid(String pid) {
        this.pid = pid == null ? null : pid.trim();
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname == null ? null : pname.trim();
    }

    public Double getMarketPrice() {
        return marketPrice;
    }

    public void setMarketPrice(Double marketPrice) {
        this.marketPrice = marketPrice;
    }

    public Double getShopPrice() {
        return shopPrice;
    }

    public void setShopPrice(Double shopPrice) {
        this.shopPrice = shopPrice;
    }

    public String getPimage() {
        return pimage;
    }

    public void setPimage(String pimage) {
        this.pimage = pimage == null ? null : pimage.trim();
    }

    public Date getPdate() {
        return pdate;
    }

    public void setPdate(Date pdate) {
        this.pdate = pdate;
    }

    public Integer getIsHot() {
        return isHot;
    }

    public void setIsHot(Integer isHot) {
        this.isHot = isHot;
    }

    public String getPdesc() {
        return pdesc;
    }

    public void setPdesc(String pdesc) {
        this.pdesc = pdesc == null ? null : pdesc.trim();
    }

    public Integer getPflag() {
        return pflag;
    }

    public void setPflag(Integer pflag) {
        this.pflag = pflag;
    }

    public String getCid() {
        return cid;
    }

    public void setCid(String cid) {
        this.cid = cid == null ? null : cid.trim();
    }
}

利用resultMap实现多对多映射

  			<!-- 多对多查询 -->
  	<resultMap id="getMyOrder" type="com.dh.model.User">
  			<!-- 用户信息 -->
	    <id column="uid" jdbcType="VARCHAR" property="uid" />
	    <result column="username" jdbcType="VARCHAR" property="username" />
	    <result column="password" jdbcType="VARCHAR" property="password" />
	    <result column="name" jdbcType="VARCHAR" property="name" />
	    <result column="email" jdbcType="VARCHAR" property="email" />
	    <result column="telephone" jdbcType="VARCHAR" property="telephone" />
	    <result column="birthday" jdbcType="DATE" property="birthday" />
	    <result column="sex" jdbcType="VARCHAR" property="sex" />
	    <result column="state" jdbcType="INTEGER" property="state" />
	    <result column="code" jdbcType="VARCHAR" property="code" />
    
    		<!-- 订单信息 -->
    	<collection property="orders" ofType="com.dh.model.Orders">
    		<id column="oid" jdbcType="VARCHAR" property="oid" />
		    <result column="ordertime" jdbcType="TIMESTAMP" property="ordertime" />
		    <result column="total" jdbcType="DOUBLE" property="total" />
		    <result column="state" jdbcType="INTEGER" property="state" />
		    <result column="address" jdbcType="VARCHAR" property="address" />
		    <result column="name" jdbcType="VARCHAR" property="name" />
		    <result column="telephone" jdbcType="VARCHAR" property="telephone" />
		    <result column="uid" jdbcType="VARCHAR" property="uid" />
    	
    			<!-- 订单明细 -->
    		<collection property="orderItems" ofType="com.dh.model.Orderitem">
    			<id column="itemid" jdbcType="VARCHAR" property="itemid" />
			    <result column="count" jdbcType="INTEGER" property="count" />
			    <result column="subtotal" jdbcType="DOUBLE" property="subtotal" />
			    <result column="pid" jdbcType="VARCHAR" property="pid" />
			    <result column="oid" jdbcType="VARCHAR" property="oid" />
    		
    				<!-- 商品 -->
    			<association property="product" javaType="com.dh.model.Product">
    				<id column="pid" jdbcType="VARCHAR" property="pid" />
				    <result column="pname" jdbcType="VARCHAR" property="pname" />
				    <result column="market_price" jdbcType="DOUBLE" property="marketPrice" />
				    <result column="shop_price" jdbcType="DOUBLE" property="shopPrice" />
				    <result column="pimage" jdbcType="VARCHAR" property="pimage" />
				    <result column="pdate" jdbcType="DATE" property="pdate" />
				    <result column="is_hot" jdbcType="INTEGER" property="isHot" />
				    <result column="pdesc" jdbcType="VARCHAR" property="pdesc" />
				    <result column="pflag" jdbcType="INTEGER" property="pflag" />
				    <result column="cid" jdbcType="VARCHAR" property="cid" />
    			
    			</association>
    			
    		</collection>
    	
    	
    	</collection>
    	
    	
  </resultMap>
  
  
  <select id="getMyOrder" parameterType="java.lang.String" resultMap="getMyOrder">
  	select 
  		u.*,o.*,od.*,p.*
  	from 
  		user u,orders o,orderitem od,product p
  	where
  		u.uid=o.uid and o.oid=od.oid and od.pid=p.pid and u.uid=#{uid}
  
  
  </select>

 

相关文章: