首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

hibernate最初印象-helloworld的兑现

2012-12-22 
hibernate最初印象---helloworld的实现最近,由于种种原因,开始接触hibernate,今天实现了第一个hibernate的

hibernate最初印象---helloworld的实现

最近,由于种种原因,开始接触hibernate,今天实现了第一个hibernate的入门程序,也就是经典的helloworld,个人喜欢在框架学习的时候,把最基本的helloworld程序的开发过程详细的写下来,以备日后搭建环境时作为参考,步入正题,说说hibernate的helloworld的开发。

以贴图为主,先来看看hibernate工程的目录结构


hibernate最初印象-helloworld的兑现

目录的结构很清晰,额外说一句,我是采用的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 stubStudent 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 logVersionINFO: HHH000412: Hibernate Core {4.0.0.CR5}2011-12-22 10:34:13 org.hibernate.cfg.Environment <clinit>INFO: HHH000206: hibernate.properties not found2011-12-22 10:34:13 org.hibernate.cfg.Environment buildBytecodeProviderINFO: HHH000021: Bytecode provider name : javassist2011-12-22 10:34:13 org.hibernate.cfg.Configuration configureINFO: HHH000043: Configuring from resource: /hibernate.cfg.xml2011-12-22 10:34:13 org.hibernate.cfg.Configuration getConfigurationInputStreamINFO: HHH000040: Configuration resource: /hibernate.cfg.xml2011-12-22 10:34:13 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 addResourceINFO: HHH000221: Reading mappings from resource: com/bjsxt/hibernate/model/Student.hbm.xml2011-12-22 10:34:13 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 doConfigureINFO: HHH000041: Configured SessionFactory: null2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000115: Hibernate connection pool size: 202011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000006: Autocommit mode: false2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: 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 configureINFO: HHH000046: Connection properties: {user=root, password=****}2011-12-22 10:34:14 org.hibernate.dialect.Dialect <init>INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect2011-12-22 10:34:14 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateServiceINFO: HHH000399: Using default transaction strategy (direct JDBC transactions)2011-12-22 10:34:14 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>INFO: HHH000397: Using ASTQueryTranslatorFactoryHibernate: insert into student (name, age, id) values (?, ?, ?)2011-12-22 10:34:14 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stopINFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate]

?

?

?

基本就这些了,做点备注吧

?

首先,本身的目录的结构是约定俗称的一种配置,包括文件的命名其次,hibernate的映射文件,必须保证要数据库中要有相应的表与之对应,或者名称是对应的,或者,自行指定也可以最后,hibernate的映射文件中,id是代表了表的主键值,property是其它的属性值特别提醒:     对于配置文件,最好还是从原始的参考文档中直接复制粘贴,而不要自己敲或者其它什么的,没啥技术含量,而且容易出错
如果在细节上,有什么问题,可以针对性的交流
?

?

?

1 楼 freedxz 2012-05-18   好几个 WARN 不处理一下? 2 楼 mlc0202 2012-05-18   freedxz 写道好几个 WARN 不处理一下?
这个只是当时做的一个入门程序,跑起来看到效果就行 3 楼 qi90mufeng 2012-10-19   刚好我正需要,谢谢了

热点排行