SpringBoot学习之生成代码(一)

创建Db类

package org.hx.springboot_generatecode_demo10.model;

public class Db {
    private String username;
    private String password;
    private String url;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

创建RespBean类

package org.hx.springboot_generatecode_demo10.model;

import org.springframework.web.bind.annotation.ResponseBody;

public class RespBean {
    private Integer status;
    private String msg;
    private Object obj;

    public static RespBean ok(String msg,Object obj)
    {
        return new RespBean(200,msg,obj);
    }
    public static RespBean ok(String msg)
    {
        return new RespBean(200,msg,null);
    }
    public static RespBean error(String msg,Object obj)
    {
        return new RespBean(500,msg,obj);
    }
    public static RespBean error(String msg)
    {
        return new RespBean(500,msg,null);
    }
    private RespBean() {
    }

    private RespBean(Integer status, String msg, Object obj) {
        this.status = status;
        this.msg = msg;
        this.obj = obj;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }
}

创建DBUtils类

package org.hx.springboot_generatecode_demo10.utils;

import org.hx.springboot_generatecode_demo10.model.Db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
    private static Connection connection;

    public static Connection getConnection() {
        return connection;
    }
    public static Connection initDb(Db db){
        if(connection == null){
            try {
                Class.forName("com.mysql.jdbc.Driver");
                connection = DriverManager.getConnection(db.getUrl(),db.getUsername(),db.getPassword());
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }
}

创建DbController类

package org.hx.springboot_generatecode_demo10.controller;

import org.hx.springboot_generatecode_demo10.model.Db;
import org.hx.springboot_generatecode_demo10.model.RespBean;
import org.hx.springboot_generatecode_demo10.utils.DBUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Connection;

@RestController
public class DbController {
    @PostMapping("/connect")
    public RespBean connect(@RequestBody Db db){
        Connection con = DBUtils.initDb(db);
        if (con != null)
        {
            return  RespBean.ok("数据库连接成功");
        }
        return RespBean.error("数据库连接失败");
    }
}

在resources/static目录下创建index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>代码生成工具</title>
    <!-- 开发环境版本,包含了有帮助的命令行警告 -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
    <div id="app">
        <table>
            <tr>
                <td><el-tag size="mini">数据库用户名:</el-tag></td>

                <td><el-input size="mini" v-model="db.username"></el-input></td>
            </tr>
            <tr>
                <td><el-tag size="mini">数据库密码:</el-tag></td>
                <td><el-input size="mini" v-model="db.password"></el-input></td>
            </tr>
            <tr>
                <td><el-tag size="mini">数据库连接地址:</el-tag></td>
                <td><el-input size="mini" v-model="db.url">
                    <template slot="prepend">
                        jdbc:mysql://
                    </template>
                    <template slot="append">
                        ?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
                    </template>
                </el-input></td>
            </tr>
        </table>
        <div style="display: flex">
            <el-button type="primary" size="mini" @click="connect" :disabled="!connectBtnEnabled">连接数据库</el-button>
            <div style="color: brown;font-weight: bold">*{{msg}}*</div>
        </div>
    </div>
    <script>
        new Vue({
            el:"#app",
            data:function () {
                return {
                    msg: '数据库未连接',
                    connectBtnEnabled:true,
                    db:{
                        username:"root",
                        password:"123456",
                        url:"localhost:3306/test"
                    }
                }
            },
            methods:{
                connect(){
                    let _this = this;
                    this.db.url ="jdbc:mysql://"+this.db.url+"?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
                    axios.post('/connect',this.db )
                        .then(function (response) {
                            _this.msg=response.data.msg;
                            _this.db={
                                username:"root",
                                password:"123456",
                                url:"localhost:3306/test"
                            }
                            _this.connectBtnEnabled=false;
                        })
                        .catch(function (error) {
                            console.log(error);
                        });
                }
            }
        })
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值