最近,由于种种原因,开始接触hibernate,今天实现了第一个hibernate的入门程序,也就是经典的helloworld,个人喜欢在框架学习的时候,把最基本的helloworld程序的开发过程详细的写下来,以备日后搭建环境时作为参考,步入正题,说说hibernate的helloworld的开发。
以贴图为主,先来看看hibernate工程的目录结构
目录的结构很清晰,额外说一句,我是采用的hibernate4的jar包,具体下载,详见hibernate官网的下载目录,还有我采用的是mysql的数据库,所以,mysql的连接jar文件必不可少
Studnet是一个model,具体实现,参见下面代码
package com.bjsxt.hibernate.model;
public class Student {
private int id;
private String name;
private int age;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
hibernate的配置文件
<?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> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">mlc</property> <!-- JDBC connection pool (use the built-in) --> <!-- <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <mapping resource="com/bjsxt/hibernate/model/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
hibernate的映射关系配置文件
<?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.bjsxt.hibernate.model">
<class name="Student" table="student">
<id name="id">
</id>
<property name="name"/>
<property name="age"/>
</class>
</hibernate-mapping>
用于测试hibernate的java类
package com.bjsxt.hibernate.model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class StudentTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu = new Student();
stu.setAge(2);
stu.setId(2);
stu.setName("mlc");
Configuration cf = new Configuration();
SessionFactory sf = cf.configure().buildSessionFactory();
Session session =sf.openSession();
session.beginTransaction();
session.save(stu);
session.getTransaction().commit();
session.close();
sf.close();
}
}
运行结果
2011-12-22 10:34:13 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.0.CR2} 2011-12-22 10:34:13 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.0.0.CR5} 2011-12-22 10:34:13 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 2011-12-22 10:34:13 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 2011-12-22 10:34:13 org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 2011-12-22 10:34:13 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 2011-12-22 10:34:13 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 2011-12-22 10:34:13 org.hibernate.cfg.Configuration addResource INFO: HHH000221: Reading mappings from resource: com/bjsxt/hibernate/model/Student.hbm.xml 2011-12-22 10:34:13 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 2011-12-22 10:34:13 org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 20 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000006: Autocommit mode: false 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate] 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000046: Connection properties: {user=root, password=****} 2011-12-22 10:34:14 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 2011-12-22 10:34:14 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 2011-12-22 10:34:14 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> INFO: HHH000397: Using ASTQueryTranslatorFactory Hibernate: insert into student (name, age, id) values (?, ?, ?) 2011-12-22 10:34:14 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate]
基本就这些了,做点备注吧
首先,本身的目录的结构是约定俗称的一种配置,包括文件的命名
其次,hibernate的映射文件,必须保证要数据库中要有相应的表与之对应,或者名称是对应的,或者,自行指定也可以
最后,hibernate的映射文件中,id是代表了表的主键值,property是其它的属性值
特别提醒:
对于配置文件,最好还是从原始的参考文档中直接复制粘贴,而不要自己敲或者其它什么的,没啥技术含量,而且容易出错
如果在细节上,有什么问题,可以针对性的交流