首頁>技術>

場景描述

一級選單和二級選單透過外來鍵關聯,在新增一級選單同時新增二級選單時,將一級選單的主鍵id值作為二級選單外來鍵id值

問題解決:

可透過mybatis在insert後透過useGeneratedKeys獲取一級選單自增主鍵,在新增二級選單時作為外來鍵id值繼續存入二級選單

Java Bean

/** * 一級列表名稱 * @author  xj */@Entity@Getter@Setter@Table(name = "tb_top_type")public class TopType extends BaseEntity implements Serializable {    /**     * 型別主鍵id     */    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @Column(name="id",unique = true)    private Long id;    /**     * 一級列表名稱     */    @Column(name = "top_type_name", unique = true)    private String topTypeName;    /**     * 一級列表建立時間     */    @Column(name = "top_type_ctime", unique = true)    private String topTypeCtime;    /**     * 關聯二級選單     */    private List<ProductType> types;}
@Entity@Getter@Setter@Table(name = "tb_type")public class ProductType extends BaseEntity implements Serializable {    /**     * 型別主鍵id     */    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    @Column(name="id",unique = true)    private Long id;    /**     * 型別名稱     */    @Column(name = "type_name", unique = true)    private String typeName;    /**     * 型別建立時間     */    @Column(name = "type_ctime", unique = true)    private String typeCtime;    /**     * 型別修改時間     */    @Column(name = "type_uptime", unique = true)    private String typeUptime;    /**     * 外來鍵id     */    @Column(name = "top_type_id", unique = true)    private Long topTypeId;    /**     * 商品集合     */    private List<Product> products;}
@Slf4j@RestController@RequestMapping("/api/topType")@Api(tags = "一級選單列表服務")public class TopTypeController extends BaseController{    @Autowired    private ITopTypeService service;        /**     * 儲存選單列表資訊     * @param topType     * @return AjaxReslut     */    @ApiOperation(value = "儲存選單列表資訊",notes = "傳入選單列表資訊")    @RequestMapping(value ="/add",method = RequestMethod.POST)    public AjaxResult add(@RequestBody TopType topType){        topType.setTopTypeCtime(DateUtils.nowTime());        return service.add(topType);    }  }

Service層:

@Service@Slf4j@Transactionalpublic class TopTypeServiceImpl implements ITopTypeService {    @Autowired    private ITopTypeMapper mapper;    @Autowired    private IProductTypeMapper typeMapper;      @Override    public AjaxResult add(Object o) {        try {            /** 一級選單*/            TopType topType = (TopType) o;            int rows = mapper.add(topType);            if (rows<0){                return AjaxResult.error("新增一級選單失敗!");            }            /** 二級選單*/            List<ProductType> types = topType.getTypes();            types.stream().forEach(                    productType->productType.setTopTypeId(topType.getId())            );            if (null != types) {                typeMapper.addList(types);            }            return AjaxResult.success("新增成功");        } catch (Exception e) {            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();            log.error("新增選單資訊失敗:" + e.getMessage());            return AjaxResult.error("新增失敗:" + e.getMessage());        }    }  }

mapper層

9
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 結合k8s和pipeline流水線,並透過k8s介面映象升級