您的位置:首页 > 其它

1.2 hibernate一个简单例子

2015-11-26 14:51 274 查看
1。数据库准备

在booknet数据库的表student中建立两个字段。    

                                                                    


2。在MyEclipse中的代码文件架构如下:



3。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>

        <!-- Database connection settings -->        

        <property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>

        <property name="connection.url">jdbc:jtds:sqlserver://localhost:49196;DatabaseName=booknet</property>

        <property name="connection.username">sa</property>  

        <property name="connection.password"></property>

        <property name="myeclipse.connection.profile">myjdbcdriver</property>

        <!-- JDBC connection pool (use the built-in) -->

        <!--  property name="connection.pool_size">1</property> -->

        <!-- SQL dialect -->

        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

        <!-- Enable Hibernate's automatic session context management -->

        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->

        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->

        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->

        <property name="hbm2ddl.auto">update</property>

        <mapping resource="com/hibernate/util/Student.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

4.student.class代码为:

package com.hibernate.util;

public class Student {

 private String name;

 private int lian;

/**

 * @return the name

 */

public String getName() {
return name;

}

/**

 * @param name the name to set

 */

public void setName(String name) {
this.name = name;

}

/**

 * @return the lian

 */

public int getLian() {
return lian;

}

/**

 * @param lian the lian to set

 */

public void setLian(int lian) {
this.lian = lian;

}

 

}

5.student.hbm.xml的内容

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.hibernate.util">
<class  name="Student" >
<id name="name" column="name"/>
<property name="lian" column="lian"/>

    </class>

</hibernate-mapping>

6.hibernate例子测试,测试代码在StudentTest类中,代码为:

package com.hibernate.util;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class StudentTest {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

        Student s =new Student();

        s.setName("p5");

        s.setLian(25);

        

        Configuration cfg=new Configuration();

        SessionFactory sf=cfg.configure().buildSessionFactory();

        Session session=sf.openSession();

        

        session.beginTransaction();

        session.save(s);

        session.getTransaction().commit();

        session.close();

        sf.close();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: