简单的Hibernate插入数据实例【转】

在此之前创建 MySQL数据库代码如下:
create database if not exists `myproject`;
USE `myproject`;
DROP TABLE IF EXISTS `myusertable`;
CREATE TABLE `myusertable` (           
               `ID` int(8) NOT NULL auto_increment,
               `UserName` varchar(16) default NULL,
               `Password` varchar(16) default NULL,
               `Email` varchar(32) default NULL,    
               PRIMARY KEY (`ID`)                  
             ) ENGINE=InnoDB DEFAULT CHARSET=gbk  

建立可持久化类,User.java()类:
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;
}
}

建立配置文件,hibernate.cfg.xm文件l:
<?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">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
   <property name="dialect">
    org.hibernate.dialect.MySQLDialect
   </property>
   <property name="connection.url">
    jdbc:mysql://localhost:3306/myproject
   </property>
   <property name="connection.username">root</property>
   <property name="connection.password">root</property>
   <property name="connection.driver_class">
    com.mysql.jdbc.Driver
   </property>
   <property name="myeclipse.connection.profile">MySQL</property>
   <mapping resource="ch03/hibernate/Myusertable.hbm.xml" />

</session-factory>
</hibernate-configuration>

创建映射文件,User.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="ch03.hibernate.User" table="myusertable" catalog="myproject">
        <id name="id" type="java.lang.Integer">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="username" type="java.lang.String">
            <column name="UserName" length="16" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="Password" length="16" />
        </property>
        <property name="email" type="java.lang.String">
            <column name="Email" length="32" />
        </property>
    </class>
</hibernate-mapping>

建立测试类,Test.java类:
package ch03.hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

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("HuSuMioa");
    user.setPassword("12345");
   
    session.save(user);
   
    tx.commit();
    session.close();
  
   }
   catch(HibernateException err)
   {
    err.printStackTrace();
   }
}
}

  你还要特别注意的是在创建此测试项目之前你必须在项目中引用 此项目所支持的用户 Hibernate包和加入连接 MySQL的连接 jar,如果没有做此步骤,你的项目是不可能正常运行成功的。如没有其它异常的话,我想应该可以正常执行插入操作了……

 

你可能感兴趣的:(Hibernate,mysql,xml,MyEclipse,jdbc)