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

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

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


目录的结构很清晰,额外说一句,我是采用的hibernate4的jar包,具体下载,详见hibernate官网的下载目录,还有我采用的是mysql的数据库,所以,mysql的连接jar文件必不可少

Studnet是一个model,具体实现,参见下面代码

Java代码 复制代码 收藏代码
  1. package com.bjsxt.hibernate.model; 
  2.  
  3. public class Student { 
  4.     private int id; 
  5.     private String name; 
  6.     private int age; 
  7.     public int getId() { 
  8.         return id; 
  9.     } 
  10.     public void setId(int id) { 
  11.         this.id = id; 
  12.     } 
  13.     public String getName() { 
  14.         return name; 
  15.     } 
  16.     public void setName(String name) { 
  17.         this.name = name; 
  18.     } 
  19.     public int getAge() { 
  20.         return age; 
  21.     }  
  22.     public void setAge(int age) { 
  23.         this.age = age; 
  24.     } 
  25.      
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代码 复制代码 收藏代码
  1. <?xml version='1.0'encoding='utf-8'?> 
  2. <!DOCTYPE hibernate-configuration PUBLIC 
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
  5.  
  6. <hibernate-configuration> 
  7.  
  8.     <session-factory> 
  9.  
  10.         <!-- Database connection settings --> 
  11.         <propertyname="connection.driver_class">com.mysql.jdbc.Driver</property> 
  12.         <propertyname="connection.url">jdbc:mysql://localhost:3306/hibernate</property> 
  13.         <propertyname="connection.username">root</property> 
  14.         <propertyname="connection.password">mlc</property> 
  15.  
  16.         <!-- JDBC connection pool (use the built-in) --> 
  17.        <!--   <property name="connection.pool_size">1</property>  --> 
  18.  
  19.         <!-- SQL dialect --> 
  20.         <propertyname="dialect">org.hibernate.dialect.MySQLDialect</property> 
  21.  
  22.         <!-- Enable Hibernate's automatic session context management --> 
  23.         <!--  <property name="current_session_context_class">thread</property> --> 
  24.  
  25.         <!-- Disable the second-level cache  --> 
  26.         <propertyname="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> 
  27.  
  28.         <!-- Echo all executed SQL to stdout --> 
  29.         <propertyname="show_sql">true</property> 
  30.  
  31.         <!-- Drop and re-create the database schema on startup --> 
  32.         <!--  <property name="hbm2ddl.auto">update</property> --> 
  33.  
  34.         <mappingresource="com/bjsxt/hibernate/model/Student.hbm.xml"/> 
  35.  
  36.     </session-factory> 
  37.  
  38. </hibernate-configuration> 

hibernate的映射关系配置文件

   <?xml version="1.0"?>

Java代码 复制代码 收藏代码
  1. <!DOCTYPE hibernate-mapping PUBLIC 
  2.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
  3.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
  4.  
  5.  
  6. <hibernate-mapping package="com.bjsxt.hibernate.model"
  7.  
  8.     <class name="Student" table="student"
  9.         <id name="id"
  10.         </id> 
  11.         <property name="name"/> 
  12.         <property name="age"/> 
  13.     </class
  14.  
  15. </hibernate-mapping> 
<!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类

Java代码 复制代码 收藏代码
  1. package com.bjsxt.hibernate.model; 
  2.  
  3. import org.hibernate.Session; 
  4. import org.hibernate.SessionFactory; 
  5. import org.hibernate.cfg.Configuration; 
  6.  
  7. public class StudentTest { 
  8.  
  9.     /**
  10.      * @param args
  11.      */ 
  12.     public staticvoid main(String[] args) { 
  13.         // TODO Auto-generated method stub 
  14.         Student stu = new Student(); 
  15.         stu.setAge(2); 
  16.         stu.setId(2); 
  17.         stu.setName("mlc"); 
  18.          
  19.         Configuration cf = new  Configuration(); 
  20.         SessionFactory sf = cf.configure().buildSessionFactory(); 
  21.         Session session =sf.openSession(); 
  22.         session.beginTransaction(); 
  23.         session.save(stu); 
  24.         session.getTransaction().commit(); 
  25.         session.close(); 
  26.         sf.close(); 
  27.     } 
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>

Xml代码 复制代码 收藏代码
  1. INFO: HCANN000001: Hibernate Commons Annotations {4.0.0.CR2} 
  2. 2011-12-22 10:34:13 org.hibernate.Version logVersion 
  3. INFO: HHH000412: Hibernate Core {4.0.0.CR5} 
  4. 2011-12-22 10:34:13 org.hibernate.cfg.Environment <clinit> 
  5. INFO: HHH000206: hibernate.properties not found 
  6. 2011-12-22 10:34:13 org.hibernate.cfg.Environment buildBytecodeProvider 
  7. INFO: HHH000021: Bytecode provider name : javassist 
  8. 2011-12-22 10:34:13 org.hibernate.cfg.Configuration configure 
  9. INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
  10. 2011-12-22 10:34:13 org.hibernate.cfg.Configuration getConfigurationInputStream 
  11. INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
  12. 2011-12-22 10:34:13 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity 
  13. 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! 
  14. 2011-12-22 10:34:13 org.hibernate.cfg.Configuration addResource 
  15. INFO: HHH000221: Reading mappings from resource: com/bjsxt/hibernate/model/Student.hbm.xml 
  16. 2011-12-22 10:34:13 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity 
  17. 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! 
  18. 2011-12-22 10:34:13 org.hibernate.cfg.Configuration doConfigure 
  19. INFO: HHH000041: Configured SessionFactory: null 
  20. 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
  21. INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 
  22. 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
  23. INFO: HHH000115: Hibernate connection pool size: 20 
  24. 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
  25. INFO: HHH000006: Autocommit mode: false 
  26. 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
  27. INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate] 
  28. 2011-12-22 10:34:13 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
  29. INFO: HHH000046: Connection properties: {user=root,password=****} 
  30. 2011-12-22 10:34:14 org.hibernate.dialect.Dialect <init> 
  31. INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect 
  32. 2011-12-22 10:34:14 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService 
  33. INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 
  34. 2011-12-22 10:34:14 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory<init> 
  35. INFO: HHH000397: Using ASTQueryTranslatorFactory 
  36. Hibernate: insert into student (name, age, id) values (?, ?, ?) 
  37. 2011-12-22 10:34:14 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop 
  38. INFO: HHH000030: Cleaning up connection pool [jdbc:mysql://localhost:3306/hibernate] 

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

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

特别提醒:
     对于配置文件,最好还是从原始的参考文档中直接复制粘贴,而不要自己敲或者其它什么的,没啥技术含量,而且容易出错
Java代码 复制代码 收藏代码
  1. 如果在细节上,有什么问题,可以针对性的交流 
如果在细节上,有什么问题,可以针对性的交流
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值