Mybatis的一对一的关联映射 插入

版权声明:本文为博主原创文章,未经博主允许不得转载! https://blog.csdn.net/qq_25560423/article/details/73936116

Mybatis的一对一的关联映射 插入

1.数据库脚本:

CREATE TABLE `product` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `product_name` varchar(50) DEFAULT NULL,
  `description` varchar(50) DEFAULT NULL,
  `add_time` bigint(19) DEFAULT NULL,
  `fixed_price` double(10,0) DEFAULT NULL,
  `dangqian_price` double(10,0) DEFAULT NULL,
  `keywords` varchar(20) DEFAULT NULL,
  `has_deleted` int(11) DEFAULT NULL,
  `product_pic` varchar(60) DEFAULT NULL,

  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=141 DEFAULT CHARSET=utf8;
CREATE TABLE `food` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `producer` varchar(50) DEFAULT NULL COMMENT '生产商',
  `produce_date` varchar(50) DEFAULT NULL COMMENT '生产日期',
  `expiration_date` int(20) DEFAULT NULL COMMENT '保质期',
  `storge` int(10) DEFAULT NULL COMMENT '库存',
  `category` varchar(20) DEFAULT NULL COMMENT '零食分类',
  `specification` varchar(50) DEFAULT NULL COMMENT '容量,大、中、小,毫升 之类的',
  `product_id` int(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=110 DEFAULT CHARSET=utf8;


--food中,product_id为外键 参照product

2.创建实体类(Pojo) :Product

/**
 * 商品类
 */
public class Product {

    private int id;
    private String product_name;
    private String description;
    private String add_time; //添加商品的时间
    private double fixed_price; //商品打折价格
    private double dangqian_price;//商品当前价格
    private String keywords;//商品关键词
    private int has_deleted;//是否被删除 标志位
    private String product_pic;//商品图片地址

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getProduct_name() {
        return product_name;
    }
    public void setProduct_name(String productName) {
        product_name = productName;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public String getAdd_time() {
        return add_time;
    }
    public void setAdd_time(String addTime) {
        add_time = addTime;
    }
    public double getFixed_price() {
        return fixed_price;
    }
    public void setFixed_price(double fixedPrice) {
        fixed_price = fixedPrice;
    }
    public double getDangqian_price() {
        return dangqian_price;
    }
    public void setDangqian_price(double dangqianPrice) {
        dangqian_price = dangqianPrice;
    }
    public String getKeywords() {
        return keywords;
    }
    public void setKeywords(String keywords) {
        this.keywords = keywords;
    }
    public int getHas_deleted() {
        return has_deleted;
    }
    public void setHas_deleted(int hasDeleted) {
        has_deleted = hasDeleted;
    }
    public String getProduct_pic() {
        return product_pic;
    }
    public void setProduct_pic(String productPic) {
        product_pic = productPic;
    }
}

Food

//商品详情类
public class Food {
    private int id;
    private String producer;//生产商
    private String produce_date; //生产日期
    private int expiration_date;//保质期
    private int storge;//库存
    private String category;//分类
    private String specification;//规格
    private int product_id;   //商品id

    public int getProduct_id() {
        return product_id;
    }
    public void setProduct_id(int productId) {
        product_id = productId;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getProducer() {
        return producer;
    }
    public void setProducer(String producer) {
        this.producer = producer;
    }
    public String getProduce_date() {
        return produce_date;
    }
    public void setProduce_date(String produceDate) {
        produce_date = produceDate;
    }
    public int getExpiration_date() {
        return expiration_date;
    }
    public void setExpiration_date(int expirationDate) {
        expiration_date = expirationDate;
    }
    public int getStorge() {
        return storge;
    }
    public void setStorge(int storge) {
        this.storge = storge;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }
    public String getSpecification() {
        return specification;
    }
    public void setSpecification(String specification) {
        this.specification = specification;
    }
}

3.创建Mapper接口:
ProductDao

public interface ProductDao{ 
  public void addProduct(Product product);  //上架商品
}

FoodDao

public interface FoodDao{ 
  public void addFood(Food food);//
} 

4.创建两个XML文件:
ProductMapper.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.ibatis.onetoone.mapper.ProductDao"> 
 <parameterMap type="cn.edu.nwsuaf.entity.Product" id="parameterProductMap">
        <parameter property="id" />
        <parameter property="product_name" />
        <parameter property="description" />
        <parameter property="fixed_price" />
        <parameter property="dangqian_price" />
        <parameter property="keywords" />
        <parameter property="product_pic" />
    </parameterMap>
    <insert id="addProduct" parameterMap="parameterProductMap">
        <!-- 获取刚插入的product的自动生成的主键id;(用来插入food表的product_id)-->
        <selectKey keyProperty="id" resultType="int" order="AFTER">
            SELECT LAST_INSERT_ID() AS ID 
    </selectKey>
        insert into product (product_name,description,add_time
        ,fixed_price,dangqian_price,keywords,product_pic)
        values(#{product_name},#{description},#{add_time},#{fixed_price},#{dangqian_price},#{keywords},#{product_pic})
    </insert>
</mapper>    

FoodMapper.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.ibatis.onetoone.mapper.FoodDao"> 
     <parameterMap type="cn.edu.nwsuaf.entity.Food" id="foodMap">
        <parameter property="id" />
        <parameter property="producer" />
        <parameter property="produce_date" />
        <parameter property="expiration_date" />
        <parameter property="storge" />
        <parameter property="category" />
        <parameter property="specification" />
        <parameter property="product_id" />

    </parameterMap>

    <insert id="addFood" parameterMap="foodMap">
        insert into food (producer,produce_date,expiration_date
        ,storge,category,specification,product_id)
        values(#{producer},#{produce_date},#{expiration_date},#{storge},#{category},#{specification},#{product_id})
    </insert>          
</mapper> 

加入Junit做测试:

public class Test { 
  。。。。
  @Before 
  public void before() { 
  。。。。。
  } 

  @Test 
  public void testAddP() throws SQLException { 

        ProductDao productDao = ctx.getBean(ProductDao.class);
        FoodDao foodDao=ctx.getBean(FoodDao.class);

        Product product=new Product();
        product.setProduct_name(product_name);
        product.setDescription(description);
        product.setAdd_time(GetTimeUtil.getFormatDateProduct());
        product.setFixed_price(Float.parseFloat(fixed_price));
        product.setDangqian_price(Float.parseFloat(dangqian_price));
        product.setKeywords(keywords);
        product.setProduct_pic("");
        productDao.addProduct(product);

        //int product_id=productDao.findProductIdByName(product_name);
        Food food=new Food();
        //System.out.println(product.getId());
        food.setProduct_id(product.getId()); //插入product表的主键id
        food.setProducer(producer);
        food.setProduce_date(produce_date);
        food.setExpiration_date(Integer.parseInt(expiration_date));
        food.setSpecification(specification+"g");
        food.setStorge(Integer.parseInt(storge));
        food.setCategory(keywords);
        foodDao.addFood(food);
} 
}

猜你喜欢

转载自blog.csdn.net/qq_25560423/article/details/73936116
今日推荐