zoukankan      html  css  js  c++  java
  • 配置hibernate

    Hibernate 主要是ORM,object-relationship-model.对象关系模型。为了熟悉Hibernate,下面简单的做了一个测试。

    jar包。

     1 <!DOCTYPE hibernate-configuration PUBLIC
     2     "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     3     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
     4 
     5 <hibernate-configuration>
     6 
     7     <session-factory>
     8         <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property>
     9         <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/test</property>
    10         <property name="hibernate.connection.username"> root</property>
    11         <property name="hibernate.connection.password"> 1234</property>
    12         
    13         <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect</property>
    14         <property name="show_sql">true</property>
    15         <property name="hibernate.hbm2ddl.auto">update</property>
    16         
    17         <mapping resource="com/shuanlei/pojo/Student.hbm.xml"/>
    18         
    19     </session-factory>
    20 </hibernate-configuration>
    hibernate.cfg.xml
    1 public class Department {
    2     private Integer id;
    3     private String departDesc;
    4     
    5     private Set<Employee> employees= new HashSet<Employee>();
    Department
     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC 
     3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     5 <hibernate-mapping 
     6     package="com.shuanlei.pojo">
     7 
     8     <class name="department" table="t_department" lazy="true">
     9         
    10         <id name="id">
    11             <generator class="native"/>
    12         </id>
    13         
    14         <property name="departDesc" column="departDesc"></property>
    15             <set name="employees">
    16                 <key>
    17                     <column name="emp_id"></column>
    18                 </key>
    19                 <one-to-many class="Employee"/>
    20             </set>
    21     </class>
    22     
    23 </hibernate-mapping>
    Department.hbm.xml
    1 public class Employee {
    2     private Integer id;
    3     private String uname;
    4     
    5     private Department department;
    Employee
     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC 
     3     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     5 <hibernate-mapping 
     6     package="com.shuanlei.pojo">
     7 
     8     <class name="Employee" table="t_employee" lazy="true">
     9         
    10         <id name="id">
    11             <generator class="native"/>
    12         </id>
    13         
    14         <property name="uname" column="uname"></property>
    15         <property name="pwd" 
    16                 column="pwd"/>
    17                 <many-to-one name="department" class="Department">
    18                     <column name="emp_id"></column>
    19                 </many-to-one>
    20     </class>
    21     
    22 </hibernate-mapping>
    Employee.hbm.xml

    测试代码

     1 public class TestHiber {
     2 
     3     Session session = HibUtil.getSession();
     4 
     5     public void add(Student stu) {
     6         try {
     7             session.getTransaction().begin();
     8 
     9             session.save(stu);
    10             session.getTransaction().commit();
    11 
    12         } catch (HibernateException e) {
    13             session.getTransaction().rollback();
    14             e.printStackTrace();
    15         } finally {
    16 
    17             session.close();
    18         }
    19     }
    20 
    21     public static void main(String[] args) {
    22         Student stu = new Student();
    23         stu.setPwd("222");
    24         stu.setUname("aba");
    25         TestHiber testHiber = new TestHiber();
    26         testHiber.add(stu);
    27     }
    28 
    29 }
    View Code
  • 相关阅读:
    三级连动的下拉框(数据库版)吐血推荐
    行排菜单
    用AJAX制作天气预
    XmlHttp实战学习中....
    ASP+JS三级连动下拉框
    ASP连接11种数据库语法总结
    oa数据库设计
    RSS PUBData 把正常时间函数转成rss2.0的标准
    浮点数的表示和基本运算
    C#4.0新特性:可选参数,命名参数,Dynamic
  • 原文地址:https://www.cnblogs.com/shuanlei/p/4250953.html
Copyright © 2011-2022 走看看