第一步 创建数据库
-- Create table create table CUSTOMER ( ID NUMBER not null, USERNAME VARCHAR2(100), AGE NUMBER, SEX VARCHAR2(100), CITY VARCHAR2(100) ) tablespace USERS pctfree 10 initrans 1 maxtrans 255 storage ( initial 64 minextents 1 maxextents unlimited );
第二步 建工程 导包 创建实体类
在myeclipse中创建一个web工程,将hibernate所需要的所有jar包导入到web工程的lib包下
创建Book类:
package com.lidongyu; public class Customer { private int id; private String username; private int age; private String sex; private String city; 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } @Override public String toString() { return "Customer [id=" + id + ", username=" + username + ", age=" + age + ", sex=" + sex + ", city=" + city + "]"; } }
第三步 配置Book.hbm.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.lidongyu.Customer" table="Customer"> <id name="id" column="id" > <generator class="native"/> </id> <property name="username" column="username" type="string" /> <property name="age" column="age" type="int" /> <property name="sex" column="sex" type="string" /> <property name="city" column="city" type="string" /> </class> </hibernate-mapping>
第四步 在src目录下创建hibernate.cfg.xmlwen文件
<?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="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <property name="connection.username">scott</property> <property name="connection.password">scott</property> <property name="connection.driver_class">oracle.jdbc.OracleDriver</property> <mapping resource="com/lidongyu/Customer.hbm.xml"/> </session-factory> </hibernate-configuration>
第五步 创建获取Session的类
package com.lidongyu; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtils { private static final Configuration config; private static final SessionFactory factory; static{ config = new Configuration().configure(); factory =config.buildSessionFactory(); } public static Session getSessinon(){ return factory.openSession(); } }
测试类
package com.lidongyu; import org.hibernate.Session; import org.junit.Test; public class TestDeno2 { @Test public void TestTest(){ Session session = HibernateUtils.getSessinon(); session.beginTransaction(); Customer c = new Customer(); c.setId(1); c.setSex("nan"); c.setCity("哈尔滨"); c.setUsername("颂七"); c.setAge(10); session.save(c); session.getTransaction().commit(); session.close(); System.out.println(c); } }
单元测试结果:
数据库信息添加成功:
数据添加了两遍。。。。
通过创建这个web工程,我们可以清楚的了解到使用Session在开发中的优点,避免了多线程的调用。