ORM框架——Hibernate

来源:互联网 发布:ubuntu内核下载地址 编辑:程序博客网 时间:2024/04/28 00:15
ORM(Object-Relationship Mapping):对象-关系映射

ORM框架技术:Hibernate、MyBatis
使用JBoss突出的Eclipse综合开发工具插件Hibernate Tools简化开发工作


============================使用Hibernate创建一个工程==========================

创建Hibernate的配置文件

<?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>    <property name="connection.username">root</property>    <property name="connection.password">123</property>    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>    <property name="connection.url">jdbc:mysql:///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="Student.hbm.xml"/>    </session-factory></hibernate-configuration>

创建持久化类

import java.util.Date;public class Student {private int sid;private String name;private String gender;private Date birthday;private String address;public Student(){}public Student(int sid, String name, String gender, Date birthday, String address) {super();this.sid = sid;this.name = name;this.gender = gender;this.birthday = birthday;this.address = address;}public int getSid() {return sid;}public void setSid(int sid) {this.sid = sid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}}

创建对象-关系映射文件

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2016-4-20 15:32:23 by Hibernate Tools 3.5.0.Final --><hibernate-mapping>    <class name="Student" table="STUDENT">        <id name="sid" type="int">            <column name="SID" />            <generator class="assigned" />        </id>        <property name="name" type="java.lang.String">            <column name="NAME" />        </property>        <property name="gender" type="java.lang.String">            <column name="GENDER" />        </property>        <property name="birthday" type="java.util.Date">            <column name="BIRTHDAY" />        </property>        <property name="address" type="java.lang.String">            <column name="ADDRESS" />        </property>    </class></hibernate-mapping>

通过Hibernate API编写数据库的代码

private SessionFactory sessionFactory;private Session session;private Transaction transaction;@Beforepublic void init(){//创建配置对象Configuration config = new Configuration().configure();//创建服务注册对象ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry();//创建会话工厂sessionFactory = config.buildSessionFactory(serviceRegistry);//创建会话session = sessionFactory.openSession();//打开事务transaction = session.beginTransaction();}@Testpublic void testStudent(){//生成学生对象Student s = new Student(1,"汉","male",new Date(),"西大街");session.save(s);}@Afterpublic void destory(){transaction.commit();session.close();sessionFactory.close();}

0 0
原创粉丝点击