hibernate之hello world
来源:互联网 发布:万网域名证书 编辑:程序博客网 时间:2024/04/28 03:57
一,建立工程项目
不论使用idea,eclipse还是eclipse,都只需要建立普通的java项目即可
二,引入类包
写最简单的hibernate代码,只需要hibernate核心jar包即可,去hibernate官网下载最新版本,解压开,将文件夹内部的lib下的required文件夹内部的jar包都拷过来.这些都是需要的.
三,配置hibernate.cfg.xml文件
这个文件,建立在代码文件夹src下.我们不用从头开始写,有一个模板,在刚才的解压包下面的project/etc/下面,将hibernate.cfg.xml拷贝过来,删除session-factory标签内部的内容和它的name属性,下面是一个最简单的初始配置:
<?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> <!-- 这里的数据库链接的设置我们可以在project/etc/下面hibernate.properties里面找到对应各种不同数据库的设置,复制过来修改一下就好了--> <!-- 这是sql方言,这里是转化为mysql方言--> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 设置mysql驱动--> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 设置数据库链接语句--> <property name="connection.url">jdbc:mysql:///hibernate</property> <!-- 设置数据库用户名--> <property name="connection.username">root</property> <!-- 设置数据库密码--> <property name="connection.password">root</property> <!-- 将Student类映射到数据库的方式的配置,看了这个设置我们知道需要写一个Student类--> <mapping resource="com/geekgao/hibernate/model/Student.hbm.xml"/> </session-factory></hibernate-configuration>
亖,写我们的Student类,将他映射到数据库
如第三步配置中写的一样,我们需要一个Student类,映射到数据库,这个类写个简单的,只有一个属性即可.
//只需要属性和属性的get,set方法即可public class Student { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}
现在我们需要写Student.hbm.xml文件将这个类映射到数据库,这个名称最好是写成这样:类名.hbm.xml,这已经是约定俗成的了.这个文件的位置需要和它所映射的类在同一个地址,下面是最简单的一个映射实例:
<?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.geekgao.hibernate.model"> <!-- 类名和他在数据库表中所对应的表明--> <class name="com.geekgao.hibernate.model.Student" table="student"> <!-- id代表主键,name是类中属性名,column代表它在数据库表中所对应的列名--> <id name="name" column="name"/> <!-- 如果还有其他的数据项,用property标签添加--> <!-- eg: <property name="age" column="age"/>--> </class></hibernate-mapping>
五,写测试代码检验
1,让hibernate根据我们的配置自己在数据库中建立表
@Testpublic void creatTable() { Configuration cfg = new Configuration().configure(); SchemaExport se = new SchemaExport(cfg); //第一个true:是否将建立表的语言打印出来 //第二个true:是否真实的在数据库中创建表 se.create(true,true);}
2,插入数据到表中
@Testpublic void insertData() { Configuration cfg = new Configuration().configure(); StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()); ServiceRegistry service = ssrb.build(); SessionFactory factory = cfg.buildSessionFactory(service); Session session = factory.openSession(); Transaction tx = session.beginTransaction(); Student s1 = new Student(); s1.setName("mangseng"); s1.setAge(23); try { //存储 session.save(s1); //提交事务 tx.commit(); } catch (Exception e) { //事务不成功就回滚 tx.rollback(); } finally { //关闭session session.close(); }}
以上就是最简单的一个hibernate样例.
0 0
- hibernate之Hello world
- hibernate之hello world
- Hibernate之hello world
- Hibernate之Hello World篇
- Hello World之Hibernate版
- hibernate的hello world
- Hibernate:Hello World
- Hibernate hello world
- Hibernate初学者---Hello World
- Spring之Hello World
- Lucene之Hello world
- DWR之HELLO world
- JNI 之 hello world
- ISAPI之Hello world
- Spring之Hello World
- ACE之hello world
- Hello,world之Erlang
- DWR之 Hello world
- dynamic web module
- 【特征检测】LBP特征算法
- 九度 题目1025:最大报销额
- 比较两个数组是否有重复的值
- jvm - 个人理解
- hibernate之hello world
- 南邮 OJ 1157 C - 红宝书
- Kotlin基础语法
- finished with non-zero exit value 42
- 动态规划_DAG例题_The Tower of Babylon(UVA437)
- XIB或者storyboard中嵌入自定义view(xib)的方法
- HDU 5344(2015多校5)-MZL's xor(水题)
- solr学习文档之增量更新索引
- 计算UITableViewCell高度