使用java代码实现操作数据库的创建等系列操作

创作背景:

使用代码实现对PostgreSQL 数据库的创建用户、授权用户、删除用户系列操作!

1、导入相关的依赖


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>


        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.26</version>
        </dependency>

2、具体实现

创建实体类

@Data
@ApiModel(value = "EntityDataBase", description = "实体数据库相关内容")
public class EntityDataBase implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 用户名
     */
    @ApiModelProperty("用户名")
    private String userName;

    /**
     * 用户密码
     */
    @ApiModelProperty("用户密码")
    private String userPassword;

    /**
     * 数据库名
     */
    @ApiModelProperty("数据库名")
    private String databaseName;

}

用户相关

    /**
     * 创建数据库用户
     *
     * @return {@link AjaxResult}
     */
    @ApiOperation("A-1创建数据库用户")
    @PostMapping("/createDbUser")
    public AjaxResult createDbUser(@RequestBody EntityDataBase entityDataBase) {
        return AjaxResult.success(projectRelatedService.createDbUser(entityDataBase));
    }

    /**
     * 删除数据库用户
     *
     * @return {@link AjaxResult}
     */
    @ApiOperation("A-2删除数据库用户")
    @GetMapping("/deleteDbUser")
    public AjaxResult deleteDbUser(EntityDataBase entityDataBase) {
        return AjaxResult.success(projectRelatedService.deleteDbUser(entityDataBase));
    }
    /**
     * 创建数据库用户
     *
     * @param entityDataBase
     * @return {@link String}
     */
    String createDbUser(EntityDataBase entityDataBase);

    /**
     * 删除数据库用户
     *
     * @param entityDataBase 实体数据库
     * @return {@link String}
     */
    String deleteDbUser(EntityDataBase entityDataBase);

    /**
     * 创建数据库用户
     *
     * @param entityDataBase 实体数据库
     * @return {@link String}
     */
    @Override
    public String createDbUser(EntityDataBase entityDataBase) {
        String userName = entityDataBase.getUserName();
        String password = entityDataBase.getUserPassword();
        String dbName = entityDataBase.getDatabaseName();
        // 判断用户信息不为空
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password) || StringUtils.isEmpty(dbName)) {
            return "用户名或密码不能为空";
        } else {
            projectRelatedMapper.createDbUser(userName, password);
            return "数据库用户创建成功";
        }

    }


    /**
     * 删除数据库用户
     *
     * @param entityDataBase 实体数据库
     * @return {@link String}
     */
    @Override
    public String deleteDbUser(EntityDataBase entityDataBase) {
        String userName = entityDataBase.getUserName();
        // 判断用户信息不为空
        if (StringUtils.isEmpty(userName)) {
            return "用户名不能为空";
        } else {
            projectRelatedMapper.deleteDbUser(entityDataBase.getUserName());
            return "成功删除数据库用户!";
        }
    }
    /**
     * 创建数据库用户
     *
     * @param userName 用户名
     * @param password 密码
     * @return {@link String}
     */
    void createDbUser(@Param("userName") String userName, @Param("password") String password);

    /**
     * 删除数据库用户
     *
     * @param userName 用户名
     * @return {@link String}
     */
    void deleteDbUser(@Param("userName") String userName);

    <!-- 创建数据库用户 -->
    <update id="createDbUser" parameterType="java.lang.String">
        CREATE USER ${userName} WITH PASSWORD '${password}'
    </update>

    <!-- 删除数据库用户 -->
    <update id="deleteDbUser">
        DROP USER ${userName}
    </update>

