今天开始学习Hibernate3.3.2,虽然之前或多或少的接触到过一点Hibernate的知识,但是这又能说明什么问题呢?什么都不能说明,将自己调试的helloworld程序贴上来,希望和大家一起分享,共同进步。
数据库:mysql
sql语句:
create database hibernate;
use hibernate;
create table student(
id int primary key auto_increment,
name varchar(20),
age int
);
student.java
package com.model;
public class student {
private int id;
private int age;
private String name;
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
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="com.model.student">
<id name="id" />
<property name="name" />
<property name="age" />
</class>
</hibernate-mapping>
StudentTest.java
package com.model.test;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;
import com.model.student;
public class StudentTest {
public static void main(String[] args){
student stu = new student();
// stu.setId(4);
stu.setAge(10);
stu.setName("li");
Configuration conf = new Configuration();
SessionFactory sf = conf.configure().buildSessionFactory();
Session sess = sf.openSession();
sess.beginTransaction();
sess.save(stu);
sess.getTransaction().commit();
sess.close();
sf.close();
}
}
hibernate.cfg.xml存放于src根目录下
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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">123456</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.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>
<!--<mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/>-->
<mapping resource="com/model/student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
- 大小: 42.2 KB
分享到:
相关推荐
一个简单的hibernate helloworld 下载下来就能用
用Spring MVC+hibernate 写的一个HelloWorld小实例,加入了一个简单验证。
hibernate的helloworld版。下载下来可以直接运行。
Hibernate教程01_Hibernate的HelloWorld及基本配置 http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
Hibernate 4——Hello World
hibernate-helloworld-native
Hibernate HelloWorld 6 1 建立新java 项目,名为hibernate_0100_HelloWorld 6 2 学习建User-library-hibernate,并加入相应的jar包 6 3 引入mysql的JDBC驱动包 6 4 在mysql中建立对应的数据库以及表 6 5 建立...
如果你没学过hibernate,但是又想看一下它的最简单形式。这个应该适合你。
建立新的java项目 名为:hibernate_0100_HelloWorld 2. 学习建立User-hibernate 并加入相应的包 a)项目右键build path-configure build path-add library b)选择User-library,在其中新建library 命名为hibernate...
第6课 第一个示例Hibernate HelloWorld 7 第7课 建立Annotation版本的HellWorld 9 第8课 什么是O/R Mapping 11 一、 定义: 11 二、 Hibernate的创始人: 11 三、 Hibernate做什么: 12 四、 Hibernate存在的原因: ...
Hibernate 4.0版helloworld,项目需要Maven管理库依赖,项目包含Oracle建表语句
NULL 博文链接:https://huangminwen.iteye.com/blog/1312068
版本差异太难搞了,找半天才找到错误,主要是包冲突 NoSuchMethodError: org.jboss....NoClassDefFoundError: org/hibernate/cache/spi/RegionFactory org.hibernate.cache.spi.RegionFactory 记录下来,以备不时
Hibernate01-简介和HelloWorld 例子代码,实现了简单新增,测试类需要引进junit4
ssh_helloworld(初学参考),初学者学习的例子,供学习参考
Hibernate_HelloWorld 使用Netbeans和SQL Server的HelloWorld Hibernate
Hibernate_annotation_学习笔记 第1课 课程内容 6 第2课 Hibernate UML图 6 第3课 风格 7 ...第6课 第一个示例Hibernate HelloWorld 7 第7课 建立Annotation版本的HellWorld 9 第8课 什么是O/R Mapping 11 ....
hibernate 使用及与oracle连接的方式
自己学习JSF, Spring, Hiebernate的 HelloWorld程序,开发环境MyEclipse,含有Tomahawk、RichFaces部分集成实例。具体包括登录、注册、树控件、日历等。可用作平时学习之用! 由于大小的限制,缺少的包请到相关...
这是hibernate 的超级简单的例子,只有一个持久化对象和一个辅助类,还有一个测试类,对于初学者很有参考价值