实现mysql数据库的增删改查功能
import com.mchange.v2.collection.MapEntry;
import lombok.Data;
import org.junit.jupiter.api.Test;import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.*;
import java.sql.*;
import java.util.*;/*** 自定义框架* 生成mysql表对应的实体类*/
public class MySqlMapper {/*** 配制参数*/static class Config {//mysql地址,数据库,用户名,密码final static private String address = "127.0.0.1:3306";final static private String dbName = "creative";final static private String username = "root";final static private String password = "123456";//模型保存的位置final static private String modelPath = "C:\\Users\\admin\\IdeaProjects\\mytest1\\src\\main\\java\\com\\libii\\model";//mysql数据类型与java数据类型转换final static MapEntry[] map = {new MapEntry("INT", "Integer"),new MapEntry("VARCHAR", "String"),new MapEntry("TIMESTAMP", "String"),new MapEntry("DOUBLE", "Double")};}/*** 内存参数*/static class Variable {final static private String packagePath = Config.modelPath.split("java\\\\")[1].replace("\\", ".");static private Map<String, String> mapper = new HashMap<>();static {for (MapEntry mapEntry : Config.map) {mapper.put(String.valueOf(mapEntry.getKey()), String.valueOf(mapEntry.getValue()));}}}/*** 获取连接** @return* @throws SQLException* @throws ClassNotFoundException*/public static Connection getConn() throws SQLException, ClassNotFoundException {Class.forName("com.mysql.jdbc.Driver");String url = "jdbc:mysql://" + Config.address + "/" + Config.dbName + "?useUnicode=true&characterEncoding=UTF-8";Connection conn = DriverManager.getConnection(url, Config.username, Config.password);return conn;}public static void main(String[] args) throws Exception {Connection conn = getConn();List<ModelTable> modelTableList = Business.getModelTable(conn);//获取数据库下的表结构conn.close();Business.foreach(modelTableList);//遍历结构数据}static class Business {//业务类/*** 获取数据库中表与字段的集合** @param conn* @return*/public static List<ModelTable> getModelTable(Connection conn) throws Exception {List<ModelTable> modelTableList = new LinkedList<>();Map<String, String> tableMsg = Business.getTableNameList(conn);for (Map.Entry<String, String> line : tableMsg.entrySet()) {ModelTable tableFieldMap = getTableFieldMap(conn, line);modelTableList.add(tableFieldMap);}return modelTableList;}/*** 获取表名称** @param conn* @return* @throws Exception*/public static Map<String, String> getTableNameList(Connection conn) throws Exception {String sql = "select TABLE_NAME,TABLE_COMMENT from information_schema.tables where table_schema=\"" + Config.dbName + "\"";List<Map<String, Object>> tableMap = Util.select(sql, conn);Map<String, String> tableMsg = new HashMap<>();for (Map<String, Object> line : tableMap) {String tableName = String.valueOf(line.get("TABLE_NAME"));String tableComment = String.valueOf(line.get("TABLE_COMMENT"));tableMsg.put(tableName, tableComment);}return tableMsg;}/*** 获取表中的字段信息** @param conn* @return* @throws Exception*/public static ModelTable getTableFieldMap(Connection conn, Map.Entry<String, String> line) throws Exception {DatabaseMetaData meta = conn.getMetaData();String tableName = line.getKey();String tableComment = line.getValue();ResultSet rs = meta.getColumns(null, "%", tableName, "%");Map<String, MapEntry> tableFieldMap = new HashMap<>();ModelTable modelTable = new ModelTable();modelTable.setTableName(tableName);modelTable.setFields(tableFieldMap);modelTable.setTableComment(tableComment);while (rs.next()) {String columnName = rs.getString("COLUMN_NAME");String dataTypeName = rs.getString("TYPE_NAME");String remarks = rs.getString("REMARKS");tableFieldMap.put(columnName, new MapEntry(dataTypeName, remarks));//字段名称,字段类型}return modelTable;}