Sharding-JDBC 从入门到精通(9)- 综合案例(二)添加商品
一、Sharding-JDBC 综合案例-添加商品-dao
1、在 shopping 子工程(子模块)中,创建 dao 接口类 ProductDao.java
/*** dbsharding\shopping\src\main\java\djh\it\shopping\dao\ProductDao.java** 2024-7-3 创建 dao 接口类 ProductDao.java*/
package djh.it.shopping.dao;import djh.it.shopping.entity.ProductDescript;
import djh.it.shopping.entity.ProductInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;import java.util.List;
import java.util.Map;@Mapper
@Component
public interface ProductDao {//添加商品基本信息@Insert("insert into product_info(store_info_id, product_name, spec, region_code, price) " +" values(#{storeInfoId},#{productName},#{spec},#{regionCode},#{price})")@Options(useGeneratedKeys = true, keyProperty = "productInfoId", keyColumn = "product_info_id")int insertProductInfo( ProductInfo productInfo );//添加商品描述信息@Insert("insert into product_descript(product_info_id, descript, store_info_id) " +" values(#{productInfoId}, #{descript}, #{storeInfoId})")@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")int insertProductDescript( ProductDescript productDescript );
}
二、Sharding-JDBC 综合案例-添加商品-service
1、在 shopping 子工程(子模块)中,创建 service 接口类 ProductService.java
/*** dbsharding\shopping\src\main\java\djh\it\shopping\service\ProductService.java** 2024-7-3 创建 service 接口类 ProductService.java*/
package djh.it.shopping.service;import djh.it.shopping.entity.ProductInfo;public interface ProductService {//添加商品public void createProduct( ProductInfo productInfo );
}
2、在 shopping 子工程(子模块)中,创建 service 实现类 ProductServiceImpl.java
/*** dbsharding\shopping\src\main\java\djh\it\shopping\service\impl\ProductServiceImpl.java** 2024-7-3 创建 service 实现类 ProductServiceImpl.java*/
package djh.it.shopping.service.impl;import djh.it.shopping.dao.ProductDao;
import djh.it.shopping.entity.ProductDescript;
import djh.it.shopping.entity.ProductInfo;
import djh.it.shopping.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;@Service
public class ProductServiceImpl implements ProductService {@AutowiredProductDao productDao;@Override@Transactionalpublic void createProduct( ProductInfo productInfo ) {ProductDescript productDescript = new ProductDescript();//设置商品描述信息productDescript.setDescript(productInfo.getDescript());//调用dao 向商品信息表productDao.insertProductInfo(productInfo);//将商品信息id设置到 productDescript
// productDescript.setId(productInfo.getProductInfoId()); //errorproductDescript.setProductInfoId(productInfo.getProductInfoId());//设置店铺idproductDescript.setStoreInfoId(productInfo.getStoreInfoId());//向商品描述信息表插入数据productDao.insertProductDescript(productDescript);}
}
三、Sharding-JDBC 综合案例-添加商品-测试
1、在 shopping 子工程(子模块)中,创建 测试类 ShardingTest.java
/*** dbsharding\shopping\src\test\java\djh\it\shopping\ShardingTest.java** 2024-7-3 创建 测试类 ShardingTest.java*/
package djh.it.shopping;import djh.it.shopping.entity.ProductInfo;
import djh.it.shopping.service.ProductService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import java.math.BigDecimal;@RunWith(SpringRunner.class)
@SpringBootTest(classes = ShoppingBootstrap.class)
public class ShardingTest {@AutowiredProductService productService;@Test //添加商品public void testCreateProduct(){ProductInfo productInfo = new ProductInfo();productInfo.setStoreInfoId(1L); //店铺id(向 product_db_2 中插入数据)productInfo.setProductName("Java编程思想"); //商品名称productInfo.setSpec("大号"); //商品规格productInfo.setPrice(new BigDecimal(60)); //商品价格productInfo.setRegionCode("110100"); //商品产地productInfo.setDescript("《java编程思想》这本书不错!!!"); //商品描述productService.createProduct(productInfo);}
}
上一节关联链接请点击
# Sharding-JDBC从入门到精通(8)- 综合案例(一)数据库设计搭建与分片策略配置