Java SpringBoot实现带界面的代码生成器详解

目录
  • 1.项目gitthub地址链接: https://github.com/baisul/generateCode.git切换到master分支
  • 2.环境
  • 2.1 springboot+freemarker+mysql
  • 2.2 要装node.js,vue文件运行依赖node.js
  • 3.以下就只拿生成java实体类来作为例子
  • 4.application.xml
  • 5.pom.xml
  • 6.Utils
  • 7.生成模板
  • 8.Controller
  • 9.Model
  • 10.index.html(数据库连接生成代码的界面)
  • 11.接口
  • 12.图形化界面
  • 总结

1.项目gitthub地址链接: https://github.com/baisul/generateCode.git切换到master分支

2.环境

2.1 springboot+freemarker+mysql

2.2 要装node.js,vue文件运行依赖node.js

3.以下就只拿生成java实体类来作为例子

4.application.xml

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root

5.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.yl</groupId>
    <artifactId>generate_code</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>generate_code</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>30.1-jre</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

6.Utils

package com.yl.generate_code.utils;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
public class CORFSConfiguration extends WebMvcConfigurerAdapter {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //所有请求都允许跨域
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }
}
package com.yl.generate_code.utils;
import com.yl.generate_code.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 init(Db db) {
        if (connection == null) {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                connection = DriverManager.getConnection( db.getUrl(),db.getUsername(), db.getPassword());
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }
}

7.生成模板

package ${packetName}.model;
import java.util.Date;
/**
* @Description: ${modelName}实体类
* @Author: wfj
* @CreateDate ${createDate}
* @Version: 1.0V
*/
public class ${modelName} {
    <#if columns??>
        <#list columns as column>
            <#if column.type='VARCHAR' || column.type='TEXT' || column.type='CHAR'>
                /**
                * ${column.remark}
                */
                private String ${column.propertyName?uncap_first};
            </#if>
            <#if column.type='INT'>
                /**
                * ${column.remark}
                */
                private Integer ${column.propertyName?uncap_first};
            </#if>
            <#if column.type='DATETIME'>
                /**
                * ${column.remark}
                */
                private Date ${column.propertyName?uncap_first};
            </#if>
            <#if column.type='BIGINT'>
                /**
                * ${column.remark}
                */
                private Long ${column.propertyName?uncap_first};
            </#if>
            <#if column.type='DOUBLE'>
                /**
                * ${column.remark}
                */
                private Double ${column.propertyName?uncap_first};
            </#if>
            <#if column.type='BIT'>
                /**
                * ${column.remark}
                */
                private Boolean ${column.propertyName?uncap_first};
            </#if>
        </#list>
        <#list columns as column>
            <#if column.type='VARCHAR' || column.type='TEXT' || column.type='CHAR'>
               public String get${column.propertyName}() {
                    return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(String ${column.propertyName?uncap_first}) {
                    this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
            <#if column.type='INT'>
                public Integer get${column.propertyName}() {
                return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(Integer ${column.propertyName?uncap_first}) {
                this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
            <#if column.type='DATETIME'>
                public Date get${column.propertyName}() {
                return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(Date ${column.propertyName?uncap_first}) {
                this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
            <#if column.type='BIGINT'>
                public Long get${column.propertyName}() {
                return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(Long ${column.propertyName?uncap_first}) {
                this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
            <#if column.type='DOUBLE'>
                public Double get${column.propertyName}() {
                return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(Double ${column.propertyName?uncap_first}) {
                this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
            <#if column.type='BIT'>
                public Boolean get${column.propertyName}() {
                return ${column.propertyName?uncap_first};
                }
                public void set${column.propertyName}(Boolean ${column.propertyName?uncap_first}) {
                this.${column.propertyName?uncap_first} = ${column.propertyName?uncap_first};
                }
            </#if>
        </#list>
    </#if>
}

8.Controller

package com.yl.generate_code.controller;
import com.google.common.base.CaseFormat;
import com.yl.generate_code.model.Db;
import com.yl.generate_code.model.ResultModel;
import com.yl.generate_code.model.TableClass;
import com.yl.generate_code.utils.DBUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
public class DBController {
    @PostMapping("/connect")
    public ResultModel connect(@RequestBody Db db) {
        Connection connection = DBUtils.init(db);
        if (connection == null) {
            return ResultModel.fail("数据库连接失败");
        } else {
            return ResultModel.success("数据库连接成功");
        }
    }
    @PostMapping("/config")
    public ResultModel config(@RequestBody Map<String,Object> map) {
        String packetName = (String)map.get("packetName");
        try {
            //获取数据库连接
            Connection connection = DBUtils.getConnection();
            //获取数据库元数据
            DatabaseMetaData metaData = connection.getMetaData();
            //获取数据库所有的表
            ResultSet rs = metaData.getTables(connection.getCatalog(), null, null, null);
            List<TableClass> list = new ArrayList<>();
            while (rs.next()) {
                TableClass tableClass = new TableClass();
                tableClass.setPacketName(packetName);
                //获取表名
                String tableName = rs.getString("TABLE_NAME");
                //数据库表下划线的字段转成驼峰,且首字母大写
                String modelName = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName);
                tableClass.setTableName(tableName);
                tableClass.setModelName(modelName);
                tableClass.setServiceName(modelName + "Service");
                tableClass.setMapperName(modelName + "Mapper");
                tableClass.setControllerName(modelName + "Controller");
                list.add(tableClass);
            }
            return ResultModel.success("数据库信息读取成功",list);
        } catch (SQLException e) {
            e.printStackTrace();
            return ResultModel.fail("数据库信息读取失败");
        }
    }
}
package com.yl.generate_code.controller;
import com.yl.generate_code.model.ResultModel;
import com.yl.generate_code.model.TableClass;
import com.yl.generate_code.service.GenerateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@RestController
public class GenerateCodeController {
    @Autowired
    private GenerateCodeService generateCodeService;
    @PostMapping("/generateCode")
    public ResultModel generateCode(@RequestBody List<TableClass> list, HttpServletRequest request) {
        return generateCodeService.generateCode(list,request.getServletContext().getRealPath("/"));
    }
}

9.Model

package com.yl.generate_code.model;
import java.io.Serializable;
public class ColumnClass implements Serializable {
    //实体类属性名
    private String propertyName;
    //实体类属性名对应的表的字段名
    private String columnName;
    //字段类型
    private String type;
    //备注
    private String remark;
    //该字段是否为主键
    private Boolean isPrimary;
    public String getPropertyName() {
        return propertyName;
    }
    public void setPropertyName(String propertyName) {
        this.propertyName = propertyName;
    }
    public String getColumnName() {
        return columnName;
    }
    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getRemark() {
        return remark;
    }
    public void setRemark(String remark) {
        this.remark = remark;
    }
    public Boolean getPrimary() {
        return isPrimary;
    }
    public void setPrimary(Boolean primary) {
        isPrimary = primary;
    }
    @Override
    public String toString() {
        return "ColumnClass{" +
                "propertyName='" + propertyName + '\'' +
                ", columnName='" + columnName + '\'' +
                ", type='" + type + '\'' +
                ", remark='" + remark + '\'' +
                ", isPrimary=" + isPrimary +
                '}';
    }
}
package com.yl.generate_code.model;
import java.io.Serializable;
public class Db implements Serializable {
    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;
    }
}
package com.yl.generate_code.model;
import java.io.Serializable;
public class ResultModel implements Serializable {
    private Integer code;
    private String msg;
    private Object obj;
    public static ResultModel success(String msg,Object obj) {
        return new ResultModel(200,msg,obj);
    }
    public static ResultModel success(String msg) {
        return new ResultModel(200,msg,null);
    }
    public static ResultModel fail(String msg,Object obj) {
        return new ResultModel(500,msg,obj);
    }
    public static ResultModel fail(String msg) {
        return new ResultModel(500,msg,null);
    }
    private ResultModel() {
    }
    public ResultModel(Integer code, String msg, Object obj) {
        this.code = code;
        this.msg = msg;
        this.obj = obj;
    }
    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }
    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;
    }
}
package com.yl.generate_code.model;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class TableClass implements Serializable {
    private String tableName;
    private String modelName;
    private String serviceName;
    private String mapperName;
    private String controllerName;
    private String packetName;
    private String createDate;
    private List<ColumnClass> columns = new ArrayList<>();
    public String getTableName() {
        return tableName;
    }
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }
    public String getModelName() {
        return modelName;
    }
    public void setModelName(String modelName) {
        this.modelName = modelName;
    }
    public String getServiceName() {
        return serviceName;
    }
    public void setServiceName(String serviceName) {
        this.serviceName = serviceName;
    }
    public String getMapperName() {
        return mapperName;
    }
    public void setMapperName(String mapperName) {
        this.mapperName = mapperName;
    }
    public String getControllerName() {
        return controllerName;
    }
    public void setControllerName(String controllerName) {
        this.controllerName = controllerName;
    }
    public String getPacketName() {
        return packetName;
    }
    public void setPacketName(String packetName) {
        this.packetName = packetName;
    }
    public List<ColumnClass> getColumns() {
        return columns;
    }
    public void setColumns(List<ColumnClass> columns) {
        this.columns = columns;
    }
    public String getCreateDate() {
        return createDate;
    }
    public void setCreateDate(String createDate) {
        this.createDate = createDate;
    }
}

10.index.html(数据库连接生成代码的界面)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <!--引入vue.js,elementui和axios-->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/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://unpkg.com/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="!enableButton">连接数据库</el-button>
        <div style="color: red;font-weight: bold" >{{msg}}</div>
        <el-input v-model="packetName" size="mini" style="width:300px"></el-input>
        <el-button size="mini" type="primary" @click="config">配置</el-button>
    </div>
    <el-table
            :data="tableData"
            stripe
            border
            style="width: 100%">
        <el-table-column
                prop="tableName"
                label="表名称"
                width="180">
        </el-table-column>
        <el-table-column
                label="实体类名称"
                width="180">
            <template slot-scope="scope">
                <el-input v-model="scope.row.modelName"></el-input>
            </template>
        </el-table-column>
        <el-table-column
                label="mapper名称">
            <template slot-scope="scope">
                <el-input v-model="scope.row.mapperName"></el-input>
            </template>
        </el-table-column>
        <el-table-column
                label="service名称">
            <template slot-scope="scope">
                <el-input v-model="scope.row.serviceName"></el-input>
            </template>
        </el-table-column>
        <el-table-column
                label="controller名称">
            <template slot-scope="scope">
                <el-input v-model="scope.row.controllerName"></el-input>
            </template>
        </el-table-column>
    </el-table>
    <div>
        <el-button type="success" size="mini" @click="generateCode">生成代码</el-button>
        <div style="color: green;font-weight: bold">{{result}}</div>
        <div>{{codePath}}</div>
    </div>
</div>
<script>
    new Vue({
        el:"#app",
        data() {
            return{
                result: '',
                codePath: '',
                tableData:[],
                packetName: 'com.yl',
                msg : "数据库未连接",
                enableButton: true,
                db: {
                    username: "root",
                    password: "root",
                    url : "localhost:3306/demo"
                }
            }
        },
        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(res => {
                    console.log(res.data)
                    _this.db = {
                        username: "root",
                        password: "root",
                        url : "localhost:3306/demo"
                    }
                    _this.msg = res.data.msg
                    _this.enableButton = false
                }).catch(err => {
                    console.log(err)
                })
            },
            config() {
                let _this = this
                axios.post("/config",{packetName: _this.packetName}).then(res => {
                    console.log(res.data)
                    _this.msg = res.data.msg
                    _this.tableData = res.data.obj
                }).catch(err => {
                    console.log(err)
                })
            },
            generateCode() {
                let _this = this
                axios.post("/generateCode",_this.tableData).then(res => {
                    console.log(res.data)
                    _this.result = res.data.msg
                    _this.codePath = res.data.obj
                }).catch(err => {
                    console.log(err)
                })
            }
        }
    })
</script>
</body>
</html>

11.接口

package com.yl.generate_code.service;
import com.yl.generate_code.model.ResultModel;
import com.yl.generate_code.model.TableClass;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
public interface GenerateCodeService {
    ResultModel generateCode(List<TableClass> list, String realpath);
}
package com.yl.generate_code.service.impl;
import com.google.common.base.CaseFormat;
import com.yl.generate_code.model.ColumnClass;
import com.yl.generate_code.model.ResultModel;
import com.yl.generate_code.model.TableClass;
import com.yl.generate_code.service.GenerateCodeService;
import com.yl.generate_code.utils.DBUtils;
import freemarker.cache.ClassTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class GenerateCodeServiceImpl implements GenerateCodeService {
    Configuration cfg = null;
    {
        cfg = new Configuration(Configuration.VERSION_2_3_31);
        cfg.setTemplateLoader(new ClassTemplateLoader(GenerateCodeServiceImpl.class,"/templates"));
        cfg.setDefaultEncoding("utf-8");
    }
    @Override
    public ResultModel generateCode(List<TableClass> list, String realpath) {
        try {
            Template modelTemplate = cfg.getTemplate("Model.java.ftl");
            Template serviceTemplate = cfg.getTemplate("Service.java.ftl");
            Template serviceImplTemplate = cfg.getTemplate("ServiceImpl.java.ftl");
            Template mapperTemplate = cfg.getTemplate("Mapper.java.ftl");
            Template mapperXmlTemplate = cfg.getTemplate("Mapper.xml.java.ftl");
            Template controllerTemplate = cfg.getTemplate("Controller.java.ftl");
            Connection connection = DBUtils.getConnection();
            DatabaseMetaData metaData = connection.getMetaData();
            for (TableClass tableClass : list) {
                //根据表名获取该表的所有字段
                ResultSet columns = metaData.getColumns(connection.getCatalog(), null, tableClass.getTableName(), null);
                //获取该表的所有主键
                ResultSet primaryKeys = metaData.getPrimaryKeys(connection.getCatalog(), null, tableClass.getTableName());
                List<ColumnClass> columnClasses = new ArrayList<>();
                while (columns.next()) {
                    //获取字段名
                    String column_name = columns.getString("COLUMN_NAME");
                    //获取字段类型
                    String type_name = columns.getString("TYPE_NAME");
                    //获取字段注释
                    String remark = columns.getString("REMARKS");
                    ColumnClass columnClass = new ColumnClass();
                    columnClass.setColumnName(column_name);
                    columnClass.setType(type_name);
                    columnClass.setRemark(remark);
                    columnClass.setPropertyName(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL,column_name));
                    //指标挪到第一
                    primaryKeys.first();
                    while (primaryKeys.next()) {
                        String primaryKey = primaryKeys.getString("COLUMN_NAME");
                        if (column_name.equals(primaryKey)) {
                            columnClass.setPrimary(true);
                        }
                    }
                    columnClasses.add(columnClass);
                }
                tableClass.setColumns(columnClasses);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                tableClass.setCreateDate(sdf.format(new Date()));
                String path = realpath + "/" + tableClass.getPacketName().replace(".","/");
                generate(modelTemplate,tableClass,path+"/model/",1);
                generate(serviceTemplate,tableClass,path+"/service/",1);
                generate(serviceImplTemplate,tableClass,path+"/service/impl",1);
                generate(mapperTemplate,tableClass,path+"/mapper/",1);
                generate(mapperXmlTemplate,tableClass,path+"/mapper/",2);
                generate(controllerTemplate,tableClass,path+"/controller/",1);
            }
            return ResultModel.success("代码已生成",realpath);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultModel.fail("代码生成失败");
        }
    }
    private void generate(Template template,TableClass tableClass,String path,Integer flag) throws IOException, TemplateException {
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String fileName;
        if (flag == 1) {
            fileName = path + "/" + tableClass.getModelName() + template.getName().replace(".ftl","").replace("Model","");
        } else {
            fileName = path + "/" + tableClass.getModelName() + template.getName().replace(".ftl","").replace(".java","");
        }
        FileOutputStream fos = new FileOutputStream(fileName);
        OutputStreamWriter  out = new OutputStreamWriter(fos);
        template.process(tableClass,out);
        fos.close();
        out.close();
    }
}

12.图形化界面

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注我们的更多内容!

(0)

相关推荐

  • SpringBoot快速整合Mybatis、MybatisPlus(代码生成器)实现数据库访问功能

    1. 创建SpringBoot项目 1.1 引入依赖 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="ht

  • SpringBoot+MybatisPlus+代码生成器整合示例

    项目目录结构: pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.or

  • 浅谈springboot中tk.mapper代码生成器的用法说明

    问:什么是tk.mapper? 答:这是一个通用的mapper框架,相当于把mybatis的常用数据库操作方法封装了一下,它实现了jpa的规范,简单的查询更新和插入操作都可以直接使用其自带的方法,无需写额外的代码. 而且它还有根据实体的不为空的字段插入和更新的方法,这个是非常好用的哈. 而且它的集成非常简单和方便,下面我来演示下使用它怎么自动生成代码. pom中引入依赖,这里引入tk.mybatis.mapper的版本依赖是因为在mapper-spring-boot-starter的新版本中没有

  • SpringBoot下使用MyBatis-Puls代码生成器的方法

    1.官方地址: http://mybatis.plus/guide/generator.html#%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B 2.数据库结构: 3.依赖导入 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <

  • springboot整合mybatis-plus代码生成器的配置解析

    AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity.Mapper.Mapper XML.Service.Controller 等各个模块的代码,极大的提升了开发效率. 具体实实现以及配置解析如下: package mybatis_plus; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus.annot

  • Java SpringBoot实现带界面的代码生成器详解

    目录 1.项目gitthub地址链接: https://github.com/baisul/generateCode.git切换到master分支 2.环境 2.1 springboot+freemarker+mysql 2.2 要装node.js,vue文件运行依赖node.js 3.以下就只拿生成java实体类来作为例子 4.application.xml 5.pom.xml 6.Utils 7.生成模板 8.Controller 9.Model 10.index.html(数据库连接生成代

  • Java SpringBoot模板引擎之 Thymeleaf入门详解

    目录 模板引擎简介 引入Thymeleaf模板引擎 分析Thymeleaf模板引擎 测试Thymeleaf模板引擎 1.编写一个TestController 2.编写一个测试页面 test.html 放在 templates 目录下 3.启动项目请求测试 4.结论 Thymeleaf入门: 1.修改测试请求,增加数据传输 2.使用thymeleaf 3.我们去编写下前端页面 4.启动测试! thymeleaf语法学习 1.使用任意的 th:attr 来替换Html中原生属性的值! 2.表达式语法

  • Java SpringBoot安全框架整合Spring Security详解

    目录 1.工业级安全框架介绍 2.建议搭建Spring Security环境 2.1在pom.xml中添加相关依赖 2.2创建Handler类 2.3创建简单的html和配置相关thymeleaf的路径 2.4最后再加个启动类,那么我们的整合测试就完成勒 2.5成果展示 用户名默认user,密码则随机生成的这串数字 3.进阶版使用 3.1用户名和密码自定义 3.2在config包下创建Encoder 3.3赋予账号角色权限 总结 1.工业级安全框架介绍 Spring Security基于Spri

  • IntelliJ Idea SpringBoot 数据库增删改查实例详解

    SpringBoot 是 SpringMVC 的升级,对于编码.配置.部署和监控,更加简单 微服务 微服务是一个新兴的软件架构,就是把一个大型的单个应用程序和服务拆分为数十个的支持微服务.一个微服务的策略可以让工作变得更为简便,它可扩展单个组件而不是整个的应用程序堆栈,从而满足服务等级协议. Spring 为 微服务提供了一整套的组件-SpringClound , SpirngBoot 就是该基础. 第一个SpringBoot程序 这里使用的开发软件是IntelliJ Idea,和Eclipse

  • HBuilderX配置tomcat外部服务器查看编辑jsp界面的方法详解

    一.第一种方法,通过启动本地tomcat,查看jsp 在tomcat的webapps目录下创建文件夹HBuilderX 打开HBuilderX软件,点击文件/导入/导入本地文件,找到tomcat的webapps 导入成功大概是这样. 打开工具设置,找运行设置,外部web服务器调用url,输入http://localhost:8080后面的端口号是自己tomcat的默认端口,如果没有修改过就是8080,我因为修改成80端口(80端口可不写),所以配置中并未填写,注意下面的选项,外部web服务器ur

  • SpringBoot中使用 RabbitMQ的教程详解

    本章主要建立在已经安装好Erlang以及RabbitMQ的基础上,接下来,简单介绍一下使用 一.Direct直接模式 通过routingKey和exchange决定的那个唯一的queue可以接收消息 1.首先到RabbitMQ的管理界面新建一个队列(Direct模式) 2.测试项目的基础结构如下: 这里为了方便测试,直接在父项目中建立两个子模块(生产者和消费者) 3.pom.xml文件的依赖如下: 父项目: <?xml version="1.0" encoding="U

  • SpringBoot实战之处理异常案例详解

    前段时间写了一篇关于实现统一响应信息的博文,根据文中实战操作,能够解决正常响应的一致性,但想要实现优雅响应,还需要优雅的处理异常响应,所以有了这篇内容. 作为后台服务,能够正确的处理程序抛出的异常,并返回友好的异常信息是非常重要的,毕竟我们大部分代码都是为了 处理异常情况.而且,统一的异常响应,有助于客户端理解服务端响应,并作出正确处理,而且能够提升接口的服务质量. SpringBoot提供了异常的响应,可以通过/error请求查看效果: 这是从浏览器打开的场景,也就是请求头不包括content

  • Springboot整合Netty实现RPC服务器详解流程

    目录 一.什么是RPC? 二.实现RPC需要解决那些问题? 1. 约定通信协议格式 RPC请求 RPC响应 2. 序列化方式 3. TCP粘包.拆包 4. 网络通信框架的选择 三.RPC服务端 四.RPC客户端 总结 一.什么是RPC? RPC(Remote Procedure Call)远程过程调用,是一种进程间的通信方式,其可以做到像调用本地方法那样调用位于远程的计算机的服务.其实现的原理过程如下: 本地的进程通过接口进行本地方法调用. RPC客户端将调用的接口名.接口方法.方法参数等信息利

  • Java OpenCV学习之Mat的基本操作详解

    目录 使用OpenCV时你需要补充的知识 Mat对象 Mat划线 Mat在己有图片上加圆圈 ImageShowAddCircle.java ImageViewer.java Mat与Image互转 OpenCVUtil.java Mat使用blur图片 环境好了,我们就可以进入正文了. 在之前入门一.二中分别已经有画图的两个例子了.但没有细节展开我们的代码和OpenCV到底在干什么. 使用OpenCV时你需要补充的知识 你需要熟练使用Java Swing,或者是其它任何一门语言中关于GUI方面的

  • java基础(System.err和System.out)详解

    今天有位同事在使用System.err和System.out遇上了一些小问题. 看了些资料总结下: 1.JDK文档对两者的解释: out:"标准"输出流.此流已打开并准备接受输出数据.通常,此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标. err:"标准"错误输出流.此流已打开并准备接受输出数据.通常,此流对应于显示器输出或者由主机环境或用户指定的另一个输出目标.按照惯例,此输出流用于显示错误消息,或者显示那些即使用户输出流(变量 out 的值)已经重

随机推荐