首先打開mySQL數據新建一個數據庫HibernateTest
create database HibernateTest;
use HibernateTest;
CREATE TABLE USER (
? ? user_id CHAR(32) NOT NULL PRIMARY KEY,
? ? name VARCHAR(16) NOT NULL,
? ? sex CHAR(1),
? ? age INT
);
然后編寫一個User.java文件
public class User {
? ? private String id;
? ? private String name;
? ? private char sex;
? ? private int age;
? ? public int getAge() {
? ? ? ? return age;
? ? }
? ? public String getId() {
? ? ? ? return id;
? ? }
? ? public String getName() {
? ? ? ? return name;
? ? }
? ? public char getSex() {
? ? ? ? return sex;
? ? }
? ? public void setAge(int i) {
? ? ? ? age = i;
? ? }
? ? public void setId(String string) {
? ? ? ? id = string;
? ? }
? ? public void setName(String string) {
? ? ? ? name = string;
? ? }
? ? public void setSex(char c) {
? ? ? ? sex = c;
? ? }
}
User.hbm.xml文件是用來描述數據庫中表的字段信息的配置文件內容如下:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
? ? PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
? ? "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
? ? <class name="onlyfun.caterpillar.User" table="USER">
? ? ? ? <id name="id" type="string" unsaved-value="null">
? ? ? ? ? ? <column name="user_id" sql-type="char(32)" />
? ? ? ? ? ? <generator class="uuid.hex"/>
? ? ? ? </id>
? ? ? ? <property name="name" type="string" not-null="true">
? ? ? ? ? ? <column name="name" length="16" not-null="true"/>
? ? ? ? </property>
? ? ? ? <property name="sex" type="char"/>
? ? ? ? <property name="age" type="int"/>
? ? </class>
</hibernate-mapping>
hibernate.cfg.xml文件時用來連接數據庫時使用的配置文件
<?xml version='1.0' encoding='big5'?>
<!DOCTYPE hibernate-configuration
? ? PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
? ? "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
? ? <session-factory>?
??????
??????? <property name="show_sql">true</property>?
??????? <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>?
??????? <property name="connection.driver_class">com.mysql.jdbc.Driver</property>?
??????? <property name="connection.url">jdbc:mysql://localhost/HibernateTest</property>?
??????? <property name="connection.username">caterpillar</property>?
????
??????? <property name="connection.password">123456</property>?
?
??????? <mapping resource="User.hbm.xml"/>
? ? </session-factory>
</hibernate-configuration>
然后我們編寫一個測試類來測試一下剛才的配置
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
public class HibernateTest {
? ? public static void main(String[] args) throws HibernateException {
? ? ? ? SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
? ? ? ? User user = new User();
? ? ? ? user.setName("caterpillar");
? ? ? ? user.setSex('M');
? ? ? ? user.setAge(28);
? ? ? ? Session session = sessionFactory.openSession();
? ? ? ? Transaction tx= session.beginTransaction();
? ? ? ? session.save(user);
? ? ? ? tx.commit();
? ? ? ? session.close();
? ? ? ? sessionFactory.close();
? ?? ?
? ? ? ? System.out.println("新增資料OK!請先用MySQL觀看結果!");
? ? }
}