zoukankan      html  css  js  c++  java
  • Hibernate手动配置

    一、hibernate3.3.1至少要集成的包

    antlr-2.7.6.jar
    commons-collections-3.1.jar
    dom4j-1.6.1.jar
    hibernate3.jar
    javassist-3.4.GA.jar
    jta-1.1.jar
    slf4j-api-1.5.10.jar
    slf4j-simple-1.5.10.jar
     
    二、hibernate开发的三种方式
    1、由Domain object -> mapping -> db。(官方推荐)
    2、由DB开始,用工具生成mapping和Domain object。(使用较多)
    3、由映射文件开始
     
    三、建表语句
    drop table if exists employee;
    create table employee(
    id int primary key,
    name varchar(64) not null,
    email varchar(64) not null,
    hiredate date not null
    )engine = myisam ;

    四、开发domain对象

    package com.yb.domain;
    public class Employee {
     
      private Integer id;
      private String name;
      private String email;
      private java.util.Date hiredate;
      public Integer getId() {
        return id;
      }
      public void setId(Integer id) {
        this.id = id;
      }
      public String getName() {
        return name;
      }
      public void setName(String name) {
        this.name = name;
      }
      public String getEmail() {
        return email;
      }
      public void setEmail(String email) {
        this.email = email;
      }
      public java.util.Date getHiredate() {
        return hiredate;
      }
      public void setHiredate(java.util.Date hiredate) {
        this.hiredate = hiredate;
      }
    }
    五、对象关系映射文件
    作用是用于指定domain对象和表的映射关系
    该文件的取名有规范domain对象.hbm.xml
    一般我们放在和domain对象同一个文件夹下(包下)
    <?xml version="1.0" encoding="utf-8"?>
    <!-- 映射文件需要一个dtd来指定格式 -->
    <!DOCTYPE hibernate-mapping PUBLIC
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.hsp.domain">
        <class name="Employee" table="employee">
            <!-- id元素用于指定主键属性 -->
            <id name="id" column="id" type="java.lang.Integer">
                <!-- 该元素用于指定主键值生成策略hilo native increment sequence uuid -->
                <generator class="increment">
                    <param name="increment">emp_inc</param>
                </generator>
            </id>
            <!-- 对其它属性还要配置 -->
            <property name="name" type="java.lang.String">
                <column name="name" not-null="false"></column>
            </property>
            <property name="email" type="java.lang.String">
                <column name="email" not-null="false"></column>
            </property>
            <property name="hiredate" type="java.util.Date">
                <column name="hiredate" not-null="false"></column>
            </property>
        </class>
    </hibernate-mapping>

    六、手动配置我们的hibernate.cfg.xml文件

    该文件用于配置连接的数据库的类型,driver、用户名、密码、url......,同时管理对象关系映射文件,该文件的名称一般不修改

    <?xml version="1.0" encoding="utf-8"?>
    <!-- 映射文件需要一个dtd来指定格式 -->
    <!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <hibernate-configuration>
        <session-factory>
            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
            <property name="connection.username">root</property>
            <property name="connection.password">1234</property>
            <property name="connection.url">jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8</property>
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
            <!-- 显示出执行sql -->
            <property name="show_sql">true</property>
            <mapping resource="com/yb/domain/Employee.hbm.xml"/>
        </session-factory>
    </hibernate-configuration>

    七、测试文件TestMain.java

    package com.yb.view;
    import java.util.Date;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import com.yb.domain.Employee;
    import com.yb.util.*;
    public class TestMain {
        public static void main(String[] args) {
            //查询[load]->hql语句(hibernate query language)
        }
    
        private static void deleteEmployee() {
            //删除
            //获取一个session
            Session session = MySessionFactory.getSessionFactory().openSession();
            Transaction ts = session.beginTransaction();
            //删除 1、先获取该雇员,然后删除
            Employee emp = (Employee) session.load(Employee.class, 3);
            session.delete(emp);
            ts.commit();
            session.close();
        }
    
        private static void updateEmployee() {
            //修改用户
            //获取一个会话
            Session session = MySessionFactory.getSessionFactory().openSession();
            Transaction ts = session.beginTransaction();
            //修改用户 1、获取要修改的用户,2、修改
            //load是通过主键属性,获取该对象实例
            Employee emp = (Employee) session.load(Employee.class,3);    //select * from employee where id=3
            emp.setName("韩顺平");    //update employee set name='韩顺平' where id=3
            emp.setEmail("abc@sohu.com");
            ts.commit();
            session.close();
        }
    
        public static void addEmployee() {
            //我们使用hibernate完成crud操作【这里我们只见对象,不见表】
            //现在我们不是用service,直接测试
            //1、创建configuration,该对象用于读取hibernate.cfg.xml,完成初始化
            Configuration configuration = new Configuration().configure("hibernate.cfg.xml");
            //2、创建SessionFactory【这是一个会话工厂,是一个重量级的对象】
            SessionFactory sessionFactory = configuration.buildSessionFactory();
            //3、创建Session相当于jdbc Connection【servlet HttpSession,也不是jsp session】
            Session session = sessionFactory.openSession();
    
            //4、对hibernate而言,要求在进行增加、删除、修改的时候必须使用事务提交
            Transaction transaction = session.beginTransaction();
            //添加一个雇员
            Employee employee = new Employee();
            employee.setName("shunping");
            employee.setEmail("shunping@sohu.com");
            employee.setHiredate(new Date());
            //insert ... ...
            //保存
            session.save(employee);//==>insert into ... [被hibernate封装]
            transaction.commit();
            session.close();
        }
    }
  • 相关阅读:
    设计模式- 结构型模式,装饰器模式(5)
    设计模式- 结构型模式,装饰器模式(5)
    jar/war/ear包的区别
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/jingyunyb/p/3540646.html
Copyright © 2011-2022 走看看