Hieberate入门程序

来源:互联网 发布:淘宝夜鹰 编辑:程序博客网 时间:2024/06/09 20:19
本节写一个简单的程序让大家对hibernate有一个简单的认识。在例子中,我使用的是Intellij IDEA,通过mvn依赖的方式导入Hibernate库,在这个例子中,我们将应用MySQL数据库连接,所以还需要添加mysql连接依赖。最后的pom文件内如下:
<dependencies><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-core</artifactId><version>5.2.10.Final</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>6.0.6</version></dependency></dependencies>
创建持久化类
public class Student {int id;String name;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;}}
创建持久化类的映射文件
Hibernate使用映射元数据来确定如何加载和存储持久化类的对象。Hibernate映射文件是为Hibernate提供此元数据的一个选择。要创建映射文件,右键单击src -> new -> file -> 指定文件名(例如student.hbm.xml) , 它必须在包外部。
student.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="demo.Student" table="student"><id name="id"><generator class="assigned"></generator></id><property name="name"></property></class></hibernate-mapping>
注意这里:
<class name="demo.Student" table="student">
...
</class>
● name属性(与包含<hibernate-mapping />元素的package属性组合在一起)命名要定义为实体的类的FQN。
● 表属性命名包含该实体的数据的数据库表。
创建配置文件
配置文件hibernate.cfg.xml包含数据库的所有信息,如:connection_url,driver_class,username,password等。hbm2ddl.auto属性用于自动在数据库中创建表。dialect属性指定了与Hibernate进行交互的特定SQL变体。connection.pool_size用于配置Hibernate内置连接池中的连接数。 要创建配置文件,请右键单击src -> new -> file。
<?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="connection.driver_class">com.mysql.jdbc.Driver</property><property name="connection.url">jdbc:mysql://localhost:3306/first_db</property><property name="connection.username">root</property><property name="connection.password">9958</property><property name="dialect">org.hibernate.dialect.MySQLDialect</property><property name="show_sql">true</property><mapping resource="student.hbm.xml"/></session-factory></hibernate-configuration>
创建检索或存储持久对象的类
在这个类中,我们只是将student对象存储到数据库中。
public class StoreData {public static void main(String[] args) {//creating configuration objectConfiguration cfg = new Configuration();cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file//creating seession factory objectSessionFactory factory = cfg.buildSessionFactory();//creating session objectSession session = factory.openSession();//creating transaction objectTransaction t = session.beginTransaction();Student student = new Student();// student.setId(5);student.setName("hhh");session.persist(student);//persisting the objectt.commit();//transaction is committedsession.close();System.out.println("successfully saved");}}
运行程序
在运行应用程序之前,看下我的目录结构:

表结构如下:

运行显示


源代码

参考:

http://www.yiibai.com/hibernate/
http://hibernate.org/orm/


原创粉丝点击