zoukankan      html  css  js  c++  java
  • Hibernate 应用

      完善的持久化层应该达到以下目标:

    1、代码可重用性高,能够完成所有的数据库访问操作。

    2、如果有需要的话,能够支持多种数据库平台。

    3、具有相对独立性,当持久化层的实现发生变化,不会影响上层的实现。

      session接口:

    Session接口也称为持久化管理接口,提供保持、更新、删除、加载和查询等数据库操作。Session接口不是线程

    安全的,要避免或采用较好的策略来处理多线程共享同一个Session的情况。Session接口是轻量级的。可以创建

    或销毁多个Session对象。每个Session对象都有自己的缓存,称为第一级缓存,存储当前工作单元加载的实体对象。

     hibernate.cfg.xml配置文件

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC  
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
      
    <hibernate-configuration>  
        <session-factory >  
            <!-- mysql数据库驱动 -->  
            <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>  
            <!-- mysql数据库名称 -->  
            <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/yan</property>  
            <!-- 数据库的登陆用户名 -->  
            <property name="hibernate.connection.username">root</property>  
            <!-- 数据库的登陆密码 -->  
            <property name="hibernate.connection.password">root</property>  
            <!-- 方言:为每一种数据库提供适配器,方便转换 -->  
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <!--指定映射文件  -->  
              <mapping resource="com/bean/Student.hbm.xml" />
        </session-factory>  
    </hibernate-configuration>  

    Strudent.hbm.xml配置文件

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
        Mapping file autogenerated by MyEclipse Persistence Tools
    -->
    <hibernate-mapping>
        <class name="com.bean.Student" table="student" >
            <id name="No" type="java.lang.Integer">
                <column name="NO"  />
                <generator class="assigned" />
            </id>
            <property name="name" type="java.lang.String">
                <column name="name" />
            </property>
            <property name="sex" type="java.lang.String">
            <column name="sex" />
            </property>
        </class>
    </hibernate-mapping>

    Test.java类

    package com.test;
    
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    import com.bean.Student;
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
               //读取配置文件  
            Configuration cfg = new Configuration().configure();  
              
            SessionFactory factory = cfg.buildSessionFactory();  
              
            Session session = null;  
            try{  
                session = factory.openSession();  
                //开启事务  
                session.beginTransaction();  
                  
               Student student=new Student();
               student.setNo(3);;
               student.setName("jk");
               student.setSex("0");
                  
                session.save(student);  
                //提交事务  
                session.getTransaction().commit();  
                  
            }catch(Exception e){  
                e.printStackTrace();  
                //回滚事务  
                session.getTransaction().rollback();  
            }finally{  
                if(session != null){  
                    if(session.isOpen()){  
                        //关闭session  
                        session.close();  
                    }  
                }  
            }  
        }
    
    }
  • 相关阅读:
    python多线程编程(6): 队列同步
    Python验证Url地址的正则表达式
    centos下redis安全配置相关
    redis
    mysql安装 配置
    centos7安装python3 环境变量配置 django安装 以及tab补全功能
    saltstack 与常用服务部署
    vim
    Linux系统基础优化及常用命令
    Shell 基本命令
  • 原文地址:https://www.cnblogs.com/heyesp/p/4612779.html
Copyright © 2011-2022 走看看