锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
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
);
鐒跺悗緙栧啓涓涓猆ser.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鏂囦歡鏄敤鏉ユ弿榪版暟鎹簱涓〃鐨勫瓧孌典俊鎭殑閰嶇疆鏂囦歡鍐呭濡備笅錛?br /><?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!璜嬪厛鐢∕ySQL瑙鐪嬬祼鏋滐紒");
聽 聽 }
}