zoukankan      html  css  js  c++  java
  • 初始Hibernate框架

    首先我们需要先创建一个案例

         构建一个Student 的实体类

               

    private String name;
       private Integer age;
       private Integer id;

           在SRC根目录下构建一个大配置

                名称是:Hibernate.cfg.xml

             

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <<hibernate-configuration>
       <session-factory>
            <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
            <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
            <property name="connection.username">sb</property>
            <property name="connection.password">sb</property>
            
            <!-- 输出所有 SQL 语句到控制台。 -->
            <property name="hibernate.show_sql">true</property>
            
            <!-- 在 log 和 console 中打印出更漂亮的 SQL。 -->
            <property name="hibernate.format_sql">true</property>
            <!-- 方言 -->
            <property name="hibernate.dialect">    org.hibernate.dialect.Oracle10gDialect</property>
        
           <!-- 关联小配置 -->
           
       </session-factory>
    
    </hibernate-configuration>
        
    大配置

           继而在组建一个名为:Student.hbm.xml 的小配置

       

    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
     
     <hibernate-mapping package="cn.happy.entity">
         <class name="Student" table="Student">
             <id name="id" type="int" column="id">
             </id>
             <property name="name" type="string" column="name"/>
             <property name="age" type="int" column="age"/>
         </class>    
     </hibernate-mapping>
    小配置

          编写一个测试类:

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.junit.After;
    import org.junit.Before;
    
    import cn.happy.entity.Student;
    import cn.happy.until.Hibernate_until;
    
    public class Test {
    
        public static void main(String[] args) {
            //addAll();
            //delete();
            find();
        }
        
        
        public static void addAll()
        {
            Student stu=new Student();
            stu.setSid(1);
            stu.setAge(18);
            stu.setName("我是狗");
            
            //获取session对象
            Session session =  Hibernate_until.getSession();
            //开启事务
            Transaction tran = session.beginTransaction();
            //hibernate保存
            session.save(stu);
            System.out.println("成功保存!");
            tran.commit();
            Hibernate_until.closeSession();
        }
        public static void delete()
        {
            //打开session
            Session session =  Hibernate_until.getSession();
            //开始一个事务
            Transaction tx=session.beginTransaction();
            //获取部门的对象
            Student stu=(Student)session.get(Student.class, new Integer(1));
            //删除对象(持久化操作)
            if(stu!=null)
            {
              session.delete(stu);
            }
            try {
                 //提交事务
                tx.commit();    
                System.out.println("删除成功");
            } catch (Exception e) {
                 //回滚事务
                tx.rollback();
                System.out.println("删除回滚");
            }
            Hibernate_until.closeSession();
        }
        public static void update()
        {
            Session session =  Hibernate_until.getSession();
            //开始一个事务
            Transaction tx=session.beginTransaction();
            //获取部门的对象
            Student stu=(Student)session.get(Student.class, new Integer(1));
            //删除对象(持久化操作)
            if(stu!=null)
            {
              stu.setName("武松");
              session.update(stu);
            }
            try {
                 //提交事务
                tx.commit();    
                System.out.println("删除成功");
            } catch (Exception e) {
                 //回滚事务
                tx.rollback();
                System.out.println("删除回滚");
            }
            Hibernate_until.closeSession();
        }
        public static void find()
        {
                Session session =  Hibernate_until.getSession();
                //获取部门的对象
                Student stu=(Student)session.get(Student.class, new Integer(2));
                System.out.println(stu);
        }
        
        
        
    
    }
    测试类

    上述就是在Hibernate 框架中,第一次实例的书写

  • 相关阅读:
    第一个dubbo程序
    spring aop通过注解实现日志记录
    Java操作zookeeper
    VMware安装Linux并配置网络通信
    多线程工具之CompletionService
    Netty实现简易http_server
    python3.4 + Django1.7.7 表单的一些问题
    【编程练习】八大排序算法
    OpenCV特征点检测------Surf(特征点篇)
    Opencv学习笔记------Harris角点检测
  • 原文地址:https://www.cnblogs.com/zsping/p/5815359.html
Copyright © 2011-2022 走看看