zoukankan      html  css  js  c++  java
  • Hibernate小程序制作

    要使用hibernate,至少要有三个步骤:

    创建持久化类

    编写映射文件

    编写配置文件

    在已经有测试用的数据库的前提下,我们来写一个小程序。

    • 数据库为

    school--->student---->id ,name, age 

    • 创建持久化的类

    持久化的类可以是一个普通的Java类(POJO类),而且一定要有现代战争无参的构造器。(略)

    编写映射文件

    我们创建了student表,又创建了Student类,虽然表的字段和类的属性是对应的,但是系统并不知道他们的关系,所以我们要编写 

    映射文件,映射文件保存在与类同一级的目录下,扩展名为.hbm.xml。文件主要部分如下

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    	<hibernate-mapping>
    		<class name="com.chuiyuan.Student" table="student">
    			<id name="id" column="id" type="java.lang.Integer">
    				<generator class="native"/>
    			</id>
    			<property name="name" column="name" type="java.lang.String"/>
    			<property name="age" column="age" type="java.lang.Byte" />
    		</class>
    	</hibernate-mapping>
    
    • 编写hibernate的配置文件

    表和类的映射关系文件都创建好了,那么如何使用hibernate访问数据库呢,这就要编写hibernate配置文件 hibernate.cfg,xml。

    直接放在src目录下。

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    	<hibernate-configuration>
    	<session-factory>
    		<property name="show_sql"> true</property>
    		<property name="connection.url">jdbc:mysql://localhost:3306/school</property>
    		<property name="dialect"> org.hibernate.dialect.MySQLDialect</property>
    		<property name="connection.username">root</property>
    		<property name="connection.password">chuiyuan</property>
    		<property name="connection.driver_class">
    			com.mysql.jdbc.Driver</property>
    		<mapping resource="com.chuiyuan.student.hbm.xml"/>
    		
    	<session-factory>
    	</hibernate-configuration>
    

    里面的show_sql属性可以让hibernate在运行时显示sql语句。

    • 测试hibernate用例

    现在表,持久化的类,映射文件,配置文件都准备好了,可以编写一个类,将对象信息保存到数据库,然后读取出来。

    package com.chuiyuan;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    
    public class Hibernate1 {
    	public static void main(String [] args ){
    		Configuration config = new Configuration().configure() ;
    		SessionFactory sessionFactory = config.buildSessionFactory() ;
    		Student student = new Student() ;
    		student.setName("lsj");
    		student.setAge(14);
    		//开启session,相当于开启jdbc的Connection
    		Session session = sessionFactory.openSession();
    		Transaction tx = session.beginTransaction() ;
    		session.save(student) ;
    		tx.commit(); 
    		System.out.println("新人员成功");
    		
    		// 读取数据 
    		Object ob = session.get(Student.class, 1) ;
    		System.out.println(ob.toString());
    		session.close() ;
    		sessionFactory.close();
    	}
    
    }
    

    代码解析

    1. Configuration类的构造方法把默认路径下的配置信息hibernate.cfg.xml文件读入。

    2. config.addClass(Student.class),读入student.hbm.xml文件。

    3. 一个SessionFactory实例对应一个数据库存储源,它有以下特点:

        >线程安全,也就是说同一个实例可以被多个线程共享。

       >重量级的,所以不能随意创建和销毁,一个数据库创建一个SessionFactory。

    4. Session接口是hibernate应用使用最广泛的接口,也称为持久化管理器。有以下特点

     >不是线程安全的,所以要避免多个线程共享一个session。

     >轻量级的,可为每个用户请求分配一个session。Session接口提供了各种数据操作方法。

    当用Session的save方法保存一个Student对象时,只要Session缓存还没有clear,Student对象就处于生命周期中。

    当用Session的load方法加载一个Student对象时,Session先到缓存中去找,如果没有,再才到数据库中去检索。 

     

      

  • 相关阅读:
    java.util.date java.sql.date java.sql.timestamp
    javadoc生成文档时,编码 GBK 的不可映射字符
    java关于ServletConfig FilterConfig什么用
    在项目中用run as java aplication调试类
    replace和replaceAll
    在easyui中的datagrid中使用行内编辑时textarea的换行保存到mysql数据库为\n
    [转] 设计模式另类版
    [转] 不错的俄罗斯方块程序代码(VC++版)
    [转] 30道模拟经典题(JDK1.4)(附解答)
    [转] C#排序算法
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/4594848.html
Copyright © 2011-2022 走看看