Java实现关系型数据库工具类JdbcUtils系列五:ORM对象关系映射
一、ORM对象关系映射
- 数据库中的一个表:Java中的一个类
- 数据库中的一个字段:Java中的一个属性
- 数据表中的一条数据:Java中的一个对象
二、表结构和表对应的实体类
CREATE TABLE `databaseInfo` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增id',
`database_name` varchar(100) NOT NULL COMMENT '数据库名称',
`table_name` varchar(100) NOT NULL COMMENT '表名',
`table_comment` varchar(300) DEFAULT NULL COMMENT '表的描述',
`table_rows` bigint(20) DEFAULT 0 COMMENT '表的行数',
`table_key` varchar(100) NOT NULL COMMENT '表的主键',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
`update_time` datetime DEFAULT NULL COMMENT '更新时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_databasename_tablename` (`database_name`,`table_name`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='源数据库表信息';
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import java.io.Serializable;
/**
* 数据库元信息
*/
@Data
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@ToString
public class DatabaseInfo implements Serializable {
/**
* 数据库名称
*/
private String databaseName;
/**
* 数据表全称,格式:dbName.tableName
*/
private String tableName;
/**
* 表的描述
*/
private String tableComment;
/**
* 表的行数
*/
private Long tableRows;
/**
* 表的主键
*/
private String tableKey;
}
三、查询mysql数据库元信息ORM映射到实体类中
public DatabaseInfo get() throws SQLException, IOException, ClassNotFoundException {
DatabaseInfo databaseInfo = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//1.获取连接
conn = JdbcUtils.getConnection();
//2.获取PreparedStatement,用于发送sql
String sql = "select t.TABLE_SCHEMA as DATABASE_NAME,CONCAT_WS('.',t.TABLE_SCHEMA,t" +
".TABLE_NAME) as TABLE_NAME,\n" +
"t.TABLE_COMMENT,t.TABLE_ROWS,k.COLUMN_NAME as TABLE_KEY \n" +
"from INFORMATION_SCHEMA.TABLES as t\n" +
"inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE as k\n" +
"on k.TABLE_NAME=t.TABLE_NAME and CONSTRAINT_NAME = 'PRIMARY' and t.TABLE_SCHEMA " +
"= ?";
ps = conn.prepareStatement(sql);
//3.填充占位符
ps.setString(1, "dw");
//4.执行sql,获取ResultSet
rs = ps.executeQuery();
//5.获取结果集中的数据
if (rs.next()) {
String datbase_name = rs.getString("DATABASE_NAME");
String table_name = rs.getString("TABLE_NAME");
String table_comment = rs.getString("TABLE_COMMENT");
Long table_rows = rs.getLong("TABLE_ROWS");
String table_key = rs.getString("TABLE_KEY");
//6.将结果集中获取的散列数据封装进对象
databaseInfo = new DatabaseInfo(datbase_name, table_name, table_comment, table_rows,
table_key);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//7.关闭连接
JdbcUtils.release(conn,ps,rs);
}
return databaseInfo;
}
四、测试类
@Test
public void test4() throws SQLException, IOException, ClassNotFoundException {
DatabaseInfo databaseInfo = get();
if(databaseInfo == null){
System.out.println("无法获取数据库元信息");
}else{
System.out.println("查询到指定mysql数据库元信息");
System.out.println(databaseInfo);
}
}
五、完整代码
import com.bigdata.plus.Entity.DatabaseInfo;
import org.junit.Test;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
/*
使用PrepareStatement完成增删改
*/
public class PrepareStatementTest {
@Test
public void test4() throws SQLException, IOException, ClassNotFoundException {
DatabaseInfo databaseInfo = get();
if(databaseInfo == null){
System.out.println("无法获取数据库元信息");
}else{
System.out.println("查询到指定mysql数据库元信息");
System.out.println(databaseInfo);
}
}
public DatabaseInfo get() throws SQLException, IOException, ClassNotFoundException {
DatabaseInfo databaseInfo = null;
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
//1.获取连接
conn = JdbcUtils.getConnection();
//2.获取PreparedStatement,用于发送sql
String sql = "select t.TABLE_SCHEMA as DATABASE_NAME,CONCAT_WS('.',t.TABLE_SCHEMA,t" +
".TABLE_NAME) as TABLE_NAME,\n" +
"t.TABLE_COMMENT,t.TABLE_ROWS,k.COLUMN_NAME as TABLE_KEY \n" +
"from INFORMATION_SCHEMA.TABLES as t\n" +
"inner join INFORMATION_SCHEMA.KEY_COLUMN_USAGE as k\n" +
"on k.TABLE_NAME=t.TABLE_NAME and CONSTRAINT_NAME = 'PRIMARY' and t.TABLE_SCHEMA " +
"= ?";
ps = conn.prepareStatement(sql);
//3.填充占位符
ps.setString(1, "dw");
//4.执行sql,获取ResultSet
rs = ps.executeQuery();
//5.获取结果集中的数据
if (rs.next()) {
String datbase_name = rs.getString("DATABASE_NAME");
String table_name = rs.getString("TABLE_NAME");
String table_comment = rs.getString("TABLE_COMMENT");
Long table_rows = rs.getLong("TABLE_ROWS");
String table_key = rs.getString("TABLE_KEY");
//6.将结果集中获取的散列数据封装进对象
databaseInfo = new DatabaseInfo(datbase_name, table_name, table_comment, table_rows,
table_key);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//7.关闭连接
JdbcUtils.release(conn,ps,rs);
}
return databaseInfo;
}
}
六、运行测试类输出如下所示
版权声明:本文为zhengzaifeidelushang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。