Hibernate项目建立
来源:互联网 发布:linux 安装ant 编辑:程序博客网 时间:2024/05/22 01:57
继上篇《认识Hibernate》
认识了Hibernate项目以后,下面来搭建一个Hibernate项目。
1、首先,创建一个java项目。
2、创建User Library,加入依赖包。
Windows-> Preferences,然后按下图操作
建好jar包库后,点上图右边按钮(Add JARs)引入jar包如下。
*HIBERNATE_HOME/lib/*.jar
*HIBERNATE_HOME/hibernate3.jar
*加入数据库驱动(mysql驱动)
Project->Properties,然后如下图操作。
3、提供hibernate.cfg.xml文件,完成基本的配置。
<span style="font-size:18px;"><!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_first</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">130427</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> </session-factory></hibernate-configuration></span>
4、建立实体类User.java
<span style="font-size:18px;">package com.tgb.hibernate;import java.sql.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;}}</span>
5、提供User.hbm.xml文件,完成实体类的映射
<span style="font-size:18px;"><?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.tgb.hibernate.User"><id name="id"><generator class="uuid"/><!--写上这个之后,会生成一个32位的字符串 --></id><property name="name"/><!--如果想用别名,那么就用Column="user_name" --><property name="password"/><property name="createTime"/><property name="expireTime"/></class></hibernate-mapping></span>
6、将User.hbm.xml文件加入到hibernate.cfg.xml文件中
<span style="font-size:18px;"><mapping resource="com/tgb/hibernate/User.hbm.xml"/></span>
7、编写工具类ExportDB.java,将hbm生成ddl,也就是hbm2ddl。
<span style="font-size:18px;">package com.tgb.hibernate;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;/** * 将hbm生成ddl * @author David * */public class ExportDB {public static void main(String[] args) {//默认读取hibernate.fig.xml文件Configuration cfg = new Configuration().configure();SchemaExport export = new SchemaExport(cfg);export.create(true, true);}}</span>运行此main方法,自动根据User实体导出数据库表。
8、建立客户端类Client,添加用户数据到mysql
<span style="font-size:18px;">package com.tgb.hibernate;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();//建立SessionFactorySessionFactory factory = cfg.buildSessionFactory();//取得sessionSession session = null;try{session = factory.openSession();//开启事务session.beginTransaction();User user = new User();user.setName("张三");user.setPassword("123");user.setCreateTime(new java.sql.Date(new java.util.Date().getTime()));user.setExpireTime(new java.sql.Date(new java.util.Date().getTime()));//保存User对象session.save(user);//提交事务session.getTransaction().commit();}catch(Exception e){e.printStackTrace();//回滚事务session.getTransaction().rollback();}finally{if(session.isOpen()){//关闭sessionsession.close();}}}}</span>运行此main方法,向数据库表中插入数据
最好在hibernate.cfg.xml中加入如下配置,方便观察hibernate sql生成:
<span style="font-size:18px;"> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property></span>最好加入log4j配置文件,将该配置文件拷贝到src下,便于程序的调试。
小结
此为一个简单的hibernate的项目的建立实例,供大家参考学习!
1 0
- 【Hibernate】helloworld项目建立
- Hibernate项目建立
- Hibernate——建立第一个hibernate项目
- 文章标题 傻瓜式建立第一个Hibernate 项目
- 建立Hibernate总流程
- hibernate建立复合索引
- 搭建Springmvc+spring+hibernate+easyui框架maven版本(一建立maven项目)
- 如何建立第一个hibernate项目以及解决相关出现的问题
- 初学Hibernate,从建立项目,配置文件到实现对单表的增删改查
- 建立Hibernate映射的新方法
- MyEclipse 建立Hibernate以及注解
- [Hibernate]SchemaUpdate建立索引问题
- Hibernate SchemaUpdate建立索引问题
- MyEclipse无法建立Hibernate…
- [Hibernate]SchemaUpdate建立索引问题
- hibernate中表建立不出来
- 建立一个struts项目
- 如何建立项目里程碑
- Java类库1(16.7.12)
- hdu-2818-Building Block-带权并查集
- Servlet生命周期
- JFreeChart中文乱码解决方法
- Unity3D 优化总结
- Hibernate项目建立
- PHP基础知识构造函数与析构函数
- mysql编码问题解决办法
- 对大二一年学习的反思总结
- Other Linker Flags参数 -ObjC、-all_load和-force_load
- 如何快糙好猛地在Windows下编译CAFFE并使用其matlab和python接口
- 【SSH进阶】No result defined for action **的解决方案
- spark-submit工具参数说明
- python matplotlib绘图设置坐标轴刻度、文本