public class User {
private String id;
private String name;
private String password;
private Date createTime;
private Date expireTime;
public User(){}
// getter and setter methods
// .....
}
<hibernate-mapping>
<class name="org.darkness.hibernate.User">
<id name="id">
<generator class="uuid"/>
</id>
<property name="name"/>
<property name="password"/>
<property name="createTime"/>
<property name="expireTime"/>
</class>
</hibernate-mapping>
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
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);
}
}
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 = null;
try {
session = factory.openSession();
//开启事务
session.beginTransaction();
User user = new User();
user.setName("张三");
user.setPassword("123");
user.setCreateTime(new Date());
user.setExpireTime(new Date());
//保存数据
session.save(user);
//提交事务
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
//回滚事务
session.getTransaction().rollback();
}finally {
if (session != null) {
if (session.isOpen()) {
//关闭session
session.close();
}
}
}
}
}
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate_first</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">depravedAngel</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="org/darkness/hibernate/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
分享到:
相关推荐
Hibernate入门到精通,分享Hibernate的干货,带你入门,走向精通。
解压后包含hibernate入门所需jar (hibernate常用jar,mysql驱动,Junit4jar)
Hibernate入门案例,提供MyEclipse和Eclipse的项目源文件以及war文件
hibernate入门
Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门笔记Hibernate入门...
总共分5部分:1和2为hibernate入门,3为hibernate实体映射,4为hibernate复合主键,5为hibernate实体层设计.
hibernate入门小例子,采用mysql(sql-front)数据库
Hibernate入门教程.pdf
hibernate 入门详细操作步骤 包括hibernate包,实例实现文档····
这是一个关于hibernate入门到精通的书,希望可以帮到你
其实Hibernate本身是个独立的框架,它不需要任何web server或application server的支持。然而,大多数的Hibernate入门介绍都加入了很多非Hibernate的东西,看了这个希望能给你点启示!
struts+hibernate入门实例(eclipse版) author:朱千平 phone:13522080786 qq:200896066
hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例 hibernate 入门经典实例
hibernate hibernate入门 hibernate学习 hibernate基础
hibernate入门教程 介绍如何引入Hibernate和搭建
Hibernate入门 - 基础配置,Hibernate入门 - 基础配置
NULL 博文链接:https://moshow.iteye.com/blog/1587008