zoukankan      html  css  js  c++  java
  • Java反射机制实现Hibernate

    每次开发项目时,在做数据库开发时,对于不同类都有对应的Dao类,这就要要编写大量的Dao类,其中大多是代码堆砌,但有时我们要完成特定的操作,开发独立的Dao类是必须的,但如果只是实现数据的插入、读取、更新、删除,那么如果有一个通用的Dao类可以对数据库中的所有表进行操作,可以免去编写大量同质代码的负担。

    一. Java反射机制

    Reflection是Java被视为动态语言的一个关键性质,这个机制允许程序在运行时通过Reflection APIs却任何一个一直名称的的class的内部信息,包括modifiers、superclass、实现的interfaces、fields和methods等所有信息,并可于运行时改变fields内容和调用methods。

    二.Java反射机制主要提供了以下功能

        1)在运行时判断人一个对象所属的类

        2)在运行是构造任意一个类的对象

        3)在运行时判断任意一个类所具有的成员变量和方法

        4)在运行时调用任意一个对象的方法

    利用Java的反射机制就可以解决之前必须对知道每个类的属性和方法来调用的问题。就可以实现一个山寨的hibernate

    三.配置

     

    首先,还是来配置项目

     这就是一使用了Hibernate的项目的结构,hibernate.cfg.xml和每个pojo类的xml配置文件,不过我们不需要Hibernate所需的配置包,因为我们要自己来实现它。

    Xml代码 复制代码
    1. <hibernate-configuration>  
    2.     <session-factory>  
    3.            
    4.         <property name="show_sql">true</property>   <!--让hb在运行时显示实际执行的sql语句 -->  
    5.            
    6.         <property name="format_sql">true</property> <!-- 使显示的sql格式化-->  
    7.            
    8.         <property name="dialect">                 <!-- 设定sql方法,使用的是mySQL -->  
    9.             org.hibernate.dialect.MySQLDialect   
    10.         </property>  
    11.            
    12.         <property name="connection.driver_class"> <!-- JDBC驱动类的名字 -->  
    13.             com.mysql.jdbc.Driver   
    14.         </property>  
    15.            
    16.         <property name="connection.url">          <!--数据库连结串配置 -->  
    17.             jdbc:mysql://localhost:3306/twblog   
    18.         </property>  
    19.         <!--数据库用户名配置 -->  
    20.         <property name="connection.username">root</property>  
    21.         <!-- 数据库密码配置 -->  
    22.         <property name="connection.password">mysql</property>  
    23.         <!--  pojo类的配置文件 -->  
    24.         <mapping resource="pojo/Userinfo.hbm.xml" />  
    25.         <mapping resource="pojo/Blog.hbm.xml" />  
    26.     </session-factory>  
    27. </hibernate-configuration>  

    用到的工具就是mysql-connector(这是JDBC必须的)和dom4(用来解析xml文件)

    DocumentReader

       用来读取xml文件返回Document对象

    Java代码 复制代码
    1. public class DocumentReader {   
    2.     /**  
    3.      *   
    4.      * @param url XML文件路径  
    5.      * @return Document对象  
    6.      * @throws DocumentException  
    7.      */  
    8.     public static Document read(String url) throws DocumentException{   
    9.         SAXReader reader=new SAXReader();   
    10.         Document document=reader.read(new File(url));   
    11.         return document;   
    12.     }   
    13. }  

    Connect2Database

       

        连接数据库

    Java代码 复制代码
    1. public class Connect2Database {   
    2.     private static Connection connection;   
    3.     private static List<String> resources=new ArrayList();      //存放pojo类配置文件路径的队列   
    4.        
    5.     public static Connection getConnection(){   
    6.         return connection;   
    7.     }   
    8.        
    9.        
    10.     static{   
    11.         try {   
    12.             Document cfgDoc=DocumentReader.read("main/src/hibernate.cfg.xml");   
    13.             Element root=cfgDoc.getRootElement();   
    14.             Element element=root.element("session-factory");   
    15.             String forname="";   
    16.             String user="";   
    17.             String url="";   
    18.             String psw="";   
    19.             for(Iterator i=element.elementIterator();i.hasNext();){   
    20.                 element=(Element)i.next();   
    21.                 Attribute attr=element.attribute(0);   
    22.                 if(attr.getText().equals("connection.driver_class"))   
    23.                     forname=element.getStringValue().trim();   
    24.                 else if(attr.getText().equals("connection.url"))   
    25.                     url=element.getStringValue().trim();   
    26.                 else if(attr.getText().equals("connection.username"))   
    27.                     user=element.getStringValue().trim();   
    28.                 else if(attr.getText().equals("connection.password"))   
    29.                     psw=element.getStringValue().trim();   
    30.                 else if(attr.getName().equals("resource")){   
    31.                     resources.add(attr.getValue());   
    32.                 }   
    33.             }   
    34.             Class.forName(forname);   
    35.             connection=java.sql.DriverManager.getConnection(url, user, psw);   
    36.         } catch (Exception e) {   
    37.             e.printStackTrace();   
    38.         }   
    39.     }   
    40.   
    41.   
    42.     public static List<String> getResources() {   
    43.         return resources;   
    44.     }   
    45. }  

    这样JDBC的配置就完成了

    其中关键是dom4j api的使用,遍历xml节点。

    http://terran-wulf-gmail-com.javaeye.com/blog/731657

  • 相关阅读:
    poj 2187 Beauty Contest(旋转卡壳)
    poj 2540 Hotter Colder(极角计算半平面交)
    poj 1279 Art Gallery(利用极角计算半平面交)
    poj 3384 Feng Shui(半平面交的联机算法)
    poj 1151 Atlantis(矩形面积并)
    zoj 1659 Mobile Phone Coverage(矩形面积并)
    uva 10213 How Many Pieces of Land (欧拉公式计算多面体)
    uva 190 Circle Through Three Points(三点求外心)
    zoj 1280 Intersecting Lines(两直线交点)
    poj 1041 John's trip(欧拉回路)
  • 原文地址:https://www.cnblogs.com/kelin1314/p/1966555.html
Copyright © 2011-2022 走看看