刚开始复习hibernate,刚复习时,发现全忘了,连环境搭建都不会了,等于从头再来啊,没办法硬着头皮,只得从头再来了。
hibernate是一款优秀的ORM框架,即object relation mapping 对象关系映射。我的理解就是自动把pojo类对象的操作转为对数据库中相应表的操作。简单说就是创建一个pojo类对象,那么数据库中相应的表中也会插入这么一个对象。修改,删除,当然也是的了。可以理解就是尽可能的隔离数据库操作与java开发。
一、下载hibernate jar包
在http://www.hibernate.org/中找到需要的hibernate版本。我下载的是最新的hibernate-release-4.3.1.Final 。
二、复制jar包
复制在lib/required下的所有jar包到项目的lib文件夹下
三、部署hibernate.cfg.xml,相应的pojo 的Student.hbm.xml
在项目的src目录下新建hibernate.cfg.xml文件。在documentation/manual/en-US/html_single/index.html
<?xml version=‘1.0‘ encoding=‘utf-8‘?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:lubby</property> <property name="connection.username">admin</property> <property name="connection.password">admin</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <mapping resource="com/lubby/pojo/Student.hbm.xml"/> </session-factory> </hibernate-configuration>前四个标签是配置数据库的驱动地址和用户名密码。
<!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>这个是配置数据库名字和版本。在documentation/manual/en-US/html_single/index.html中能找到相应数据库对应的字段 我用的是oracle 11g 对应就是上面所写的
<!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property>
<mapping resource="com/lubby/pojo/Student.hbm.xml"/>这个非常重要是告诉hibernate配置文件所配置的pojo类的配置文件地址
四、创建pojo类Student
package com.lubby.pojo; public class Student { private String sid; private String sname; private int age; public Student() { super(); // TODO Auto-generated constructor stub } public Student(String sid, String sname, int age) { super(); this.sid = sid; this.sname = sname; this.age = age; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [sid=" + sid + ", sname=" + sname + ", age=" + age + "]"; } }
每个pojo类都有一个相应的配置文件。都放在pojo类所在的包里面。
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.lubby.pojo"> <class name="Student" table="tab_stu"> <id name="sid" column="sid"></id> <property name="sname" column="sname"></property> <property name="age" column="age"></property> </class> </hibernate-mapping>
<class name="Student" table="tab_stu"> </class>name:类名
table:数据库中对应表名
<id name="sid" column="sid"></id>id:是设置pojo类的主键,column是数据库中对应的主键名。如果column不写,默认和name的内容一样。
<property name="sname" column="sname"></property> <property name="age" column="age"></property>property:设置一般的属性
六、调用hibernate
package com.lubby.main; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.jboss.weld.logging.messages.EventMessage; import com.lubby.pojo.Student; public class StudentTest { public static void main(String args[]){ Student stu = new Student(); stu.setSid("3"); stu.setAge(25); stu.setSname("李惠堂"); Configuration cfg = new Configuration(); SessionFactory sf = cfg.configure().buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); session.save(stu); session.getTransaction().commit(); session.close(); sf.close(); } }
hibernate4之hello world(基础环境搭建)
原文:http://blog.csdn.net/liu00614/article/details/19176813