Spring boot+Mybatis 數據庫新增 create/insert 利用api文檔調用

任務

實現數據庫的create/insert功能

相關

使用的庫:culture-center

使用的表:dept_detail

項目入口controller: DeptController

———————————————————分——界——線——————————————————

我的解決方案

明確自己應該創建的java文件有哪些

main -->

- java -->
	- Controller -->>
		- ①DeptDetailController
	- Service -->>
		- ②IDeptDetailService
		- impl -->>>
			- ③DeptDetailServiceImpl
	- DAO -->
			- ④IDeptDAO 

api -->

- model -->
	- entity -->>>
		- ⑤DeptDetail
	- param -->>>
		- ⑥NoahDeptDataSaveParam

①DeptDetailController(項目入口)

@Api(tags = "部門管理")
@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private IDeptDetailService deptDetailService;

    @ApiOperation(value = "添加部門資料")
    @RequestMapping(value = "/data/create", method = RequestMethod.POST)
    @Transactional(rollbackFor = Exception.class)
    public Long create(@Valid @RequestBody NoahDeptDataSaveParam param) {
        Long result = deptDetailService.create(param);
        return result;
    }
}

②IDeptDetailService

public interface IDeptDetailService extends IService<DeptDetail> {
    Long create(NoahDeptDataSaveParam param);
}


③DeptDetailServiceImpl

@Service
public class DeptDetailServiceImpl extends AbstractNoahServiceImpl<IDeptDetailDAO, DeptDetail> implements IDeptDetailService {
    @Override
    public Long create(NoahDeptDataSaveParam param) {
        DeptDetail deptDetail = new DeptDetail();
        deptDetail.setDeptId(param.getDeptId());
        if(!CollectionUtils.isEmpty(param.getPictureUrls())){
            deptDetail.setPictureUrls(StringUtil.list2String(param.getPictureUrls()));
        }
        boolean save = this.save(deptDetail);
        return deptDetail.getId();
    }
}

④IDeptDAO

package net.xinhuamm.noah.task.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xinhuamm.noah.permission.model.entity.Dept;
import net.xinhuamm.noah.task.model.entity.DeptDetail;
import net.xinhuamm.noah.task.model.param.NoahDeptDataEditParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @program: noah-culture-center
 * @description: 更新
 * @author: chenmin
 * @create: 2019-07-09 15:10
 **/
@Repository
public interface IDeptDAO extends BaseMapper<DeptDetail> {
}

⑤DeptDetail

package net.xinhuamm.noah.task.model.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import net.xinhuamm.noah.common.entity.LogicDelEntity;
import java.sql.Date;

@TableName("dept_detail")
@Data
public class DeptDetail extends LogicDelEntity {
   
    /**
     *
     */
    private Long deptId;
    /**
     *
     */
    private Long memberCount;
    /**
     *
     */
    private String abstracts;
    /**
     *
     */
    private String pictureUrls;
}

⑥NoahDeptDataSaveParam

@Data
public class NoahDeptDataSaveParam {
    /**
     * 部門id
     */
    @NotNull
    @ApiModelProperty(value = "部門id")
    private Long deptId;
    
    /**
     * 圖片地址
     */
    @ApiModelProperty(value = "圖片地址")
    private List<String> pictureUrls;

    /**
     * appId
     */
    @ApiModelProperty(value = "appId")
    private Long  appId;
}

測試成功啦

在這裏插入圖片描述

總結

DeptDetailServiceImpl中的 boolean save = this.save(deptDetail);
此代碼中的save雖然未被使用,但是在反工時測試數據是否被保存時很實用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章