java快速生成数据库和实体类

写个根据字段属性快速生成表单和对应实体类的方法
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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值