Hibernate框架的搭建和第一个简单的实例
来源:互联网 发布:sql2005数据库补丁 编辑:程序博客网 时间:2024/06/11 09:49
Hibernate是一个支持对JDBC进行封装的框架,实现了对底层数据库访问的封装。非常适合使用和开发。首先需要下
载Hibernate,可以在这个网站下载最新包。http://www.hibernate.org/然后打开他的目录结构,将lib目录下的required目
录下的包全部导入到工程中去,这个是hibernate运行所必须的最少的包。
然后写一个Bean,将需要储存到数据库中的变量封装成Bean。为了让Hibernate识别这个bean,需要一个配置文
件,这里起名叫User.hbm.xml。先看一下User的代码和User.hbm.xml的代码
package com.bird.domain;import java.util.Date;public class User {private int id;private String name;private Date birthday;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Date getBirthday() {return birthday;}public void setBirthday(Date birthday) {this.birthday = birthday;}}
<?xml version="1.0" ?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.bird.domain"> <class name="User"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <property name="birthday"/> </class> </hibernate-mapping>
然后需要一个Hibernate的配置文件,这个文件的例子可以再Hibenate解压目录的project里面的ect目录里面找到。更加
详细的配置选项和要求可以参考hibernate.properties.template这个文件.
<?xml version='1.0' encoding='utf-8'?><!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property><property name="hibernate.connection.url">jdbc:mysql:///test</property><property name="hibernate.connection.username">root</property><property name="hibernate.connection.password">mysql</property><property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property><property name="hibernate.hbm2ddl.auto">update</property><mapping resource="com/bird/domain/User.hbm.xml"/></session-factory></hibernate-configuration>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>这句话的意思是指定你使用的数
据库的方言.
<property name="hibernate.hbm2ddl.auto">update</property>这句话的意思是自动创建或者更改数据库里面的表或
者表的内容结构
<mapping resource="com/bird/domain/User.hbm.xml"/>这句话的意思是要求装载这个类映射文件
下面就可以运行这个了,记住,别忘了导入Mysql的Connection的Jar包。
package com.bird.hibernate.test;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import com.bird.domain.User;public class Base {/** * @param args */public static void main(String[] args) {Configuration cfg = new Configuration();cfg.configure();@SuppressWarnings("deprecation")SessionFactory sf = cfg.buildSessionFactory();Session s = sf.openSession();Transaction tx = s.beginTransaction();User use = new User();use.setBirthday(new Date());use.setName("bird");s.save(use);tx.commit();s.close();}}
运行结果如下
2012-2-28 12:12:38 org.hibernate.annotations.common.Version <clinit>INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}2012-2-28 12:12:38 org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {4.0.1.Final}2012-2-28 12:12:38 org.hibernate.cfg.Environment <clinit>INFO: HHH000206: hibernate.properties not found2012-2-28 12:12:38 org.hibernate.cfg.Environment buildBytecodeProviderINFO: HHH000021: Bytecode provider name : javassist2012-2-28 12:12:38 org.hibernate.cfg.Configuration configureINFO: HHH000043: Configuring from resource: /hibernate.cfg.xml2012-2-28 12:12:38 org.hibernate.cfg.Configuration getConfigurationInputStreamINFO: HHH000040: Configuration resource: /hibernate.cfg.xml2012-2-28 12:12:38 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/bird/domain/User.hbm.xml2012-2-28 12:12:38 org.hibernate.cfg.Configuration doConfigureINFO: HHH000041: Configured SessionFactory: null2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000115: Hibernate connection pool size: 202012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000006: Autocommit mode: false2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql:///test]2012-2-28 12:12:38 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000046: Connection properties: {user=root, password=****}2012-2-28 12:12:39 org.hibernate.dialect.Dialect <init>INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect2012-2-28 12:12:39 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateServiceINFO: HHH000399: Using default transaction strategy (direct JDBC transactions)2012-2-28 12:12:39 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>INFO: HHH000397: Using ASTQueryTranslatorFactory2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate executeINFO: HHH000228: Running hbm2ddl schema update2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate executeINFO: HHH000102: Fetching database metadata2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate executeINFO: HHH000396: Updating schema2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000261: Table found: test.user2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000037: Columns: [id, birthday, name]2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000108: Foreign keys: []2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000126: Indexes: [primary]2012-2-28 12:12:39 org.hibernate.tool.hbm2ddl.SchemaUpdate executeINFO: HHH000232: Schema update complete
这样就可以了
- Hibernate框架的搭建和第一个简单的实例
- Hibernate框架的搭建和第一个简单的实例
- 《struts2》框架第一个实例的搭建
- Hibernate第一个简单实例
- hibernate框架的简单实例
- Hibernate的基本架构和第一个简单工程(2)
- 第一个成功的Hibernate实例
- 第一个成功的Hibernate实例
- 搭建ror的第一个实例
- Spring---搭建开发环境和第一个简单实例
- Spring---搭建开发环境和第一个简单实例
- spring框架下的第一个实例
- 第一个最简单的Hibernate
- 第一个简单的hibernate程序
- 简单的hibernate示例(第一个)
- 第一个简单的hibernate小例子
- 第一个超简单的hibernate
- Hibernate原理和第一个Hibernate实例
- js时间-价格-排序案例____冒泡排序实例
- 为JBoss7配置MSSQL的DataSource
- ADO 开发指南(程式猎人收集整理)----发现最好的ADO MFC的数据库操作
- C#|.NET从控制反转(依赖注入)想到事件注入 (非AOP)
- HttpClient 教程 (一)
- Hibernate框架的搭建和第一个简单的实例
- HttpClient 教程 (二)
- 以后要慢慢写博客……
- c/c++源码转换
- Android隐藏状态栏和标题栏,相当于全屏效果
- 调用类函数指针引起的编译器错误 C2064
- 循环冗余校验 CRC 原理
- MMORPG游戏的典型架构
- MySQL 分页查询: To SQL_CALC_FOUND_ROWS or not to SQL_CALC_FOUND_ROWS?