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)的更多相关文章
随机推荐
- window10系统安装SQL数据库和小蝴蝶的问题
最近刚刚升了windows10系统.由于以前一直使用的是SQL2008数据库,所以也就没有下载最新的数据库,但是在安装的过程中一直提示让重启,重启了很多回也没有用. 在启动SQL2008安装程序的时候 ...
- SQL事务
一.事务概念 事务是一种机制.是一种操作序列,它包含了一组数据库操作命令,这组命令要么全部执行,要么全部不执行.因此事务是一个不可分割的工作逻辑单元.在数据库系统上执行并发操作时事务是作为最小的 ...
- 宏碁台式机,如何设置u盘启动
1.按delete进入BIOS2.Authentication->Secure Boot状态改为Disabled;Boot Options->Launch CSM状态改为Always;Bo ...
- web初学之JavaBean
JavaBean 1.javaBean类:特殊的java类,以封装和重用为目的.通常JavaBean与Jsp搭配使用构成JavaBean技术. (1)JavaBean的规范: ●JavaBean是个公 ...
- 源码编译Nodejs 4.6 on CentOS6
1.本来是要按照个newman程序,无奈newman必须要node版本>=4,所以不得不安装node4.6 下载地址:https://nodejs.org/dist/v4.6.0/node-v4 ...
- spring mvc 4.3.2 + mybatis 3.4.1 + mysql 5.7.14 +shiro 幼儿园收费系统 之 动态组合条件查询
实际应用中,系统设计无法预料到用户最终的查询条件是怎样的.通常的做法是给出一些限制死的查询条件让用户查询.业务稍有改动,就要重新设计界面,增加查询字段等,费时费力. 比较好的做法是,除了常用的查询外, ...
- NPOI支持excel2003和excel2007
IWorkbook wk = null; if (filePath.ToLower() == ".xls") { wk = new HSSFWorkbook(fs); } else ...
- ssh config host
Host mygithub HostName github.com User git IdentityFile ~/.ssh/github/id_rsa IdentitiesOnly yes
- OpenLDAP,一登录系统就修改密码
http://guodayong.blog.51cto.com/263451/d-2 郭大勇的博客 1:修改配置文件 在前面打开注释 moduleload ppolicy.la modulepat ...
- 三、HTTP抓包测试
package testHTTP; import java.io.BufferedReader;import java.io.IOException;import java.io.InputStrea ...