jfinal java类型和数据库类型_JFinal 连接数据库生成Model类

很喜欢JFinal 框架的,简单,不用像SSH那样乱七八糟的注入,配置。不过用惯了Hibernate,Eclipse 可以直接连接数据库,批量生成表对象类。嘿嘿……“懒惰”驱使,那就自己写一个吧,肯定以后的学习中,JFinal是很有用的,就写了这个CreateJfinalEntityUtil 工具类。用的是POSTGRESQL数据库,这个数据库非常不错,呵呵……写的不好,大家勿喷。

package xidian.wwf.utils;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.ArrayList;

import java.util.List;

import java.util.Locale;

/**

* 映射数据库,自动生成Entity

* @author WWF

*/

public class CreateJfinalEntityUtil {

private static Connection conn = null;

private static final String URL;

private static final String JDBC_DRIVER;

private static final String USER_NAME;

private static final String PASSWORD;

private static final String DATABASENAME;

private static final String PACKAGENAME;

static {

DATABASENAME = "数据库名";

URL = "jdbc:postgresql://localhost:5432/"+DATABASENAME;

JDBC_DRIVER = "org.postgresql.Driver";

USER_NAME = "数据库帐号";

PASSWORD = "密码";

PACKAGENAME = "xidian.wwf.entity";

}

/**

* 获得连接

* @return

*/

public static Connection getConnection() {

try {

Class.forName(JDBC_DRIVER);

conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);

} catch (ClassNotFoundException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

/**

* 关闭数据库

*/

public static void closeConnection(){

try {

if (conn != null && !conn.isClosed()) {

conn.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 生成字段静态声明

* @param connection

* @param tableName

* @return

*/

private static String CreateEntityString(String tableName){

String result = "package "+PACKAGENAME+";\n\n";

result += "import com.jfinal.plugin.activerecord.Model; \n\n";

result += "public class "+StringUtil.toLowerCaseTheFristChar(tableName)+" extends Model{\n\n";

result += " private static final long serialVersionUID = 1L;\n";

result += " public static final "+StringUtil.toLowerCaseTheFristChar(tableName)+" dao = new "+StringUtil.toLowerCaseTheFristChar(tableName)+"();\n\n";

result += " /**表名**/ \n" ;

result += " public static String TABLE = \""+tableName+"\";" ;

String sql = "select column_name from information_schema.columns where table_name = '"+tableName+"';";

try {

Statement statement = conn.createStatement();

ResultSet resultSet = statement.executeQuery(sql);

while (resultSet.next()) {

if (resultSet.getString(1)!=null&&!resultSet.getString(1).isEmpty()) {

String string = resultSet.getString(1);

String row = " public static final String "+ string.toUpperCase(Locale.CHINA) +" = \""+string+"\";";

String note = " /****/";

result += "\n"+note + "\n" +row;

}

}

resultSet.close();

statement.close();

result+="\n }";

return result;

} catch (SQLException e) {

e.printStackTrace();

return null;

}

}

/**

* 获得数据库的所有表名

* @return

*/

public static List getAllTables(){

String sql = "select relname as TABLE_NAME from pg_class c " +

"where relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%' order by relname";

try {

List result = new ArrayList();

Statement statement = conn.createStatement();

ResultSet resultSet = statement.executeQuery(sql);

while (resultSet.next()) {

if (resultSet.getString(1)!=null&&!resultSet.getString(1).isEmpty()){

result.add(resultSet.getString(1));

}

}

return result;

} catch (Exception e) {

e.printStackTrace();

return null;

}

}

/**

* 生成Entity

*/

public static void CreateEntity(){

try {

getConnection();

List tables = getAllTables();

for (int i = 0; i < tables.size(); i++) {

File createFolder = new File(System.getProperty("user.dir")+"/src/"+PACKAGENAME.replace(".", "/"));

//路径不存在,生成文件夹

if (!createFolder.exists()) {

createFolder.mkdirs();

}

String entityString = CreateEntityString(tables.get(i).trim());

File entity = new File(createFolder.getAbsolutePath()+"/"+StringUtil.toLowerCaseTheFristChar(tables.get(i))+".java");

if (!entity.exists())

{

//写入文件

BufferedWriter out = new BufferedWriter(new FileWriter(entity, true));

out.write(entityString);

out.close();

out = null;

entity = null;

}

}

closeConnection();

System.out.println("生成成功");

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args) {

CreateEntity();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,如果您使用JFinal框架,可以按照以下步骤集成数据库数据: 1. 在JFinal配置文件(通常为config文件)中配置数据库连接参数,例如: ```java public class DemoConfig extends JFinalConfig { public void configConstant(Constants me) { // ... } public void configRoute(Routes me) { // ... } public void configPlugin(Plugins me) { // 配置数据库连接池插件 DruidPlugin dp = new DruidPlugin("jdbc:mysql://localhost:3306/mydb", "root", "password"); me.add(dp); // 配置ActiveRecord插件 ActiveRecordPlugin arp = new ActiveRecordPlugin(dp); me.add(arp); // 配置Model映射 arp.addMapping("mytable", MyTable.class); } public void configInterceptor(Interceptors me) { // ... } public void configHandler(Handlers me) { // ... } } ``` 其中,mydb为数据库名称,root和password分别为数据库的用户名和密码,mytable为要映射的表名,MyTable为对应的Model。 2. 定义ModelModel表示数据库中的一张表,例如: ```java public class MyTable extends Model<MyTable> { public static final MyTable dao = new MyTable(); // ... } ``` 在中定义静态变量dao,用于访问该表的数据。 3. 执行查询语句。使用JFinalModel中的静态方法可以执行查询语句并将结果存储到List对象中,例如: ```java List<MyTable> list = MyTable.dao.find("SELECT * FROM mytable"); ``` 其中,mytable为要查询的表名。 4. 处理查询结果。通过List对象可以获取查询结果的各个字段的值,例如: ```java for (MyTable mytable : list) { int id = mytable.getInt("id"); String name = mytable.getStr("name"); int age = mytable.getInt("age"); // ... } ``` 这里假设查询结果包含id、name和age三个字段。 以上就是JFinal集成数据库数据的基本步骤,您可以根据具体情况进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值