bibnernate(2)
2. 实例代码
新建一个java工程,假设取名为HibernateHelloWorld。在src下新那一个package,可取名为com.sun.hibernate.model
2.1 类代码
新建一个简单的类,放在com.sun.hibernate.model包下。内容如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package com.sun.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; }} |
2.2 配置hibernate配置文件
hibernate\projec\etc中的hiberante.cfg.xml可以作为hibernate的配置文档,或者可使用hibernate\documentation\manual\en-US\html_single\index.html作为模板。在src文件夹下新建一个文件,并命名为hibernate.cfg.xml。(不可命名为其他文件名)最基础的配置文件可参考如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
<?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/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</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> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Enable Hibernate's automatic session context management --> <!--<property name="current_session_context_class">thread</property>--> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">create</property> --> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <mapping resource="com/sun/hibernate/model/Student.hbm.xml"/> </session-factory></hibernate-configuration> |
mapping resource处的值可根据包名和类名做修改。若类名为Student,则此处的类配置文件必为:Student.hbm.xml。
2.3 类mapping文件
新建一个文件,命名为Student.hbm.xml,放在com.sun.hibernate.model包下。内容如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
<?xml version="1.0" encoding="UTF-8"?><!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.sun.hibernate.model"> <class name="Student"> <id name="id"></id> <property name="name"></property> <property name="age"></property> </class></hibernate-mapping> |
注意文件开始处的配置,此处与hibernate.cfg.xml不一样。如果配置的与hiberante.cfg.xml一样,运行时会提示错误:“文档根元素 "hibernate-mapping" 必须匹配 DOCTYPE 根 "hibernate-configuration" ”
2.4 StudentTest测试类
新增Student.java的junit测试类StudentTest.java,放在com.sun.hibernate.model包下代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.sun.hibernate.model;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public class StudentTest { public static void main(String[] args){ Student s = new Student(); s.setId(1); s.setName("s1"); s.setAge(1); Configuration cfg = new Configuration(); SessionFactory sf = cfg.configure().buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); session.close(); sf.close(); }} |
2.5. 运行结果
运行StudentTest.java这个类,虽然提示输入成功。去数据库查询后,可发现数据已存储到student数据表中。虽然myeclipse会提示buildSessionFactory()这个函数被deprecated,但实际上程序还是可以运行成功的。
bibnernate(2)的更多相关文章
随机推荐
- begin-end语句块在mysql中的使用问题
在最近在通过navicate连接mysql数据库时,进行查询操作: delimiter $$BEGIN SET @a=1; if (@a > 0) THEN SELECT COUNT(*) fr ...
- [MAC]2015款MACBOOK使用BOOTCAMP安装WIN8.1+多分区
注意事项: 2013年以前,带光驱的,请使用WinClone安装WIN7或WIN8,或可使用BOOTCAMP制作WINDOWS安装光盘 13-14年,不带光驱的,也能使用WinClone安装WIN7和 ...
- VC 使用OnCtlColor函数来改变控件颜色(引用)
在MFC类库提供了CWnd::OnCtlColor函数,在工作框架的子窗口被重画时将调用该成员函数.因此可以重载WM_CTLCOLOR消息的响应函数.此函数的原型:afx_msg HBRUSH OnC ...
- C# 导入EXCEL 报错外部表不是预期的格式错误 .
错误经过:在读取Excel时,出现外部表不是预期的格式 错误原因1: 由于Excel 97-2003的连接格式与Excel 2010 的 不同造成. 以下是从网上摘抄原文 Excel “Externa ...
- 关于js中的for(var in)遍历属性报错问题
之前遇到过这个问题,但是没找到问题的所在,将for(var i in array){} 改成了for(var i ;i<array.length;i++)循环,但是今天又遇到了,mark一下错 ...
- oracle 空间大小查询
一.表空间大小 select b.file_name 物理文件名, b.tablespace_name 表空间, b.bytes / 1024 / 1024 大小M, (b.bytes - sum(n ...
- 关于GC的几篇文章
http://msdn.microsoft.com/zh-cn/magazine/bb985010(en-us).aspx http://msdn.microsoft.com/zh-cn/magazi ...
- Java 对文件的读取操作
package pack; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; ...
- SeekBar 圆角问题
用图片做背景色,最后处理成.9.png的.用普通png图片做背景,则两边会有圆角出现,原因是图片不适合SeekBar尺寸,因而被拉伸或压缩,从而产生圆角. <?xml version=" ...
- Git相关知识
一些有用的链接: https://www.git-scm.com/ http://nvie.com/posts/a-successful-git-branching-model/ Git开发模式: 建 ...