zoukankan      html  css  js  c++  java
  • 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()+" {  
    ";
            //要建立的包名
            System.out.println("请输入要建立的包名:");
            String pack="package "+input.next()+" ;
    ";
            //获取表中的字段
             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=" 
     // 无参数构造方法
     public "+tableName.substring(0,1)+tableName.substring(1).toLowerCase()+"(){
    
    }
    ";
                return conString;
            }
            
        //将字段转为字符串属性
        public static String getProerty(Map<String,String> map){
            String str="// Fields 
    ";
            for(String s:map.keySet()){
                if(map.get(s).equals("NUMBER")){
                    str+=" 
     private " +"int "+s.toLowerCase()+";
    ";
                }else  if(map.get(s).equals("VARCHAR2")){
                    str+=" 
     private " +"String "+s.toLowerCase()+";
    ";
                }else  if(map.get(s).equals("DATE")){
                    str+=" 
     private " +"Date "+s.toLowerCase()+";
    ";
                }else{
                    str+=" 
     private " +map.get(s)+" "+s.toLowerCase()+";
    ";
                }
            }
            return str;
        }
        //get方法
        //将字段转为get方法
            public static String getMethod(Map<String,String> map){
                String str="//getter方法
    ";
                for(String s:map.keySet()){
                    if(map.get(s).equals("NUMBER")){
                        str+=" 
     public " +"int "+"get"+s.substring(0,1)+s.substring(1).toLowerCase()+"(){
      return  this."+s.toLowerCase()+"; 
    }";
                    }else  if(map.get(s).equals("VARCHAR2")){
                        str+="  
     public " +"String "+"get"+s.substring(0,1)+s.substring(1).toLowerCase()+"(){
      return  this."+s.toLowerCase()+"; 
    }";
                    }else  if(map.get(s).equals("DATE")){
                        str+="  
     public " +"Date "+"get"+s.substring(0,1)+s.substring(1).toLowerCase()+"(){
      return  this."+s.toLowerCase()+"; 
    }";
                    }else{
                        str+=" 
     public " +map.get(s)+" "+"get"+s.substring(0,1)+s.substring(1).toLowerCase()+"(){
      return  this."+s.toLowerCase()+"; 
    }";
                    }
                }
                return str;
            }
            //set方法
            //将字段转为set方法
                public static String setMethod(Map<String,String> map){
                    String str="
    //setter方法
    ";
                    for(String s:map.keySet()){
                        if(map.get(s).equals("NUMBER")){
                            str+=" 
     public  void  " +"set"+s.substring(0,1)+s.substring(1).toLowerCase()+"("+"int "+s.toLowerCase()+") {
     this."+s.toLowerCase()+"="+s.toLowerCase()+";
    }
    ";
                        }else  if(map.get(s).equals("VARCHAR2")){
                            str+="  
     public  void " +"set"+s.substring(0,1)+s.substring(1).toLowerCase()+"("+"String "+s.toLowerCase()+") {
     this."+s.toLowerCase()+"="+s.toLowerCase()+";
    }
    ";
                        }else  if(map.get(s).equals("DATE")){
                            str+="  
     public  void " +"set"+s.substring(0,1)+s.substring(1).toLowerCase()+"("+"Date "+s.toLowerCase()+") {
     this."+s.toLowerCase()+"="+s.toLowerCase()+";
    }
    ";
                        }else{
                            str+=" 
     public   void " +" "+"set"+s.substring(0,1)+s.substring(1).toLowerCase()+"("+map.get(s)+"  "+s.toLowerCase()+") {
     this."+s.toLowerCase()+"="+s.toLowerCase()+";
    }
    ";
                        }
                    }
                    return str;
                }
    }

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

  • 相关阅读:
    php极光网络一键登录(yii框架)
    Sublime Text3将多行转为为一行 | Sublime Text 快速分别独立选中多行
    mysql 将时间戳转换成日期格式
    Vant主题定制修改颜色样式
    TypeError: this.getOptions is not a function 引入less一直报错
    export defaul 和 export定义和区别
    Vue vant引入,tabbar封装使用示例
    php去除富文本编辑器中的内容格式
    ES6:高级数组函数,filter/map/reduce
    [BZOJ2793][Poi2012]Vouchers
  • 原文地址:https://www.cnblogs.com/liunanjava/p/4325596.html
Copyright © 2011-2022 走看看