(1).创建数据表
/* MySQL Data Transfer Source Host: localhost Source Database: hibernate Target Host: localhost Target Database: hibernate Date: 2008-9-17 12:07:45 */ SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for person -- ---------------------------- DROP TABLE IF EXISTS `person`; CREATE TABLE `person` ( `id` int(11) NOT NULL auto_increment, `name` varchar(20) NOT NULL, `sex` char(1) default NULL, `address` varchar(200) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=gb2312; -- ---------------------------- -- Records -- ---------------------------- INSERT INTO `person` VALUES ('1', 'anzey', 'm', '广西南宁'); |
(2)POJO代码
package hibernateTest; public class Person { public Person() { // TODO Auto-generated constructor stub } int id; String name; String address; String sex; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } |
(3)配置文件
Hibenate.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"> <!-- 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/hibernate </property> <property name="connection.username">root</property> <property name="connection.password">anzey</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <mapping resource="hibernateTest/Person.hbm.xml" /> </session-factory> </hibernate-configuration> |
Person.hbm.xml配置文件
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="hibernateTest"> <class name="Person" table="person" > <meta attribute="sync-DAO">false</meta> <id name="Id" type="integer" column="id" > <generator class="increment"/> </id> <property name="Name" column="name" type="string" not-null="true" length="20" /> <property name="Sex" column="sex" type="string" not-null="false" length="1" /> <property name="Address" column="address" type="string" not-null="false" length="200" /> </class> </hibernate-mapping> |
(4)测试程序
package hibernateTest; import org.hibernate.*; import org.hibernate.cfg.*; import java.io.*; public class PersonTest { public static void main(String[] args) { Configuration cfg; SessionFactory sf; Session session; Transaction tx; Person person=new Person(); person.setId(3); person.setName("anzey"); person.setAddress("广西南宁"); person.setSex("m"); try { File file=new File("C:\\Documents and Settings\\Administrator\\workspace\\hibernate_Proj\\hibernateTest\\hibernate.cfg.xml"); cfg= new Configuration().configure(file); sf=cfg.buildSessionFactory(); session=sf.openSession(); tx=session.beginTransaction(); session.save(person); tx.commit(); session.close(); System.out.print("Data have been saved into database"); }catch(HibernateException e) { e.printStackTrace(); } } } |
转载于:https://www.cnblogs.com/aboutj2ee/archive/2008/09/17/1292429.html