Hello Hibernate 实例(一) 基础实例,hellohibernate,HelloHiberna
分享于 点击 47634 次 点评:185
Hello Hibernate 实例(一) 基础实例,hellohibernate,HelloHiberna
HelloHibernate 实例是个最简单的Hibernate入门实例,对代码没其它要求,遵循一个就行,让人能一看就懂.1.Mysql里有一个Customer表,字段为id,name,age,job.2.Customer实例化类
Customer.java
package com.hibernate;public class Customer { private int id; private String name; private String age; private String job; public String getAge() { return age; } public void setAge(String age) { this.age = age; } 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; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } }
Customer.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="com.hibernate"> <class name="Customer" table="customer" > <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="45" /><property name="Age" column="age" type="string" not–null="true" length="45" /><property name="Job" column="job" type="string" not–null="true" length="45" /> </class></hibernate–mapping>
hibernate.cfg.xml
<?xml version="1.0"?><!DOCTYPE hibernate–configuration PUBLIC "–//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate–configuration–3.0.dtd" ><hibernate–configuration> <session–factory> <!–– local connection properties ––><property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/dys </property><property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">123</property> <!–– property name="hibernate.connection.pool_size"></property ––> <!–– dialect for MySQL ––><property name="dialect"> org.hibernate.dialect.MySQLDialect </property><property name="hibernate.show_sql">false</property><property name="hibernate.transaction.factory_class"> org.hibernate.transaction.JDBCTransactionFactory </property><mapping resource="com/hibernate/Customer.hbm.xml" /> </session–factory></hibernate–configuration>
TestAction.java
package com.hibernate;import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory;import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class TestAction { /** * @param args */ public static void main(String[] args) { try { SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session session = sf.openSession(); Transaction t = session.beginTransaction(); Customer c1 = new Customer(); c1.setName("易木.DingYs"); c1.setAge("24"); c1.setJob("haven't a job!"); session.save(c1); t.commit(); session.close(); } catch (HibernateException e) { e.printStackTrace(); } } }
用户点评