Hieberate入门程序

本节写一个简单的程序让大家对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 object
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");//populates the data of the configuration file

//creating seession factory object
SessionFactory factory = cfg.buildSessionFactory();
//creating session object
Session session = factory.openSession();

//creating transaction object
Transaction t = session.beginTransaction();

Student student = new Student();
// student.setId(5);
student.setName("hhh");

session.persist(student);//persisting the object

t.commit();//transaction is committed
session.close();

System.out.println("successfully saved");

}
}
运行程序
在运行应用程序之前,看下我的目录结构:

表结构如下:

运行显示


源代码

参考:



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值