菜鸟学习Hibernate——简单的一个例子
来源:互联网 发布:塔夫斯大学 知乎 编辑:程序博客网 时间:2024/04/28 08:46
一、Hibernate开发。
上篇博客已经为大家介绍了持久层框架的发展流程,持久层框架的种类。
为了能够使用Hibernate快速上手,我们先讲解一个简单的Hibernate应用实例hibernate_first。
二、开发流程。
1.首先在MyEclipce中新建一个hibernate_first的项目,然后新建后的项目目录为:
2.配置Hibernate环境。
3.编写持久化类User.java
- package com.bjpowernode.hibernate;
- import java.util.Date;
- public class User {
- private String id;
- private String name;
- private String password;
- private Date createTime;
- private Date expireTime;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public Date getCreateTime() {
- return createTime;
- }
- public void setCreateTime(Date createTime) {
- this.createTime = createTime;
- }
- public Date getExpireTime() {
- return expireTime;
- }
- public void setExpireTime(Date expireTime) {
- this.expireTime = expireTime;
- }
- }
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>
- <class name="com.bjpowernode.hibernate.User">
- <id name="id">
- <generator class="uuid"/>
- </id>
- <property name="name"/>
- <property name="password"/>
- <property name="createTime"/>
- <property name="expireTime"/>
- </class>
- </hibernate-mapping>
5.编写hibernate.cfg.xml文件。
- <!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_frist</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">root</property>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.show_sql">true</property>
- <property name="hibernate.format_sql">true</property>
- <mapping resource="com/bjpowernode/hibernate/User.hbm.xml"/>
- </session-factory>
- </hibernate-configuration>
6.生成表的类ExportDB.java。
- package com.bjpowernode.hibernate;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.tool.hbm2ddl.SchemaExport;
- /**
- * 将hbm生成ddl
- * @author Administrator
- *
- */
- public class ExportDB {
- public static void main(String[] args) {
- //默认读取hibernate.cfg.xml文件
- Configuration cfg = new Configuration().configure();
- SchemaExport export = new SchemaExport(cfg);
- export.create(true, true);
- }
- }
7.以上六个步骤已经把表建起来了下面我们就保存个数据,新建一个Client.java类来存入一个数据。代码如下:
- package com.bjpowernode.hibernate;
- import java.util.Date;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class Client {
- public static void main(String[] args) {
- //读取hibernate.cfg.xml文件
- Configuration cfg = new Configuration().configure();
- //建立SessionFactory
- SessionFactory factory = cfg.buildSessionFactory();
- //取得session
- Session session = null;
- try {
- session = factory.openSession();
- //开启事务
- session.beginTransaction();
- User user = new User();
- user.setName("张三");
- user.setPassword("123");
- user.setCreateTime(new Date());
- user.setExpireTime(new Date());
- //保存User对象
- session.save(user);
- //提交事务
- session.getTransaction().commit();
- }catch(Exception e) {
- e.printStackTrace();
- //回滚事务
- session.getTransaction().rollback();
- }finally {
- if (session != null) {
- if (session.isOpen()) {
- //关闭session
- session.close();
- }
- }
- }
- }
- }
0 0
- 菜鸟学习Hibernate——简单的一个例子
- 菜鸟学习Hibernate——简单的一个例子
- 一个简单的Hibernate例子!
- 一个简单的Hibernate例子
- 一个简单的Hibernate例子
- 一个简单的学习spring+hibernate的例子
- 一个简单的Hibernate例子的心得
- Hibernate Component映射的一个简单例子
- OpenCV学习(2)——一个简单的例子
- 菜鸟学习Hibernate——简单的增、删、改、查操作
- 菜鸟学习Hibernate——简单的增、删、改、查操作
- Hibernate学习笔记——Introduction和简单例子
- Hibernate的简单例子
- Hibernate的简单例子
- 一个Hibernate的例子
- 一个Hibernate的例子
- Hibernate学习笔记(二)——创建一个简单的Hibernate项目
- 一个简单的Struts Hibernate入门例子(上)
- scrapy爬虫之错误 No module named PIL + IOError:decoder jpeg not available +PIL的安装
- css3实现各种角度的三角形
- AFNetwork 2.0在请求时报错code=-1016 和 3840
- 获取站点
- Android Studio SDK 更新方法
- 菜鸟学习Hibernate——简单的一个例子
- android 蓝牙
- leetcode刷题系列C++-3sum
- Android 源码获取-----在Windows环境下通过Git得到Android SDK源代码
- 【提高】面试--三
- Activity初窥门径
- SAP中MM模块采购数据报表(入库,订单)
- [Scala函数特性系列]——按名称传递参数
- 回顾第一次搭建hibernate本机环境