Fork me on GitHub

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="// 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="//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;
            }
}

不用框架自己写的,还有漏吊的,还有很多,没有写明白的,望指正

posted @ 2015-03-10 12:02  森林森  阅读(1415)  评论(0编辑  收藏  举报