Hibernate generates entity classes-manual writing (1), hibernate entity
BaseDao
package com.pb.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class BaseDao { protected Connection conn; protected PreparedStatement ps; protected ResultSet rs; public Connection getConnection() { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:orcl"; String username = "accp"; String password = "accp"; try { Class.forName(driver); conn = DriverManager.getConnection(url, username, password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } public void closeConnection() { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (ps != null) { try { ps.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public ResultSet executeQuery(String sql,Object [] params){ getConnection(); try { ps=conn.prepareStatement(sql); if(params!=null){ for (int i = 0; i < params.length; i++) { ps.setObject(i+1, params[i]); } } rs=ps.executeQuery(); } catch (SQLException e) { e.printStackTrace(); } return rs; } public int executeUpdate(String sql,Object [] params){ int updateNum=-1; getConnection(); try { ps=conn.prepareStatement(sql); if(params!=null){ for (int i = 0; i < params.length; i++) { ps.setObject(i+1, params[i]); } } updateNum=ps.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally{ this.closeConnection(); } return updateNum; } }
TableDao is still the Dao Layer
package com.pb.dao;import java.util.List;import java.util.Map;public interface TableDao { public List<String> getTableName(); public Map<String, String> getCols(String tableName);}
TableDaoImpl for data access
Package com. pb. dao. impl; import java. SQL. resultSet; import java. SQL. SQLException; import java. util. arrayList; import java. util. hashMap; import java. util. iterator; import java. util. list; import java. util. map; import com. pb. dao. baseDao; import com. pb. dao. tableDao; public class TableDaoImpl extends BaseDao implements TableDao {// All table names under the current user @ Override public List <String> getTableName () {List <String> tableNameLi St = null; try {String SQL = "select * from user_tables"; Object [] params ={}; ResultSet rs = super.exe cuteQuery (SQL, params); if (rs! = Null) {tableNameList = new ArrayList <String> (); while (rs. next () {tableNameList. add (rs. getString ("TABLE_NAME") ;}} catch (SQLException e) {e. printStackTrace ();} finally {super. closeConnection () ;}return tableNameList;} // all fields in the specified table name @ Override public Map <String, String> getCols (String tableName) {Map <String, string> map = null; try {String SQL = "select * from user_tab_cols where table_nam E =? "; Object [] params = {tableName}; ResultSet rs1_super.exe cuteQuery (SQL, params); if (rs! = Null) {map = new HashMap <String, String> (); while (rs. next () {map. put (rs. getString ("COLUMN_NAME"), rs. getString ("DATA_TYPE") ;}} catch (SQLException e) {e. printStackTrace ();} finally {super. closeConnection () ;}return map ;}}
Business Layer biz
TableService Interface
package com.pb.biz;import java.util.List;import java.util.Map;public interface TableService { public List<String> getTableName(); public Map<String, String> getCols(String tableName);}
TableService interface business layer implementation
package com.pb.biz.impl;import java.util.List;import java.util.Map;import com.pb.biz.TableService;import com.pb.dao.TableDao;import com.pb.dao.impl.TableDaoImpl;public class TableServiceImpl implements TableService { private TableDao tableDao=new TableDaoImpl(); @Override public List<String> getTableName() { return tableDao.getTableName(); } @Override public Map<String, String> getCols(String tableName) { return tableDao.getCols(tableName); }}
Test class
Package com. pb. test; import java. io. file; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. util. list; import java. util. map; import java. util. producer; import com. pb. biz. tableService; import com. pb. biz. impl. tableServiceImpl; public class DemoTest {public static TableService tableService = new TableServiceImpl (); public static partition input = new partition (System. in); public static void main (String [] args) {Map <String, String> map; // class name String className = null; // obtain the List of all table names under the accp user <String> tableNameList = getTableName (); System. out. println ("enter the name of the table you need"); String tableName = input. next (). toUpperCase (); boolean flag = false; for (String s: tableNameList) {if (s. contains (tableName) {flag = true ;}} if (flag = true) {System. out. println ("the table name has been found" + tableName); // output the table name and generate the class name className = "public class" + tableName. substring (0, 1) + tableName. substring (1 ). toLowerCase () + "{\ n"; // package name to be created: System. out. println ("enter the name of the package to be created:"); String pack = "package" + input. next () + "; \ n"; // obtain the table field map = tableService. getCols (tableName); // generate the file name String filename = tableName Based on the table name. substring (0, 1) + tableName. substring (1 ). toLowerCase () + ". java "; // find the field map = getTableCols (tableName) in the table; // create the attribute String proerty = getProerty (map ); // No parameter construction method String con = getconString (tableName); // get method String getter = getMethod (map); // setter method String setter = setMethod (map ); // generate the total String str = pack + className + proerty + con + getter + setter + "}"; // Write File file File = new File ("d: "+ File. separator + filename); FileOutputStream fos = null; try {fos = new FileOutputStream (file); fos. write (str. getBytes ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {try {fos. close ();} catch (IOException e) {e. printStackTrace () ;}} else {System. out. println ("This table is not available" + tableName) ;}// all table names under the current user are public static List <String> getTableName () {return tableService. getTableName ();} // field public static Map <String, String> getTableCols (String tableName) {return tableService. getCols (tableName);} // No parameter construction method public static String getconString (String tableName) {String conString = "\ n // No parameter Construction Method \ n public" + tableName. substring (0, 1) + tableName. substring (1 ). toLowerCase () + "() {\ n} \ n"; return conString;} // convert the field to the String attribute public static String getProerty (Map <String, string> map) {String str = "// Fields \ n"; for (String s: map. keySet () {if (map. get (s ). equals ("NUMBER") {str + = "\ n private" + "int" + s. toLowerCase () + "; \ n";} else if (map. get (s ). equals ("VARCHAR2") {str + = "\ n private" + "String" + s. toLowerCase () + "; \ n";} else if (map. get (s ). equals ("DATE") {str + = "\ n private" + "Date" + s. toLowerCase () + "; \ n";} else {str + = "\ n private" + map. get (s) + "" + s. toLowerCase () + "; \ n" ;}} return str ;}// get method // convert the field to the get method public static String getMethod (Map <String, String> map) {String str = "// getter Method \ n"; for (String s: map. keySet () {if (map. get (s ). equals ("NUMBER") {str + = "\ n public" + "int" + "get" + s. substring (0, 1) + s. substring (1 ). toLowerCase () + "() {\ n return this. "+ s. toLowerCase () + "; \ n}";} else if (map. get (s ). equals ("VARCHAR2") {str + = "\ n public" + "String" + "get" + s. substring (0, 1) + s. substring (1 ). toLowerCase () + "() {\ n return this. "+ s. toLowerCase () + "; \ n}";} else if (map. get (s ). equals ("DATE") {str + = "\ n public" + "Date" + "get" + s. substring (0, 1) + s. substring (1 ). toLowerCase () + "() {\ n return this. "+ s. toLowerCase () + "; \ n}" ;}else {str + = "\ n public" + map. get (s) + "" + "get" + s. substring (0, 1) + s. substring (1 ). toLowerCase () + "() {\ n return this. "+ s. toLowerCase () + "; \ n}" ;}} return str ;}// set method // convert the field to the set Method public static String setMethod (Map <String, string> map) {String str = "\ n // setter Method \ n"; for (String s: map. keySet () {if (map. get (s ). equals ("NUMBER") {str + = "\ n public void" + "set" + s. substring (0, 1) + s. substring (1 ). toLowerCase () + "(" + "int" + s. toLowerCase () + ") {\ n this. "+ s. toLowerCase () + "=" + s. toLowerCase () + "; \ n} \ n";} else if (map. get (s ). equals ("VARCHAR2") {str + = "\ n public void" + "set" + s. substring (0, 1) + s. substring (1 ). toLowerCase () + "(" + "String" + s. toLowerCase () + ") {\ n this. "+ s. toLowerCase () + "=" + s. toLowerCase () + "; \ n} \ n";} else if (map. get (s ). equals ("DATE") {str + = "\ n public void" + "set" + s. substring (0, 1) + s. substring (1 ). toLowerCase () + "(" + "Date" + s. toLowerCase () + ") {\ n this. "+ s. toLowerCase () + "=" + s. toLowerCase () + "; \ n} \ n";} else {str + = "\ n public void" + "" + "set" + s. substring (0, 1) + s. substring (1 ). toLowerCase () + "(" + map. get (s) + "" + s. toLowerCase () + ") {\ n this. "+ s. toLowerCase () + "=" + s. toLowerCase () + "; \ n} \ n" ;}} return str ;}}
You don't need to write the framework yourself, but there are still a lot of things that you don't need to understand. I hope you can correct them.