hibernate操作数据库例子
1、工程目录结构如下
2、引入需要的jar包,如上图。
3、创建持久化类User对应数据库中的user表
package com.hibernate.配置文件.pojo;
import java.sql.Date;
public class User {
private Integer id;
private String username;
private String password;
private Date update_time;
public User() {
super();
}
public User( String username, String password, Date update_time) {
super();
this.username = username;
this.password = password;
this.update_time = update_time;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getUpdate_time() {
return update_time;
}
public void setUpdate_time(Date update_time) {
this.update_time = update_time;
}
}
4、创建持久化类对应的配置文件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 package="com.hibernate.配置文件.pojo">
<class name="User" table="user" dynamic-insert="true">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
<generator class="native" />
</id> <property name="username" type="java.lang.String" column="username"/>
<property name="password" type="java.lang.String" column="password"/>
<property name="update_time" type="date">
<column name="update_time" />
</property>
</class>
</hibernate-mapping>
5、创建hibernate的配置文件hibernate.cfg.xml
<?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">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://127.0.0.1/hibernate</property> <!-- 配置hibernate的基本信息 -->
<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 执行操作时是否在控制台打印 SQL -->
<property name="show_sql">true</property> <!-- 是否对 SQL 进行格式化 -->
<property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property> <!-- 指定关联的.hbm.xml文件 -->
<mapping resource="com/hibernate/配置文件/pojo/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
6、创建SessionFactoryUtil,单例模式,用于生成SessionFactory对象
package com.hibernate.配置文件.util; import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder; public class SessionFactoryUtil {
private SessionFactoryUtil(){}
private static SessionFactory sessionFactory = null; public static SessionFactory getSessionFactory() { if(sessionFactory == null){
/**
* 1、 Configuration 类负责管理 Hibernate 的配置信息。包括如下内容: Hibernate
* 运行的底层信息:数据库的URL、用户名、密码、JDBC驱动类,数据库Dialect,数据库连接池等(对应
* hibernate.cfg.xml 文件)。 持久化类与数据表的映射关系(*.hbm.xml 文件)
*/
Configuration configeration = new Configuration().configure(); /**
* 2、 创建一个 ServiceRegistry 对象: hibernate 4.x 新添加的对象 hibernate
* 的任何配置和服务都需要在该对象中注册后才能有效.
*/
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configeration.getProperties())
.buildServiceRegistry();
/**
* 3、创建SessionFactory对象
*/
sessionFactory = configeration.buildSessionFactory(serviceRegistry);
}
return sessionFactory; }
}
7、测试代码
package com.hibernate.配置文件.service.impl; import java.sql.Date; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction; import com.hibernate.配置文件.pojo.User;
import com.hibernate.配置文件.service.HibernateService;
import com.hibernate.配置文件.util.SessionFactoryUtil; public class HibernateServiceImpl implements HibernateService { public void hibernateTest(){
//1、创建SessionFactory对象
SessionFactory sessionFactory = SessionFactoryUtil.getSessionFactory(); //2、创建session
Session session = sessionFactory.openSession(); //3、开启事务
Transaction tran = session.beginTransaction(); //4、执行持久化操作
User user = new User("lvyf","123456",new Date(new java.util.Date().getTime()));
session.save(user); //5、提交事务
tran.commit(); //6、关闭session
session.close(); //7、关闭sessionFactory
sessionFactory.close();
} public static void main(String[] args) {
HibernateServiceImpl hs = new HibernateServiceImpl(); hs.hibernateTest();
}
}
~H1SN(D_EW25~[2K{GPBM.png)
~H1SN(D_EW25~[2K{GPBM.png)
hibernate操作数据库例子的更多相关文章
- hibernate操作数据库总结
这篇文章用于总结hibernate操作数据库的各种方法 一.query方式 1.hibernate使用原生态的sql语句执行数据库查询 有些时候有些开发人员总觉得用hql语句不踏实,程序出现了错误,就 ...
- Java_Web三大框架之Hibernate操作数据库(三)
使用Hibernate操作数据库需要七个步骤: (1)读取并解析配置文件 Configuration conf = newConfiguration().configure(); (2)读取并解析映射 ...
- hibernate操作数据库总结(转)
一.query方式 1.hibernate使用原生态的sql语句执行数据库查询 有些时候有些开发人员总觉得用hql语句不踏实,程序出现了错误,就猜测因为不是原生态的sql语句,数据库不支持,因此情愿选 ...
- Hibernate操作数据库的回调机制--Callback
1:一般情况下,在使用Hibernate Session存取数据库的代码中,基本上大部分是相同的,如下两个方法所示, //查询Teacher操作 ublic Teacher getTeacher ...
- Hibernate 操作数据库
com.tao.pojo实体类 package com.tao.pojo; public class User { private int id; private String name; priva ...
- 转 使用Hibernate操作数据库时报:No CurrentSessionContext configured! 异常
没有currentSession配置错误,即在我们使用currentSession的时候要在hibernate.cfg.xml中进行相关的事务配置:1.本地事务<property name=&q ...
- hibernate操作数据库时报错解决方式
java.sql.SQLException: Parameter index out of range (28 > number of parameters, which is 27). 这个说 ...
- Spring MVC基础知识整理➣Spring+SpringMVC+Hibernate整合操作数据库
概述 Hibernate是一款优秀的ORM框架,能够连接并操作数据库,包括保存和修改数据.Spring MVC是Java的web框架,能够将Hibernate集成进去,完成数据的CRUD.Hibern ...
- 5 -- Hibernate的基本用法 --2 2 Hibernate的数据库操作
在所有的ORM框架中有一个非常重要的媒介 : PO(持久化对象:Persistent Object).持久化对象的作用是完成持久化操作,简单地说,通过该对象可对数据执行增.删.改的操作 ------ ...
随机推荐
- python的闭包与装饰器
原文发表在我的博客主页,转载请注明出处 前言 如果把python当作脚本语言,每次就是写个几十行上百行来处理数据的话,装饰器也许不是很必要,但是如果要开发一个大型系统,装饰器是躲不开的,最开始体会ry ...
- 如何编写Iveely搜索引擎插件
如果一个搜索引擎仅仅是网页搜索,那么将会是非常枯燥的,也不能根据业务需求扩展,还好Iveely在设计之初,就考虑了扩展性,预留插件功能,在不关闭服务或者停用服务的情况下,可以随时启用新插件或者禁用. ...
- Zigbee技术特点
ZigBee工作原理 基于 ZigBee 的无线设备工作在 868MHZ, 915MHZ 和 2.4Z 频带.其最大数据速 率是 250Kbps. ZigBee 技术主要针对以电池为电源的应用,这些应 ...
- OWIN-WebAPI-Windows Service
tks: https://github.com/danesparza/OWIN-WebAPI-Service add 2015 0717:http://kb.cnblogs.com/page/5092 ...
- Linq之延迟加载特性
目录 写在前面 系列文章 延迟加载 总结 写在前面 上篇文章介绍了linq中常见的几个关键字,并列举了几个例子,算是对linq如何使用有了初步了解.上篇文章中也提到了,能够使用linq的场合有一个要求 ...
- hdu2222 AC自动机
字典树也可以做. #include<stdio.h> #include<string.h> #include<stdlib.h> #define maxn 1000 ...
- codeforces 719B:Anatoly and Cockroaches
Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...
- 洛谷P2320 [HNOI2006]鬼谷子的钱袋
https://www.luogu.org/problem/show?pid=2320#sub 题目描述全是图 数学思维,分治思想 假设总数为n 从n/2+1到n的数都可以用1~n的数+n/2表示出来 ...
- 洛谷P2731骑马修栅栏
题目背景 Farmer John每年有很多栅栏要修理.他总是骑着马穿过每一个栅栏并修复它破损的地方. 题目描述 John是一个与其他农民一样懒的人.他讨厌骑马,因此从来不两次经过一个栅栏.你必须编一个 ...
- Jquery CDN
新浪CDN <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></ ...