用户表增删改查

这是一个关于字典用户管理服务的RESTful API设计示例,包括添加、更新、删除用户和获取用户列表的接口。每个操作都使用了DustMapping注解进行映射,并通过Swagger进行API文档说明,详细描述了请求参数、响应状态码和错误信息。
摘要由CSDN通过智能技术生成

Resource:

@RestController
@Api(description = "字典用户管理服务")
@DustMapping(value = "/dm", method = RequestMethod.POST)
public class AddUserResource {

    @Autowired
    DictManageService dms;




    @ApiOperation(value = "更新应用",
            consumes = "application/x-www-form-urlencoded", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "数据对象(JSON)", required = true, dataType = "json",
                    paramType = "form", example = "{}")
    })
    @ApiResponses({
            @ApiResponse(code = 200,
                    message = "| 错误码   | 释义  |\n" +
                            "| - | - |\n" +
                            "| SUCCESS  | 成功 |\n" +
                            "| DB_ERR | 数据库异常 |\n")
    })
    @DustMapping("/user/updateUser")
    public Object updateUserInfo(String data) throws SQLException, DustMsException, BuzException {
        checkNotNull(data);
        JSONObject row = JSON.parseObject(data);
        row.put("$rowState", RowState.MODIFIED);
        return dms.save("user" , row);
    }


    @ApiOperation(value = "删除用户",
            consumes = "application/x-www-form-urlencoded", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户ID", required = true, dataType = "int",
                    paramType = "form", example = "{}")
    })
    @ApiResponses({
            @ApiResponse(code = 200,
                    message = "| 错误码   | 释义  |\n" +
                            "| - | - |\n" +
                            "| SUCCESS  | 成功 |\n" +
                            "| DB_ERR | 数据库异常 |\n")
    })
    @DustMapping("/user/deleteuser")
    public Object deleteUserInfo(String uid) throws SQLException, DustMsException, BuzException {
      //  checkNotNull(id);
        return dms.delete("user", uid);
    }

    @ApiOperation(value = "获取用户列表",
            consumes = "application/x-www-form-urlencoded", produces = "application/json")
    @ApiResponses({
            @ApiResponse(code = 200,
                    message = "| 错误码   | 释义  |\n" +
                            "| - | - |\n" +
                            "| SUCCESS  | 成功 |\n" +
                            "| DB_ERR | 数据库异常 |\n")
    })
    @DustMapping("/user/userlist")
    public Object listUser() throws SQLException, DustMsException, BuzException {
        return dms.list("user", null);
    }

    @ApiOperation(value = "新增用户",
            consumes = "application/x-www-form-urlencoded", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "data", value = "数据对象(JSON)", required = true, dataType = "json",
                    paramType = "form", example = "{}")
    })
    @ApiResponses({
            @ApiResponse(code = 200,
                    message = "| 错误码   | 释义  |\n" +
                            "| - | - |\n" +
                            "| SUCCESS  | 成功 |\n" +
                            "| DB_ERR | 数据库异常 |\n")
    })
    @DustMapping("/app/addUser")
    public Object addUserInfo(String data) throws SQLException, DustMsException, BuzException {
        checkNotNull(data);
        JSONObject row = JSON.parseObject(data);
        row.put("$rowState", RowState.ADDED);
        return dms.save("user", row);
    }



}

Serivice:

@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
@EnableConfigurationProperties(DmProperties.class)
public class DictManageService extends BaseService {

    @Autowired
    DbAdapterManager dbAdapterManager;

    @Autowired
    DmProperties dmProperties;

    private ISqlAdapter getDictSqlAdapter() throws SQLException {
        ISqlAdapter sqlAdapter;
        if (StringUtils.isNotEmpty(dmProperties.getDataSourceName())) {
            sqlAdapter = dbAdapterManager.getAdapter(dmProperties.getDataSourceName());
        } else {
            sqlAdapter = DictGlobalConfig.getSqlAdapter();
        }

        if (sqlAdapter == null) {
            throw new SQLException("DB not found");
        }
        return sqlAdapter;
    }

    public DataObj list(String alias, Condition condition) throws SQLException, BuzException, DustMsException {
        ISqlAdapter sqlAdapter = getDictSqlAdapter();
        DataObj obj = DataObjBuilder.create(com.hisense.dm.VERSION.APP_ID, "basic", alias);
        if (condition != null) {
            obj.addCondition(condition);
        }
        obj.search(sqlAdapter);
        sqlAdapter.commitAndCloseQuiet();
        return obj;
    }

    public DataObjRow save(String alias, JSONObject data) throws SQLException, BuzException, DustMsException {
        ISqlAdapter sqlAdapter = getDictSqlAdapter();
        DataObj obj = DataObjBuilder.create(com.hisense.dm.VERSION.APP_ID, "basic", alias);
        json2Row(data, obj);
        obj.validate();
        RowState rowState = obj.moveTo(0).getRowState();
        obj.save(sqlAdapter);
        sqlAdapter.commit();

        String id = Converter.toString(obj.moveTo(0).getValue("id"));
        obj.addCondition(new Condition(obj.getTableName(), "id", id));
        obj.search(sqlAdapter);
        return obj.moveTo(0);
    }

    public boolean delete(String alias, String id) throws SQLException {
        ISqlAdapter sqlAdapter = getDictSqlAdapter();
        DataObj obj = DataObjBuilder.create(com.hisense.dm.VERSION.APP_ID, "basic", alias);
        SqlCommand cmd = new SqlCommand("DELETE FROM " + obj.getTableName());
        cmd.appendWhere("id=:id");
        cmd.setParameter("id", id);
        sqlAdapter.update(cmd);
        sqlAdapter.commitAndCloseQuiet();
        return true;
    }

    public boolean deleteObj(String id) throws SQLException {
        ISqlAdapter sqlAdapter = getDictSqlAdapter();

        //dataobj
        SqlCommand deleteObj = new SqlCommand("DELETE FROM " + "dataobj");
        deleteObj.appendWhere("id=:id");
        deleteObj.setParameter("id", id);
        sqlAdapter.update(deleteObj);

        //dataobj_column
        SqlCommand deleteColumn = new SqlCommand("DELETE FROM " + "dataobj_column");
        deleteColumn.appendWhere("master_id=:id");
        deleteColumn.setParameter("id", id);
        sqlAdapter.update(deleteColumn);


        //dataobj_table
        SqlCommand deleteTable = new SqlCommand("DELETE FROM " + "dataobj_table");
        deleteTable.appendWhere("master_id=:id");
        deleteTable.setParameter("id", id);
        sqlAdapter.update(deleteTable);

        sqlAdapter.commitAndCloseQuiet();
        return true;
    }

    public String getTableScript(String id, String dbType) {
        DataObj obj = DataObjBuilder.create(Converter.toLong(id));
        return obj.toTableScript(dbType);
    }

    public JSONObject getSchemaJSON(String id) {
        DataObj obj = DataObjBuilder.create(Converter.toLong(id));
        return obj.toSchemaJson();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值