基於 lambda表達式的兩表插入(List集合)
封裝的DTO
/**
* @author 宮崎不駿
* @className MallManagementCategoryDTO
* @Version 1.0
* @Description: TODO
* @date 2020/1/1415:37
*/
@Data
public class MallInfoDTO extends InputObject {
/**
* 商家ID
*/
private Integer mallId;
/**
* userID
*/
private Long userId;
/**
* 店鋪名稱
*/
private String mallName;
/**
* 成立日期(開店時間)
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date openTime;
/**
* 店鋪簡介
*/
private String mallIntroduction;
/**
* 聯繫人姓名
*/
private String contactName;
/**
* 手機號
*/
private String telephoneNumber;
/**
* 郵箱
*/
private String email;
/**
* 營業執照電子版地址1
*/
private String businessLicenseUrl1;
/**
* 營業執照電子版地址2
*/
private String businessLicenseUrl2;
/**
* 公司名稱
*/
private String companyName;
/**
* 營業執照註冊號
*/
private String businessLicenseNumber;
/**
* 營業執照所在地省份
*/
private String businessLicenseProvince;
/**
* 營業執照所在地 市
*/
private String businessLicenseCity;
/**
* 營業執照所在地 區
*/
private String businessLicenseDistrict;
/**
* 營業執照詳細地址
*/
private String businessLicenseAddress;
/**
* 成立日期
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date dateOfEstablishment;
/**
* 營業期限開始時間
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date businessTermBegin;
/**
* 營業期限結束時間
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date businessTermEnd;
/**
* 註冊資金/0-999999999 單位萬元
*/
private Integer registeredCapital;
/**
* 經營範圍
*/
private String businessScope;
/**
* 法定代表人證件類型/1身份證(mybatis中寫死)
*/
private Integer legalRepresentativeLicenseType;
/**
* 法定代表人證件電子版地址1
*/
private String legalRepresentativeLicenseUrl1;
/**
* 法定代表人證件電子版地址2
*/
private String legalRepresentativeLicenseUrl2;
/**
* 法定代表人姓名
*/
private String legalRepresentativeName;
/**
* 法定代表人證件號碼
*/
private String legalRepresentativeLicenseNumber;
/**
* 法定代表人證件有效期開始時間
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date legalRepresentativeLicenseTermBegin;
/**
* 法定代表人證件有效期結束時間/長期默認爲2099-12-31
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date legalRepresentativeLicenseTermEnd;
/**
* 銀行開戶許可證電子版地址
*/
private String bankAccountUrl;
/**
* 組織機構代碼證電子版地址
*/
private String organizingInstitutionBarCodeUrl;
/**
* 組織機構代碼
*/
private String organizingInstitutionBarCode;
/**
* 組織機構代碼證有效期開始時間
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date organizingInstitutionBarCodeTermBegin;
/**
* 組織機構代碼證有效期結束時間/長期默認爲2099-12-31
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date organizingInstitutionBarCodeTermEnd;
/**
* 店鋪logo地址
*/
private String mallLogoUrl;
/**
* 店鋪背景圖地址
*/
private String mallLogoBackground;
private List<MallManagementCategoryDTO> mallManagementCategoryDTO;
}
/**
* @author 宮崎不駿
* @className MallManagementCategoryDTO
* @Version 1.0
* @Description: TODO
* @date 2020/1/1415:37
*/
@Data
public class MallManagementCategoryDTO extends InputObject {
/**
* 主鍵ID
*/
private Integer id;
/**
* 商家ID
*/
private Integer mallId;
/**
* 一級類目ID
*/
private Integer firstLevelId;
/**
* 一級類目名字
*/
private String firstLevelName;
/**
* 二級類目ID
*/
private Integer secondLevelId;
/**
* 二級類目名字
*/
private String secondLevelName;
/**
* 三級類目ID
*/
private Integer thirdLevelId;
/**
* 三級類目名字
*/
private String thirdLevelName;
}
MySql數據庫文件
-- ----------------------------
-- Table structure for mall_info
-- ----------------------------
DROP TABLE IF EXISTS `mall_info`;
CREATE TABLE `mall_info` (
`mall_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '商家ID',
`mall_name` varchar(64) DEFAULT NULL COMMENT '店鋪名稱',
`mall_introduction` varchar(512) DEFAULT NULL COMMENT '店鋪簡介',
`contact_name` varchar(64) DEFAULT NULL COMMENT '聯繫人姓名',
`telephone_number` varchar(32) DEFAULT NULL COMMENT '手機號',
`email` varchar(64) DEFAULT NULL COMMENT '郵箱',
`business_license_url1` varchar(255) DEFAULT NULL COMMENT '營業執照電子版地址1',
`business_license_ur2` varchar(255) DEFAULT NULL COMMENT '營業執照電子版地址2',
`company_name` varchar(64) NOT NULL COMMENT '公司名稱',
`business_license_number` varchar(64) NOT NULL COMMENT '營業執照註冊號',
`business_license_province` varchar(32) NOT NULL COMMENT '營業執照所在地省份',
`business_license_city` varchar(32) NOT NULL COMMENT '營業執照所在地 市',
`business_license_district` varchar(32) NOT NULL COMMENT '營業執照所在地 區',
`business_license_address` varchar(255) NOT NULL COMMENT '營業執照詳細地址',
`date_of_establishment` date NOT NULL COMMENT '成立日期',
`business_term_begin` date NOT NULL COMMENT '營業期限開始時間',
`business_term_end` date NOT NULL COMMENT '營業期限結束時間',
`registered_capital` int(11) NOT NULL COMMENT '註冊資金/0-999999999 單位萬元',
`business_scope` text NOT NULL COMMENT '經營範圍',
`legal_representative_license_type` tinyint(1) NOT NULL COMMENT '法定代表人證件類型/1身份證',
`legal_representative_license_number` varchar(64) NOT NULL COMMENT '法定代表人證件號碼',
`legal_representative_license_url1` varchar(255) DEFAULT NULL COMMENT '法定代表人證件電子版地址1',
`legal_representative_license_url2` varchar(255) DEFAULT NULL COMMENT '法定代表人證件電子版地址2',
`legal_representative_name` varchar(64) DEFAULT NULL COMMENT '法定代表人姓名',
`legal_representative_license_term_begin` date NOT NULL COMMENT '法定代表人證件有效期開始時間',
`legal_representative_license_term_end` date NOT NULL COMMENT '法定代表人證件有效期結束時間/長期默認爲2099-12-31',
`bank_account_url` varchar(255) DEFAULT NULL COMMENT '銀行開戶許可證電子版地址',
`organizing_institution_bar_code_url` varchar(255) NOT NULL COMMENT '組織機構代碼證電子版地址',
`organizing_institution_bar_code` varchar(64) NOT NULL COMMENT '組織機構代碼',
`organizing_institution_bar_code_term_begin` date NOT NULL COMMENT '組織機構代碼證有效期開始時間',
`organizing_institution_bar_code_term_end` date NOT NULL COMMENT '組織機構代碼證有效期結束時間/長期默認爲2099-12-31',
`mall_logo_url` varchar(255) DEFAULT NULL COMMENT '店鋪logo地址',
`mall_logo_background` varchar(255) DEFAULT NULL COMMENT '店鋪logo地址',
`online_customerservice_status` char(1) DEFAULT NULL COMMENT '在線客服狀態:0關閉,1開啓',
`telephone_customerservice_status` char(1) DEFAULT NULL COMMENT '電話客服狀態:0關閉,1開啓',
`customerservice_telephone1` varchar(20) DEFAULT NULL COMMENT '電話客服號碼1',
`customerservice_telephone2` varchar(20) DEFAULT NULL COMMENT '電話客服號碼2',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間',
PRIMARY KEY (`mall_id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='商家基本信息表';
-- ----------------------------
-- Table structure for mall_management_category
-- ----------------------------
DROP TABLE IF EXISTS `mall_management_category`;
CREATE TABLE `mall_management_category` (
`id` int(10) NOT NULL AUTO_INCREMENT COMMENT '主鍵id',
`mall_id` int(10) unsigned NOT NULL COMMENT '商家ID',
`first_level_id` int(10) DEFAULT NULL COMMENT '一級類目ID',
`first_level_name` varchar(64) DEFAULT NULL COMMENT '一級類目名字',
`second_level_id` int(10) DEFAULT NULL COMMENT '二級類目ID',
`second_level_name` varchar(64) DEFAULT NULL COMMENT '二級類目名字',
`third_level_id` int(10) DEFAULT NULL COMMENT '三級類目ID',
`third_level_name` varchar(64) DEFAULT NULL COMMENT '三級類目名字',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創建時間',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時間',
PRIMARY KEY (`id`),
KEY `ind_mallmanagementcategory_mallid` (`mall_id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8 COMMENT='商家經營類目表';
dao層
/**
* @Description 向商家基本信息表插入信息
* @Retuen
* @Param
**/
int saveMallInfo(List<MallInfoDTO> mallInfoDTO);
/**
* @Description 向商家經營類目表插入信息
* @Retuen
* @Param
**/
int saveMallManagementCategory(List<MallManagementCategoryDTO> mallManagementCategoryDTO);
Mapper.xml
<insert id="saveMallInfo" parameterType="com.dy.mallmanagement.pojo.dto.MallInfoDTO" useGeneratedKeys="true"
keyProperty="mallId">
INSERT INTO mall_info (
user_id,
mall_name,
open_time,
mall_introduction,
contact_name,
telephone_number,
email,
business_license_url1,
business_license_url2,
company_name,
business_license_number,
business_license_province,
business_license_city,
business_license_district,
business_license_address,
date_of_establishment,
business_term_begin,
business_term_end,
registered_capital,
business_scope,
legal_representative_license_type,
legal_representative_license_url1,
legal_representative_license_url2,
legal_representative_name,
legal_representative_license_number,
legal_representative_license_term_begin,
legal_representative_license_term_end,
bank_account_url,
organizing_institution_bar_code_url,
organizing_institution_bar_code,
organizing_institution_bar_code_term_begin,
organizing_institution_bar_code_term_end,
mall_logo_url,
mall_logo_background
) VALUES
<foreach collection="list" separator="," item="item">
(
#{item.userId},
#{item.mallName},
#{item.openTime},
#{item.mallIntroduction},
#{item.contactName},
#{item.telephoneNumber},
#{item.email},
#{item.businessLicenseUrl1},
#{item.businessLicenseUrl2},
#{item.companyName},
#{item.businessLicenseNumber},
#{item.businessLicenseProvince},
#{item.businessLicenseCity},
#{item.businessLicenseDistrict},
#{item.businessLicenseAddress},
#{item.dateOfEstablishment},
#{item.businessTermBegin},
#{item.businessTermEnd},
#{item.registeredCapital},
#{item.businessScope},
#{item.legalRepresentativeLicenseType},
#{item.legalRepresentativeLicenseUrl1},
#{item.legalRepresentativeLicenseUrl2},
#{item.legalRepresentativeName},
#{item.legalRepresentativeLicenseNumber},
#{item.legalRepresentativeLicenseTermBegin},
#{item.legalRepresentativeLicenseTermEnd},
#{item.bankAccountUrl},
#{item.organizingInstitutionBarCodeUrl},
#{item.organizingInstitutionBarCode},
#{item.organizingInstitutionBarCodeTermBegin},
#{item.organizingInstitutionBarCodeTermEnd},
#{item.mallLogoUrl},
#{item.mallLogoBackground})
</foreach>
</insert>
<insert id="saveMallManagementCategory" parameterType="com.dy.mallmanagement.pojo.dto.MallInfoDTO">
INSERT INTO mall_management_category (
mall_id,
first_level_id,
first_level_name,
second_level_id,
second_level_name,
third_level_id,
third_level_name) values
<foreach collection="list" item="item" separator="," >
(
#{item.mallId},
#{item.firstLevelId},
#{item.firstLevelName},
#{item.secondLevelId},
#{item.secondLevelName},
#{item.thirdLevelId},
#{item.thirdLevelName})
</foreach>
</insert>
Service層
/**
* @Description 向商家基本信息表插入信息
* @Retuen
* @Param
**/
OutputObject saveMallInfo(List<MallInfoDTO> mallInfoDTO);
ServiceImpl層
/**
* @author 宮崎不駿
* @className MallInfoServiceImpl
* @Version 1.0
* @Description: TODO
* @date 2020/1/1420:26
*/
@Service
public class MallInfoServiceImpl implements MallInfoService {
@Autowired
private MallInfoMapper mallInfoMapper;
/**
* @Description 向商家基本信息表插入信息
* @Retuen
* @Param
**/
@Override
@Transactional(rollbackFor = Exception.class)
public OutputObject saveMallInfo(List<MallInfoDTO> mallInfoDTO) {
try{
//批量添加商家基本信息表信息
int i = mallInfoMapper.saveMallInfo(mallInfoDTO);
//將商家經營類目表抽離出來
List<MallManagementCategoryDTO> mallManagementCategoryDTO = mallInfoDTO.stream()
.flatMap(o -> o.getMallManagementCategoryDTO().stream().peek(s ->{
s.setMallId(o.getMallId());
})).collect(Collectors.toList());
if (i > 0 || mallManagementCategoryDTO.size()>0){
mallInfoMapper.saveMallManagementCategory(mallManagementCategoryDTO);
return new OutputObject(ReturnCode.SUCCESS,"添加成功","添加成功");
}else {
return new OutputObject(ReturnCode.FAIL,"添加失敗",mallInfoDTO);
}
}catch (Exception e){
// 事務回滾
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
return new OutputObject(ReturnCode.FAIL,"添加失敗",e.getMessage());
}
}
}
Controller層
/**
* @author 宮崎不駿
* @className MallInfoController
* @Version 1.0
* @Description: TODO
* @date 2020/1/1420:54
*/
@Controller
@RequestMapping("/api/mallInfo/")
public class MallInfoController {
private static final Logger LOGGER = LoggerFactory.getLogger(MallInfoController.class);
@Autowired
private MallInfoService mallInfoService;
@RequestMapping("/saveMallInfo")
@ResponseBody
public OutputObject saveMallInfo(@RequestBody List<MallInfoDTO> mallInfoDTO){
LOGGER.info("com.dy.mallmanagement.MallInfoController.saveMallInfo.向商家基本信息表插入信息 mallInfoDTO{}",mallInfoDTO);
return mallInfoService.saveMallInfo(mallInfoDTO);
}
}
postman測試
[
{
"userId": 11,
"mallName": "解憂雜貨鋪",
"openTime": "2020-01-01 12:12:12",
"mallIntroduction": "幫你解憂",
"contactName": "宮崎駿",
"telephoneNumber": "13838384438",
"email": "[email protected]",
"businessLicenseUrl1": "F:/photo/1.jpg",
"businessLicenseUrl2": "F:/photo/2.jpg",
"companyName": "解憂集團",
"businessLicenseNumber": "123456654321",
"businessLicenseProvince": "河南省",
"businessLicenseCity": "鄭州市",
"businessLicenseDistrict": "金水區",
"businessLicenseAddress": "坑你大廈18樓",
"dateOfEstablishment": "2020-01-01 12:12:13",
"businessTermBegin": "2020-01-01 12:12:13",
"businessTermEnd": "2220-01-01 12:12:13",
"registeredCapital": 100000000,
"businessScope": "智慧",
"legalRepresentativeLicenseType": 1,
"legalRepresentativeLicenseUrl1": "F:/photo/3.jpg",
"legalRepresentativeLicenseUrl2": "F:/photo/4.jpg",
"legalRepresentativeName": "張三丰",
"legalRepresentativeLicenseNumber": "12345670987654",
"legalRepresentativeLicenseTermBegin": "2020-01-01 12:12:13",
"legalRepresentativeLicenseTermEnd": "2220-01-01 12:12:13",
"bankAccountUrl": "F:/photo/5.jpg",
"organizingInstitutionBarCodeUrl": "F:/photo/6.jpg",
"organizingInstitutionBarCode": "123ertyu",
"organizingInstitutionBarCodeTermBegin": "2020-01-01 12:12:13",
"organizingInstitutionBarCodeTermEnd": "2220-01-01 12:12:13",
"mallLogoUrl": "F:/photo/7.jpg",
"mallLogoBackground": "F:/photo/8.jpg",
"mallManagementCategoryDTO": [
{
"firstLevelId": 1,
"firstLevelName": "一級分類",
"secondLevelId": 1,
"secondLevelName": "二級分類",
"thirdLevelId": 1,
"thirdLevelName": "三級分類"
}
]
},
{
"userId": 12,
"mallName": "解憂雜貨鋪",
"openTime": "2020-01-01 12:12:12",
"mallIntroduction": "幫你解憂",
"contactName": "宮崎駿",
"telephoneNumber": "13838384438",
"email": "[email protected]",
"businessLicenseUrl1": "F:/photo/1.jpg",
"businessLicenseUrl2": "F:/photo/2.jpg",
"companyName": "解憂集團",
"businessLicenseNumber": "123456654321",
"businessLicenseProvince": "河南省",
"businessLicenseCity": "鄭州市",
"businessLicenseDistrict": "金水區",
"businessLicenseAddress": "坑你大廈18樓",
"dateOfEstablishment": "2020-01-01 12:12:13",
"businessTermBegin": "2020-01-01 12:12:13",
"businessTermEnd": "2220-01-01 12:12:13",
"registeredCapital": 100000000,
"businessScope": "智慧",
"legalRepresentativeLicenseType": 1,
"legalRepresentativeLicenseUrl1": "F:/photo/3.jpg",
"legalRepresentativeLicenseUrl2": "F:/photo/4.jpg",
"legalRepresentativeName": "張三丰",
"legalRepresentativeLicenseNumber": "12345670987654",
"legalRepresentativeLicenseTermBegin": "2020-01-01 12:12:13",
"legalRepresentativeLicenseTermEnd": "2220-01-01 12:12:13",
"bankAccountUrl": "F:/photo/5.jpg",
"organizingInstitutionBarCodeUrl": "F:/photo/6.jpg",
"organizingInstitutionBarCode": "123ertyu",
"organizingInstitutionBarCodeTermBegin": "2020-01-01 12:12:13",
"organizingInstitutionBarCodeTermEnd": "2220-01-01 12:12:13",
"mallLogoUrl": "F:/photo/7.jpg",
"mallLogoBackground": "F:/photo/8.jpg",
"mallManagementCategoryDTO": [
{
"firstLevelId": 1,
"firstLevelName": "一級分類",
"secondLevelId": 1,
"secondLevelName": "二級分類",
"thirdLevelId": 1,
"thirdLevelName": "三級分類"
}
]
}
]
mysql數據庫