一个Hibernate的Hello World, 基于Hibernate 4.0
2012-01-18 18:39
330 查看
先在官网上下载最近的Hibernate完整包, 目前最新的是hibernate-release-4.0.0.Final http://www.hibernate.org/downloads .
打开Myeclipse, 新建一个JavaProject.
1.导入Hibernate需要的Jar包.
需要加入的包有:hibernate-release-4.0.0.Final里面Lib目录下Required的所有Jar包.
2.加入Hibernate配置文件hibernate.cfg.xml, 文件内容可参考文档(这里使用的是么Mysql):
3.新建一个实体类Student, 并成长get, set方法:
4.在数据库中建好对应的表Student, 有id, name ,age三个字段.
5.编写实体与关系的映射文件:Student.hbm.xml
6.在hibernate.cfg.xml配置文件中加入Student.hbm.xml:在 </session-factory> 之前加入:<mapping resource="jyu/hibernate/model/Student.hbm.xml" />
7.测试:StudentTest
运行, 可以查看到数据库中已经插入了一条记录.
这种方法是比较常用的使用XML来映射实体与关系的方法, 另有使用Annotation(注解)方式也能完成映射功能.
本文为尚学堂马士兵Hibernate视频教程笔记.
打开Myeclipse, 新建一个JavaProject.
1.导入Hibernate需要的Jar包.
需要加入的包有:hibernate-release-4.0.0.Final里面Lib目录下Required的所有Jar包.
2.加入Hibernate配置文件hibernate.cfg.xml, 文件内容可参考文档(这里使用的是么Mysql):
<?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> <!-- 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">用户名</property> <property name="connection.password">密码</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.internal.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>--> <property name="myeclipse.connection.profile"> Hibernate_Test </property> <mapping resource="jyu/hibernate/model/Student.hbm.xml" /> <mapping class="jyu.hibernate.model.Teacher" /> </session-factory> </hibernate-configuration>
3.新建一个实体类Student, 并成长get, set方法:
package jyu.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; } }
4.在数据库中建好对应的表Student, 有id, name ,age三个字段.
5.编写实体与关系的映射文件:Student.hbm.xml
<?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="jyu.hibernate.model.Student"> <id name="id" column="id"/> <property name="name" /> <property name="age" /> </class> </hibernate-mapping>
6.在hibernate.cfg.xml配置文件中加入Student.hbm.xml:在 </session-factory> 之前加入:<mapping resource="jyu/hibernate/model/Student.hbm.xml" />
7.测试:StudentTest
package jyu.hibernate.model; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class StudentTest { public static void main(String[] agrs){ Student st = new Student(); st.setId(2); st.setName("学生"); st.setAge(20); Configuration cfg = new Configuration(); //读取配置hibernate文件 SessionFactory sf = cfg.configure().buildSessionFactory(); //根据配置文件生成事务工厂 Session session = sf.openSession(); session.beginTransaction(); //事务开启 session.save(st); //保存对象 session.getTransaction().commit(); session.close(); sf.close(); } }
运行, 可以查看到数据库中已经插入了一条记录.
这种方法是比较常用的使用XML来映射实体与关系的方法, 另有使用Annotation(注解)方式也能完成映射功能.
本文为尚学堂马士兵Hibernate视频教程笔记.
相关文章推荐
- 基于Hibernate 4.0 的Hello World
- Hibernate是一个基于对象模型持久化的技术
- springMVC用法 以及一个简单的基于springMVC hibernate spring的配置
- 用Maven如何构建一个基于Hibernate Annotation的web项目?
- Smobiler 4.0 是一个基于.Net的企业APP开发管理平台
- 对基于Spring-Structs-Hibernate做的一个小项目的理解
- 搭建基于Windows 7 的Beagleboard开发平台: 编写一个可在Beagleboard上运行的Hello World
- 如何用cocos2d-x来做一个基于TileMap地图块的游戏 (一)
- 一个基于jquery的页面预载入效果(loading)
- 基于 vue2 + vuex 构建一个具有 45 个页面的大型单页面应用 他人代码,借鉴
- Spring+Hibernate+Struts技术的一个分页程序
- 实现一个基于Ajax的调查程序
- 一个基于jquery的页面预载入效果(loading)
- OSGI企业应用开发(十五)基于Spring、Mybatis、Spring MVC实现一个登录应用
- 一个基于Socket协议的手机聊天室源码
- 一个基于protocol buffer的RPC实现
- 嵌入式 一个基于linux的聊天软件(并发服务器,sqlite3数据库,有注释)
- DIY一个基于树莓派和Python的无人机视觉跟踪系统
- 在一个spring配置文件中同时配置hibernate和jdbc
- 如何创建一个Tab bar Application (xcode 4.0或4.1中的方式)