1,什么是ORM? ORM(Object/relationship Mapping),关系对象映射 Hibernate是管理java对象和关系型数据库映射的框架。java对象和sql语句对应有很多不便,orm简化映射操作。
2,第一个Hibernate小例子
插件介绍:Hibernate Tools for Eclipse Plugins 是jBoss推出的一个Eclipse综合开发工具插件,该插件可以简化Hibernate\EJB\JBoss Seam等ORM框架开发的工作 安装方法:Eclipse-->help-->install new software-->add 添加下载好的插件压缩包安装,并在new里检查Hibernate选项即可
JAR包版本:hibernate-4.2.4发布包中lib\requierd\下所有jar、mysql-jdbc驱动包、junit-4.10.jar包。
1).创建文件名"hibernate.cfg.xml"配置文档 借助 hibernate tools生成,没有提示需要导入hibernate-mapping-3.0.dtd支持。
<?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"> <hibernate-configuration> <session-factory> <!-- 必须配置 --> <property name="connection.username">root</property> <!-- 数据库用户名 --> <property name="connection.password">myoa888</property> <!-- 数据库密码 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 驱动字符串 --> <property name="connection.url">jdbc:mysql://127.0.0.1:3336/hibernate?useUnicode=true&characterEncoding=UTF-8</property><!-- 数据库连接字符串 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!--数据库连接方言 --> <!-- 开发常见配置 --> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <!--加载实体类映射文件 --> <mapping resource="Students.hbm.xml" /> </session-factory> </hibernate-configuration>
2).创建要持久化的实体类
1 import java.util.Date; 2 3 //学生持久类 4 public class Students { 5 /* 6 * 设计hibernate持久类必须遵循的原则 1.公有的类,public修饰 2,提供公有的不带参数的构造方法 3,属性要私有, 7 * 4,属性getter/setter封装 8 */ 9 private int sid; // 学号 10 private String sname;// 姓名 11 private String gender;// 性别 12 private Date birthday;// 出生日期 13 private String address;// 地址 14 15 public Students() { 16 17 } 18 19 public Students(int sid, String sname, String gender, Date birthday, 20 String address) { 21 // super(); 22 this.sid = sid; 23 this.sname = sname; 24 this.gender = gender; 25 this.birthday = birthday; 26 this.address = address; 27 } 28 29 @Override 30 public String toString() { 31 return "Students [sid=" + sid + ", sname=" + sname + ", gender=" 32 + gender + ", birthday=" + birthday + ", address=" + address 33 + "]"; 34 } 35 36 public int getSid() { 37 return sid; 38 } 39 40 public void setSid(int sid) { 41 this.sid = sid; 42 } 43 44 public String getSname() { 45 return sname; 46 } 47 48 public void setSname(String sname) { 49 this.sname = sname; 50 } 51 52 public String getGender() { 53 return gender; 54 } 55 56 public void setGender(String gender) { 57 this.gender = gender; 58 } 59 60 public Date getBirthday() { 61 return birthday; 62 } 63 64 public void setBirthday(Date birthday) { 65 this.birthday = birthday; 66 } 67 68 public String getAddress() { 69 return address; 70 } 71 72 public void setAddress(String address) { 73 this.address = address; 74 } 75 76 }
3).借助hibernate tools生成实体类映射文档 Students.hbm.xml,new-->others-->hibernate里带hbm.xml的那个
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2015-11-17 21:20:51 by Hibernate Tools 3.4.0.CR1 --> 5 <hibernate-mapping> 6 <class name="Students" table="STUDENTS"> 7 <id name="sid" type="int"> 8 <column name="SID" /> 9 <generator class="assigned" /> 10 </id> 11 <property name="sname" type="java.lang.String"> 12 <column name="SNAME" /> 13 </property> 14 <property name="gender" type="java.lang.String"> 15 <column name="GENDER" /> 16 </property> 17 <property name="birthday" type="java.util.Date"> 18 <column name="BIRTHDAY" /> 19 </property> 20 <property name="address" type="java.lang.String"> 21 <column name="ADDRESS" /> 22 </property> 23 </class> 24 </hibernate-mapping>
此文将创建完之后,要加入到Hibernate.cfg.xml文件稍后边
1 <!--加载实体类映射文件 --> 2 <mapping resource="Students.hbm.xml" />
4).创建数据库,只需要建库就行,设置好字符集。
5).使用单元测试工具Junit测试:
@Before表示初始化方法 @Test测试方法 @After释放资源方法,测试方法惯例写在Test文件夹中,与src同级;
测试方法如下:
1 import java.util.Date; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7 import org.hibernate.service.ServiceRegistry; 8 import org.hibernate.service.ServiceRegistryBuilder; 9 import org.junit.After; 10 import org.junit.Before; 11 import org.junit.Test; 12 13 public class StudentsTest { 14 private SessionFactory sessionFactory; 15 private Session session; 16 private Transaction transaction; 17 18 @Before 19 public void init(){ 20 //创建配置对象 21 Configuration config=new Configuration().configure(); 22 //创建服务注册对象 23 ServiceRegistry serviceRegistry= 24 new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 25 //创建会话工厂对象 26 sessionFactory=config.buildSessionFactory(serviceRegistry); 27 //创建会话对象 28 session=sessionFactory.openSession(); 29 //开启事务 30 transaction=session.beginTransaction(); 31 } 32 @After 33 public void destory(){ 34 transaction.commit();//提交事务 35 session.close();//关闭会话 36 sessionFactory.close();//关闭会话工厂 37 } 38 @Test 39 public void testSaveStudent(){ 40 Students s=new Students(1,"张三丰","男",new Date(),"武当山"); 41 session.save(s); 42 } 43 }
Hibernate会自动创建数据库表,并插入数据,控制台打印格式化好的建表和插入语句。
原文:http://www.cnblogs.com/sunqian/p/5084168.html