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)的更多相关文章
随机推荐
- css 去除点击之后的虚线
链接在被点击时会出现虚线框,即使松开了也仍然存在,在有的时候显得不美观.既然不好看,那就不要它.怎样去掉呢? 方法一 IE下可使用其私有的html属性:hideFoucs,在标签的结构中加入hidef ...
- HostOnly Cookie和HttpOnly Cookie
怎么使用Cookie? 通常我们有两种方式给浏览器设置或获取Cookie,分别是HTTP Response Headers中的Set-Cookie Header和HTTP Request Header ...
- innodb buffer pool小解
INNODB维护了一个缓存数据和索引信息到内存的存储区叫做buffer pool,他会将最近访问的数据缓存到缓冲区.通过配置各个buffer pool的参数,我们可以显著提高MySQL的性能. INN ...
- SQL多表联查
left join >>>外联 on >>>跟and一样使用,作为外联条件 表明后面加大写字母表示该表别名 例: select A.*,B.name userNam ...
- 制作一个简洁的jquery插件
原文:http://mp.weixin.qq.com/s?__biz=MzAxMzgwNDU3Mg==&mid=401571467&idx=1&sn=08cb00963e6ef ...
- 【原】相煎何太急——input的blur事件与button的click事件
先来一段引子,最近在写手机h5页面,主要是一些登陆注册方面的,最绕不开的就是表单元素. 我想实现的是:在输入框后边有一个删除图标,当输入东西的时候触发事件,显示删除图标,点击该图标会删除之前输入的内容 ...
- Unity5 AssetBundle 打包以及加载
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; us ...
- C89和C99区别--简单总结
(1)对数组的增强 可变长数组 C99中,程序员声明数组时,数组的维数可以由任一有效的整型表达式确定,包括只在运行时才能确定其值的表达式,这类数组就叫做可变长数组,但是只有局部数组才可以是变长的.可变 ...
- Linux 常用命令 :ls命令
ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下ls用来打印出当前目录的清单如果ls指定其他目录那么就会显示指定目录里的文件及文件夹清单. 通过ls 命令不仅可以查看linu ...
- 常见绘图框架-(Charts)
swift 出来后有很多优秀的第三方绘图.动画框架,最近项目需要使用了 Charts Github: https://github.com/danielgindi/Charts 因为是在Object- ...