Chinaunix首页 | 论坛 | 博客
  • 博客访问: 342582
  • 博文数量: 329
  • 博客积分: 2633
  • 博客等级: 少校
  • 技术积分: 3633
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-22 15:43
文章分类

全部博文(329)

文章存档

2013年(244)

2012年(46)

2011年(39)

我的朋友

分类: Java

2012-02-19 16:28:04

Welcome2hibernate工程

需要在mysql中建立myusertable的数据库

Field Name

Datatype

Len

PK,Not NULL,Auto incr

ID

int

8

 

UserName

varchar

16

 

Password

varchar

16

 

Email

varchar

32

 

 

1.hibernate包的引用

主要用到hibernate-3.1.2.zip里面的hibernate3.jar\lib下的36jar包。

2.hibernate.cfg.xml

配置文件

xml version='1.0' encoding='utf-8'?>

DOCTYPE hibernate-configuration PUBLIC

        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

        "">

<hibernate-configuration>

    <session-factory>

       

        <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>

        <property name="connection.url">jdbc:mysql://localhost:3306/MyProjectproperty>

        <property name="connection.username">rootproperty>

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

       

        <property name="connection.pool_size">1property>

       

        <property name="dialect">org.hibernate.dialect.MySQLDialectproperty>

       

        <property name="current_session_context_class">threadproperty>

       

        <property name="cache.provider_class">org.hibernate.cache.NoCacheProviderproperty>

       

        <property name="show_sql">trueproperty>

       

        <property name="hbm2ddl.auto">createproperty>

        <mapping resource="ch03/hibernate/User.hbm.xml"/>

    session-factory>

hibernate-configuration>

3.Test.java

 

package ch03.hibernate;

import org.hibernate.*;

import org.hibernate.cfg.*;

public class Test {

       public static void main(String[] args) {

              try {

                     SessionFactory sf = new Configuration().configure().buildSessionFactory();

                     Session session = sf.openSession();

                     Transaction tx = session.beginTransaction();

                     User user = new User();

                     user.setUsername("Hibernate");

                     user.setPassword("Fra22");

                     session.save(user);

                     session.save(user);

                     tx.commit();

                     session.close();

              } catch (HibernateException e) {

                     e.printStackTrace();

              }

       }

}

 

从这里可以知道是先建立一个Session工厂,再在工厂中打开个session,开始一个事务,完成事务之后,最后是session关闭。

4.User.hbm.xml

User.hbm.xml存储一个表的信息

xml version="1.0" encoding='utf-8'?>

DOCTYPE hibernate-mapping PUBLIC

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

        "">

<hibernate-mapping>

    <class name="ch03.hibernate.User" table="Myusertable">

        <id name="id">

            <generator class="increment"/>

        id>

        <property name="username"/>

        <property name="password"/>

        <property name="email"/>

    class>

hibernate-mapping>

5.User.java

User.hbm.xmlUser.java组成了模型层(Model layer

package ch03.hibernate;

public class User {

    private int id;

    private String username;

    private String password;

    private String email;

       public String getEmail() {

              return email;

       }

       public void setEmail(String email) {

              this.email = email;

       }

       public int getId() {

              return id;

       }

       public void setId(int id) {

              this.id = id;

       }

       public String getPassword() {

              return password;

       }

       public void setPassword(String password) {

              this.password = password;

       }

       public String getUsername() {

              return username;

       }

       public void setUsername(String username) {

              this.username = username;

       }

}

 

阅读(602) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~