项目搭建
添加模块
easyJava
1. application.properties
1 2 3 4 5 6 7 8 9 10 11 12
| db.driver.name=com.mysql.jdbc.Driver
db.url=jdbc:mysql:
db.username=root
db.password=root
|
2. 配置pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.easyjava</groupId> <artifactId>easyJava</artifactId> <version>1.0-SNAPSHOT</version>
<properties> <mysql.version>5.1.5</mysql.version> <apach.commons.version>3.4</apach.commons.version> <logback.version>1.2.10</logback.version> <slf4j.version>1.7.7</slf4j.version>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
<dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>${apach.commons.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> </dependencies> </project>
|
easyJava-Demo
1. 添加application.properties
easyJava-Demo->src->main->resources->application.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| # 设置服务器端口号为8070 server.port=8070
# 设置数据库驱动类名为com.mysql.cj.jdbc.Driver spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 设置数据库连接URL,连接到本地MySQL服务器的test数据库,设置时区为GMT+8,不使用SSL连接 spring.datasource.url=jdbc:mysql:
# 设置数据库用户名为root spring.datasource.data-username=root
# 设置数据库密码为root spring.datasource.data-password=root
# 设置HikariCP连接池的最小空闲连接数为5 spring.datasource.hikari.minimum-idle=5
# 设置HikariCP连接池的空闲连接超时时间为180000毫秒(3分钟) spring.datasource.hikari.idle-timeout=180000
# 设置HikariCP连接池的最大连接数为10 spring.datasource.hikari.maximum-pool-size=10
# 设置HikariCP连接池的自动提交为true spring.datasource.hikari.auto-commit=true
# 设置HikariCP连接池的名称为easyjavaPoool spring.datasource.hikari.pool-name=easyjavaPoool
# 设置HikariCP连接池中连接的最大生命周期为1800000毫秒(30分钟) spring.datasource.hikari.max-lifetime=1800000
# 设置HikariCP连接池的连接超时时间为30000毫秒(30秒) spring.datasource.hikari.connection-timeout=30000
# 设置HikariCP连接池的连接测试查询语句为SELECT 1 spring.datasource.hikari.connection-test-query=SELECT 1
|
2. 修改pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| <?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.easyjava</groupId> <artifactId>easyJava-Demo</artifactId> <version>1.0-SNAPSHOT</version>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> </parent>
<properties> <springboot.version>2.2.6.RELEASE</springboot.version> <mybatis.version>1.3.2</mybatis.version> <mysql.version>8.0.17</mysql.version>
<maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties>
<dependencies>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>${springboot.version}</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies> </project>
|
3. 添加easyjava/RunDemoApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.easyjava;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class RunDemoApplication { public static void main(String[] args) { SpringApplication.run(RunDemoApplication.class, args); } }
|
sql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| CREATE TABLE tb_product_info ( id int(11) NOT NULL AUTO_INCREMENT COMMENT '自增ID', company_id varchar(30) DEFAULT NULL COMMENT '公司ID', code varchar(11) DEFAULT NULL COMMENT '商品编号', product_name varchar(200) DEFAULT NULL COMMENT '商品名称', price decimal(15,2) DEFAULT NULL COMMENT '价格', sku_type tinyint(4) DEFAULT NULL COMMENT 'sku类型', color_type tinyint(4) DEFAULT NULL COMMENT '颜色类型', create_time datetime DEFAULT NULL COMMENT '创建时间', create_date date DEFAULT NULL COMMENT '创建日期', stock bigint(20) DEFAULT NULL COMMENT '库存', staus tinyint(4) DEFAULT NULL COMMENT '状态', PRIMARY KEY (id), UNIQUE KEY idx_code (code) USING BTREE, UNIQUE KEY idx_sku_color (sku_type,color_type) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COMMENT='商品信息'
|
读取表结构_读取表
添加easyjava/utils/PropertiesUtils.java
添加workspace-easyjava/easyJava/src/main/java/com/easyjava/utils/PropertiesUtils.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| package com.easyjava.utils;
import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.Map; import java.util.Objects; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap;
public class PropertiesUtils { private static Properties props = new Properties(); private static Map<String, String> PROPER_MAP = new ConcurrentHashMap();
static { InputStream is = null; try { is = PropertiesUtils.class.getClassLoader().getResourceAsStream("application.properties"); props.load(is);
Iterator<Object> iterator = props.keySet().iterator(); while (iterator.hasNext()) { String key = (String) iterator.next(); PROPER_MAP.put(key, props.getProperty(key)); } } catch (Exception e) {
} finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public static String getString(String key) { return PROPER_MAP.get(key); }
public static void main(String[] args) { System.out.println(getString("db.driver.name")); } }
|
添加easyjava/builder/BuildTable.java
添加workspace-easyjava/easyJava/src/main/java/com/easyjava/builder/BuildTable.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| package com.easyjava.builder;
import com.easyjava.utils.PropertiesUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.sql.*;
public class BuildTable { private static final Logger logger = LoggerFactory.getLogger(BuildTable.class); private static Connection conn = null;
private static String SQL_SHOW_TABLE_STATUS = "show table status"; static { String dirverName = PropertiesUtils.getString("db.driver.name"); String url = PropertiesUtils.getString("db.url"); String user = PropertiesUtils.getString("db.username"); String password = PropertiesUtils.getString("db.password"); try { Class.forName(dirverName); conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { logger.error("数据库连接失败", e); } } public static void getTables(){ PreparedStatement ps =null; ResultSet tableResult = null; try { ps = conn.prepareStatement(SQL_SHOW_TABLE_STATUS); tableResult = ps.executeQuery(); while(tableResult.next()){ String tableName = tableResult.getString("name"); String comment = tableResult.getString("comment"); logger.info("tableName:{},comment:{}", tableName, comment); } } catch (Exception e) { logger.error("读取表失败", e); }finally { if (tableResult != null) { try { tableResult.close(); } catch (SQLException e) { logger.error("关闭ResultSet失败", e); } } if(ps != null) { try { ps.close(); } catch (SQLException e) { logger.error("关闭PreparedStatement失败", e); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { logger.error("关闭Connection失败", e); } } } } }
|
添加RunApplication.java
添加workspace-easyjava/easyJava/src/main/java/com/easyjava/RunApplication.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.easyjava;
import com.easyjava.builder.BuildTable;
public class RunApplication { public static void main(String[] args) { BuildTable.getTables(); } }
|
读取表结构——读取表基本信息
easyJava/src/main/java/com/easyjava/bean/TableInfo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| package com.easyjava.bean;
import java.util.LinkedHashMap; import java.util.List; import java.util.Map;
public class TableInfo { private String tableName; private String beanName; private String beanParamName; private String comment; private List<FieldInfo> fieldList; private Map<String, List<FieldInfo>> keyIndexmap = new LinkedHashMap(); private Boolean haveDate; private Boolean haveDateTime; private Boolean haveBigDecimal;
public String getTableName() { return tableName; }
public void setTableName(String tableName) { this.tableName = tableName; }
public String getBeanName() { return beanName; }
public void setBeanName(String beanName) { this.beanName = beanName; }
public String getBeanParamName() { return beanParamName; }
public void setBeanParamName(String beanParamName) { this.beanParamName = beanParamName; }
public String getComment() { return comment; }
public void setComment(String comment) { this.comment = comment; }
public List<FieldInfo> getFieldList() { return fieldList; }
public void setFieldList(List<FieldInfo> fieldList) { this.fieldList = fieldList; }
public Map<String, List<FieldInfo>> getKeyIndexmap() { return keyIndexmap; }
public void setKeyIndexmap(Map<String, List<FieldInfo>> keyIndexmap) { this.keyIndexmap = keyIndexmap; }
public Boolean getHaveDate() { return haveDate; }
public void setHaveDate(Boolean haveDate) { this.haveDate = haveDate; }
public Boolean getHaveDateTime() { return haveDateTime; }
public void setHaveDateTime(Boolean haveDateTime) { this.haveDateTime = haveDateTime; }
public Boolean getHaveBigDecimal() { return haveBigDecimal; }
public void setHaveBigDecimal(Boolean haveBigDecimal) { this.haveBigDecimal = haveBigDecimal; } }
|
easyJava/src/main/java/com/easyjava/bean/FieldInfo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| package com.easyjava.bean;
import com.sun.org.apache.xpath.internal.operations.Bool;
public class FieldInfo { private String FieldName; private String propertyName;
private String sqlType; private String javaType; private String comment; private Boolean isAutoIncrement;
public String getFieldName() { return FieldName; }
public void setFieldName(String fieldName) { FieldName = fieldName; }
public String getPropertyName() { return propertyName; }
public void setPropertyName(String propertyName) { this.propertyName = propertyName; }
public String getSqlType() { return sqlType; }
public void setSqlType(String sqlType) { this.sqlType = sqlType; }
public String getJavaType() { return javaType; }
public void setJavaType(String javaType) { this.javaType = javaType; }
public String getComment() { return comment; }
public void setComment(String comment) { this.comment = comment; }
public Boolean getAutoIncrement() { return isAutoIncrement; }
public void setAutoIncrement(Boolean autoIncrement) { isAutoIncrement = autoIncrement; } }
|
easyJava/src/main/java/com/easyjava/bean/Constants.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.easyjava.bean;
import com.easyjava.utils.PropertiesUtils;
public class Constants { public static boolean IGNORE_TABLE_PERFIX;
public static String SUFFIX_BEAN_PARAM;
static { IGNORE_TABLE_PERFIX = Boolean.valueOf(PropertiesUtils.getString("ignore.table.prefix")); SUFFIX_BEAN_PARAM = PropertiesUtils.getString("suffix.bean.param"); } }
|
修改BuildTable
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| package com.easyjava.builder;
import com.easyjava.bean.Constants; import com.easyjava.bean.TableInfo; import com.easyjava.utils.PropertiesUtils; import com.easyjava.utils.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory;
import java.sql.*; import java.util.ArrayList; import java.util.List;
public class BuildTable { private static final Logger logger = LoggerFactory.getLogger(BuildTable.class); private static Connection conn = null;
private static String SQL_SHOW_TABLE_STATUS = "show table status"; static { String dirverName = PropertiesUtils.getString("db.driver.name"); String url = PropertiesUtils.getString("db.url"); String user = PropertiesUtils.getString("db.username"); String password = PropertiesUtils.getString("db.password"); try { Class.forName(dirverName); conn = DriverManager.getConnection(url, user, password); } catch (Exception e) { logger.error("数据库连接失败", e); } } public static void getTables(){ PreparedStatement ps =null; ResultSet tableResult = null;
List<TableInfo> tableInfoList = new ArrayList<>();
try { ps = conn.prepareStatement(SQL_SHOW_TABLE_STATUS); tableResult = ps.executeQuery(); while(tableResult.next()){ String tableName = tableResult.getString("name"); String comment = tableResult.getString("comment");
String beanName = tableName; if (Constants.IGNORE_TABLE_PERFIX) { beanName = tableName.substring(beanName.indexOf("_") + 1); } beanName = processField(beanName, true);
TableInfo tableInfo = new TableInfo(); tableInfo.setTableName(tableName); tableInfo.setBeanName(beanName); tableInfo.setComment(comment); tableInfo.setBeanParamName(beanName + Constants.SUFFIX_BEAN_PARAM);
logger.info("表:{},备注:{},Javabean:{},JavaParamBean:{}",tableInfo.getTableName(),tableInfo.getComment(),tableInfo.getBeanName(),tableInfo.getBeanParamName()); } } catch (Exception e) { logger.error("读取表失败", e); }finally { if (tableResult != null) { try { tableResult.close(); } catch (SQLException e) { logger.error("关闭ResultSet失败", e); } } if(ps != null) { try { ps.close(); } catch (SQLException e) { logger.error("关闭PreparedStatement失败", e); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { logger.error("关闭Connection失败", e); } } } } private static String processField(String field,Boolean uperCaseFirstLetter) { StringBuffer sb = new StringBuffer(); String[] fields = field.split("_"); sb.append(uperCaseFirstLetter ? StringUtils.uperCaseFirstLetter(fields[0]) : fields[0]); for (int i =1,len= fields.length;i < len;i++) { sb.append(StringUtils.uperCaseFirstLetter(fields[i])); } return sb.toString();
} }
|
easyJava/src/main/java/com/easyjava/utils/StringUtils.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.easyjava.utils;
public class StringUtils { public static String uperCaseFirstLetter(String field) { if(org.apache.commons.lang3.StringUtils.isEmpty(field)) { return field; } return field.substring(0, 1).toUpperCase() + field.substring(1); }
public static String lowerCaseFirstLetter(String field) { if(org.apache.commons.lang3.StringUtils.isEmpty(field)) { return field; } return field.substring(0, 1).toLowerCase() + field.substring(1); }
public static void main(String[] args) { System.out.println(lowerCaseFirstLetter("Company")); } }
|
读取表结构,读取表字段
修改BuildTable.java
修改Constants.java
读取表结构_读取表索引
报错原因
idea导入java项目时包名路径报错解决:https://blog.csdn.net/qq_39324871/article/details/90748577
解决Could not find artifact org.springframework.boot:spring-boot-starter-parent:pom:3.1.1 in alimave问题:
https://blog.csdn.net/weixin_64280364/article/details/131380587
Navicat连接MySQL8.0报客户端不支持请求认证协议,要升级mysql服务器:https://blog.csdn.net/bai500/article/details/105045796