hibernate开发1--使用hibernate5.1.0保存数据到数据库
从hibernate5.x.x开始对获取SessionFactory的方式做了修改,按照原来的代码获取不到SessionFactory了,hibernate5.x.x获取SessionFactory的代码如下:
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
Session session =sessionFactory.openSession();
hibernate最新版本(目前最新版本为hibernate5.1.0)的下载地址为:http://hibernate.org/
一、hibernate环境的搭建:hibernate环境搭建还算比较简单的,其主要的jar包为hibernate-core-5.1.0.Final.jar,当然,我这里强烈建议大家把下载好的hibernate文件中lib目录下required里面的所有jar文件都拷贝到自己项目WEB-INF/lib目录下面,否则在调试的的时候总会遇到各种找不到类的错误问题。
二、新建hibernate.cfg.xml文件,其作用为配置数据库基本连接信息,配置好以后我们就不需要再程序中写连接数据库的代码了,其配置如下:
<!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.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.username">你的mysql数据库登陆名</property>
<property name="connection.password">你的mysql数据库登陆密码</property>
<property name="connection.url">
jdbc:mysql:///TestHibernate?createDatabaseIfNotExist=true<!--如果数据库不存在,则自动创建数据库
--> </property>
<property name="hbm2ddl.auto">update</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="cn/test/Bean/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
三、创建User.java类,代码如下:
package cn.test.Bean;
public class User {
private long id;
private String name;
private String age;
private String position;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
四、创建User.hbm.xml映射文件,该文件的作用是自动在数据库中建表
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="cn.test.Bean.User" table="User">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<property name="age"></property>
<property name="position"></property>
</class>
</hibernate-mapping>
好了,到这里hibernate的需要基本配置就ok了。
五,创建一个类,保存一条数据至数据库。
package cn.test.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.Test;
import cn.test.Bean.User;
public class TestSave {
private SessionFactory sessionFactory;
@Test
public void SaveUser()
{
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
Session session =sessionFactory.openSession();
User user =new User();
user.setAge("19");
user.setName("李四");
user.setPosition("学生");
session.beginTransaction();//开启事务
session.save(user);
session.getTransaction().commit();//提交事务
session.close();
System.out.println("保存成功!");
}
}
大家主要要注意的就是在 final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();中configure()默认读取的hibernate.cfg.xml路径为项目的src目录下的文件,如果hibernate.cfg.xml
没有直接放在项目src目录下,则需在configure() 中进行配置。否则,会找不到hibernate.cfg.xml
,另外,要使得类中的方法可以单独执行需要添加单元测试@Test。
- 非使用hibernate配置实体类关联数据库表,简单javaBean开发时读取一条数据解决方案
- 使用 Hibernate 将 Java 对象持久保存到 IBM DB2 通用数据库中
- Visual C# 2008+SQL Server 2005 数据库与网络开发--第8章 使用数据绑定和DataSet
- 小学生之Hibernate插入数据修改数据使用数据库默认值的实现
- Hibernate 中把一对多关系的数据保存到数据库
- iOS开发笔记--iOS开发 使用NSUserDefaults 保存数据
- 使用RESTful Jersey框架搭建WebService,Hibernate框架访问数据库,MySQL存储数据
- SpringBoot开发详解(十) -- 使用JPA访问数据库下篇及使用Page进行数据分页
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- 关于使用Yii框架开发时数据库增加字段后保存无效的问题
- Spring+Hibernate DAO 持久层开发, Spring 用 Hibernate 访问数据库的三种方法.推荐使用回调
- 使用WebMagic爬虫框架及javaEE SSH框架将数据保存到数据库(一)
- django中使用mongoengine保存字典结构的数据到数据库。
- Android 使用LitePal将list(String,对象,集合)数据保存到本地数据库
- 问题7:当使用jpa操作数据库的时候,插入数据到数据库使用主键自增策略时候报错:Springboot-jpa Table 'sell.hibernate_sequence' doesn't exist
- [drp 4] 使用dom4j,读取XML数据,保存至数据库
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- 多层数据库开发十二:使用数据控件
- 使用Hibernate向数据库中添加数据
- 多层数据库开发十二:使用数据控件