使用代码去实现创建数据库表
/**
* @Author Li
* @data 2022/1/6 14:50
* @description 创建数据库表工具类
*/
@Slf4j
@Component
public class CreateTable {
@Autowired
private ISysMeterService sysMeterService;
private static CreateTable createTable;
/**
* 初始化注入的 service
*/
@PostConstruct
public void init() {
createTable= this;
createTable.sysMeterService = this.sysMeterService;
}
private static String driverClassName;
private static String url;
private static String username;
private static String password;
/**
*
*/
@Value("${spring.datasource.driverClassName}")
public void setDriverClassName(String driverClassName) {
CreateTable.driverClassName = driverClassName;
}
@Value("${spring.datasource.druid.master.url}")
public void setUrl(String url) {
CreateTable.url = url;
}
@Value("${spring.datasource.druid.master.username}")
public void setUsername(String username) {
CreateTable.username = username;
}
@Value("${spring.datasource.druid.master.password}")
public void setPassword(String password) {
CreateTable.password = password;
}
/*String*/
public static final String TYPE_STRING = "String";
/*Integer*/
public static final String TYPE_INTEGER = "Integer";
/*Double*/
public static final String TYPE_DOUBLE = "Double";
/*Date*/
public static final String TYPE_Date = "Date";
/*BigDecimal*/
public static final String TYPE_BigDecimal = "BigDecimal";
/*text*/
public static final String TYPE_text = "text";
/**
* 连接MySQL数据库
*/
public static Map<String, Object> connectMySQL(){
Connection connection = null;
Statement statement = null;
Map<String, Object> map = new HashMap<>();
try {
// 连接数据库
Class.forName(driverClassName);
// 获取数据库连接
connection = DriverManager.getConnection(url, username, password);
// 根据连接获取可执行Statement
statement = connection.createStatement();
map.put("connection",connection);
map.put("statement",statement);