写个根据字段属性快速生成表单和对应实体类的方法
application.properties中的sql配置
# 数据源配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
读取数据源配置,操作数据库
public void CreateSql() {
//读取配置文件中的sql数据
String Driver=environment.getProperty("spring.datasource.driver-class-name");
String url=environment.getProperty("spring.datasource.url");
String username=environment.getProperty("spring.datasource.username");
String password=environment.getProperty("spring.datasource.password");
try {
//与数据库连接
Connection con= DriverManager.getConnection(url,username,password);
Statement statement=con.createStatement();
//创建新表
statement.execute("create table testsql(id int, name varchar(80))");
//插入数据
statement.executeUpdate("insert into testsql values(1,'dsad')");
statement.executeUpdate("insert into testsql values(2, '李四')");
} catch (SQLException e) {
e.printStackTrace();
}
}
根据关键字生成实体类
@Override
public void CreateModule() throws IOException {
//定义类名
String ClassName="usertest";
//使用map存属性和对应类型
Map<String,String> map=new HashMap<String,String>();
map.put("int","id");
map.put("String","name");
//java文件存放位置
String Path="D:/project/boot/src/main/java/com/light/spring/Entity/";
File file=new File(Path+ClassName+".java");
file.createNewFile();
FileWriter writer = new FileWriter(file, true);
writer.append("public class "+ClassName+" {\n" );
for(String i:map.keySet()){
writer.append(" private "+i+" "+map.get(i)+";\n" );
}
writer.append("}");
writer.flush();
writer.close();
}
生成的实体类:
public class usertest {
private String name;
private int id;
}