标签:
关系型数据库与面向对象
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///test1</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password"></property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto">create</property> <mapping resource="com/dzq/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
User.java
package com.dzq.domain; import java.util.Date; public class User { private int id; private String username; private Date birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }
User.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.dzq.domain"> <class name="User"> <id name="id"> <generator class="native" /> </id> <property name="username" /> <property name="birthday" /> </class> </hibernate-mapping>
TestHibernate.java
package com.dzq.test; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import com.dzq.domain.User; public class TestHibernate { public static void main(String[] args) { Configuration cfg=new Configuration(); cfg.configure(); SessionFactory sc=cfg.buildSessionFactory(); Session s=sc.openSession(); Transaction tx=s.beginTransaction(); User user=new User(); user.setBirthday(new Date()); user.setUsername("dudu"); s.save(user); tx.commit(); s.close(); System.out.println("end"); } }
标签:
原文地址:http://www.cnblogs.com/xiaoduc-org/p/5460033.html