国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Hibernate生成實體類-手工寫法(一)

2019-11-14 23:13:26
字體:
來源:轉載
供稿:網友
Hibernate生成實體類-手工寫法(一)

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仍然是Dao層

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實現數據訪問

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 { //當前用戶下的所有表名    @Override    public List<String> getTableName() {        List<String> tableNameList = null;        try {            String sql = "select * from user_tables";            Object[] params = {};            ResultSet rs = super.executeQuery(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;    }  //指定表名的所有字段    @Override    public Map<String, String> getCols(String tableName) {        Map<String, String> map=null;        try {        String sql="select * from user_tab_cols where table_name=?";        Object [] params={tableName};        ResultSet rs=super.executeQuery(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;    }}

業務層biz

TableService接口
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接口業務層實現
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);    }}

測試類

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.Scanner;import com.pb.biz.TableService;import com.pb.biz.impl.TableServiceImpl;public class DemoTest {    public static TableService tableService=new TableServiceImpl();    public static Scanner input=new Scanner(System.in);    public static void main(String[] args) {        Map<String, String> map;        //類名        String className=null;                //獲取accp用戶下的全部表名    List<String> tableNameList=getTableName();    System.out.println("請輸入你需要的表名");    String tableName=input.next().toUpperCase();    boolean flag=false;    for (String s : tableNameList) {        if(s.contains(tableName)){            flag=true;                    }    }            if(flag==true){        System.out.println("表已經找到表名為"+tableName);        //輸出表名并生成類名        className ="public class " + tableName.substring(0,1)+tableName.substring(1).toLowerCase()+" {  /n";        //要建立的包名        System.out.println("請輸入要建立的包名:");        String pack="package "+input.next()+" ;/n";        //獲取表中的字段         map =tableService.getCols(tableName);        //根據表名生成文件名         String filename=tableName.substring(0,1)+tableName.substring(1).toLowerCase()+".java";            //找到表中的字段            map=getTableCols(tableName);            //建立屬性字符串            String proerty=getProerty(map);            //無參數構造方法            String con=getconString(tableName);            //get方法            String getter=getMethod(map);            //setter方法            String setter=setMethod(map);            //生成總字符串            String str=pack+className+proerty+con+getter+setter+"}";            //寫入文件            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("沒有這個表"+tableName);    }        }    //當前用戶下所有表名    public static List<String> getTableName(){        return tableService.getTableName();    }    //字段    public static Map<String,String> getTableCols(String tableName){        return tableService.getCols(tableName);    }    //無參數構造方法        public static String getconString(String tableName){            String conString=" /n // 無參數構造方法/n public "+tableName.substring(0,1)+tableName.substring(1).toLowerCase()+"(){/n/n}/n";            return conString;        }            //將字段轉為字符串屬性    public static String getProerty(Map<String,String> map){        String str="http:// 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方法    //將字段轉為get方法        public static String getMethod(Map<String,String> map){            String str="http://getter方法/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方法        //將字段轉為set方法            public static String setMethod(Map<String,String> map){                String str="/n//setter方法/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;            }}

不用框架自己寫的,還有漏吊的,還有很多,沒有寫明白的,望指正


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南安市| 新宾| 从化市| 土默特左旗| 潮安县| 府谷县| 芦溪县| 肇州县| 利川市| 策勒县| 高平市| 北川| 新化县| 金阳县| 蓝山县| 蒲江县| 六枝特区| 五莲县| 柳江县| 肇州县| 丰城市| 阜阳市| 成武县| 仁化县| 从化市| 洛阳市| 江都市| 黄石市| 新化县| 玉环县| 宜昌市| 江源县| 湟中县| 文水县| 望城县| 潮安县| 揭东县| 大余县| 蓬溪县| 靖江市| 麻阳|