数据库相关


    /**
     * 数据库创建成功
     *
     * @return {@link AjaxResult}
     */
    @ApiOperation("B-1数据库创建成功")
    @GetMapping("/createDatabase")
    public AjaxResult createDatabase(EntityDataBase entityDataBase) {
        projectRelatedService.createDatabase(entityDataBase);
        return AjaxResult.success("数据库创建成功");
    }

    /**
     * 数据库删除成功
     *
     * @return {@link AjaxResult}
     */
    @ApiOperation("B-2数据库删除成功")
    @GetMapping("/deleteDatabase")
    public AjaxResult deleteDatabase(EntityDataBase entityDataBase) {
        projectRelatedService.deleteDatabase(entityDataBase);
        return AjaxResult.success("数据库删除成功");
    }
    /**
     * 创建数据库
     *
     * @param entityDataBase 实体数据库
     */
    void createDatabase(EntityDataBase entityDataBase);

    /**
     * 删除数据库
     *
     * @param entityDataBase 实体数据库
     */
    void deleteDatabase(EntityDataBase entityDataBase);

    /**
     * 创建数据库
     *
     * @param entityDataBase 实体数据库
     */
    @Override
    public void createDatabase(EntityDataBase entityDataBase) {
        String dbName = entityDataBase.getDatabaseName();
        // 判断用户信息不为空
        if (StringUtils.isEmpty(dbName )) {
        } else {
            projectRelatedMapper.createDatabase(entityDataBase.getDatabaseName());
        }
    }

    /**
     * 删除数据库
     *
     * @param entityDataBase 实体数据库
     */
    @Override
    public void deleteDatabase(EntityDataBase entityDataBase) {
        String dbName = entityDataBase.getDatabaseName();
        // 判断用户信息不为空
        if (StringUtils.isEmpty(dbName )) {
        } else {
            projectRelatedMapper.deleteDatabase(entityDataBase.getDatabaseName());
        }
    }

    /**
     * 创建数据库
     *
     * @param dbName 数据库名称
     * @return {@link String}
     */
    void createDatabase(@Param("dbName") String dbName);

    /**
     * 删除数据库
     *
     * @param dbName 数据库名称
     * @return {@link String}
     */
    void deleteDatabase(@Param("dbName") String dbName);
    <!--创建数据库 CREATE DATABASE #{userName} OWNER #{userName}-->
    <update id="createDatabase" >
        CREATE DATABASE ${dbName}
    </update>

    <!--删除数据库-->
    <update id="deleteDatabase" >
        DROP DATABASE ${dbName}
    </update>

授权相关

    /**
     * 授权用户操作数据库
     *
     * @return {@link AjaxResult}
     */
    @ApiOperation("0-授权用户操作数据库")
    @GetMapping("/grantAllPrivileges")
    public AjaxResult grantAllPrivileges(EntityDataBase entityDataBase) {
        projectRelatedService.grantAllPrivileges(entityDataBase);
        return AjaxResult.success("授权用户操作数据库成功");
    }

    /**
     * 撤消所有权限
     *
     * @return {@link AjaxResult}
     */
    @ApiOperation("0-撤消所有权限")
    @GetMapping("/revokeAllPrivileges")
    public AjaxResult revokeAllPrivileges(EntityDataBase entityDataBase) {
        projectRelatedService.revokeAllPrivileges(entityDataBase);
        return AjaxResult.success("撤消所有权限成功");
    }
    /**
     * 授予所有权限
     *
     * @param entityDataBase 实体数据库
     */
    void grantAllPrivileges(EntityDataBase entityDataBase);

    /**
     * 撤消所有权限
     *
     * @param entityDataBase 实体数据库
     */
    void revokeAllPrivileges(EntityDataBase entityDataBase);

    /**
     * 授予所有权限
     *
     * @param entityDataBase 实体数据库
     */
    @Override
    public void grantAllPrivileges(EntityDataBase entityDataBase) {
        String userName = entityDataBase.getUserName();
        String dbName = entityDataBase.getDatabaseName();
        // 判断用户信息不为空
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(dbName)) {
        } else {
            projectRelatedMapper.grantAllPrivileges(dbName,userName);
        }
    }

    /**
     * 撤消所有权限
     *
     * @param entityDataBase 实体数据库
     */
    @Override
    public void revokeAllPrivileges(EntityDataBase entityDataBase) {
        String userName = entityDataBase.getUserName();
        String dbName = entityDataBase.getDatabaseName();
        // 判断用户信息不为空
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(dbName)) {
        } else {
            projectRelatedMapper.revokeAllPrivileges(dbName,userName);
        }
    }
    /**
     * 授予所有权限
     *
     * @param dbName 数据库名称
     * @return {@link String}
     */
    void grantAllPrivileges(@Param("dbName") String dbName, @Param("userName") String userName);

    /**
     * 撤消所有权限
     *
     * @param dbName   数据库名称
     * @param userName 用户名
     */
    void revokeAllPrivileges(@Param("dbName") String dbName, @Param("userName") String userName);
    <!--授权用户操作数据库 -->
    <update id="grantAllPrivileges" >
        GRANT ALL PRIVILEGES ON DATABASE ${dbName} TO ${userName}
    </update>

    <!--撤销授权用户操作数据库 -->
    <update id="revokeAllPrivileges" >
        REVOKE ALL PRIVILEGES ON DATABASE ${dbName} FROM ${userName}
    </update>

注意:这个授权,也是可以授权单个权限的!

 授权 ALL PRIVILEGES 是表示所有权限,也可以授INSERT,UPDATE,DELETE等权限。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值