1.2 hibernate一个简单例子
2015-11-26 14:51
274 查看
1。数据库准备
在booknet数据库的表student中建立两个字段。
2。在MyEclipse中的代码文件架构如下:
3。hibernate.cfg.xml内容如下
<?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">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://localhost:49196;DatabaseName=booknet</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="myeclipse.connection.profile">myjdbcdriver</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</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="com/hibernate/util/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.student.class代码为:
package com.hibernate.util;
public class Student {
private String name;
private int lian;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the lian
*/
public int getLian() {
return lian;
}
/**
* @param lian the lian to set
*/
public void setLian(int lian) {
this.lian = lian;
}
}
5.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 package="com.hibernate.util">
<class name="Student" >
<id name="name" column="name"/>
<property name="lian" column="lian"/>
</class>
</hibernate-mapping>
6.hibernate例子测试,测试代码在StudentTest类中,代码为:
package com.hibernate.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class StudentTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s =new Student();
s.setName("p5");
s.setLian(25);
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();
}
}
在booknet数据库的表student中建立两个字段。
2。在MyEclipse中的代码文件架构如下:
3。hibernate.cfg.xml内容如下
<?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">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://localhost:49196;DatabaseName=booknet</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="myeclipse.connection.profile">myjdbcdriver</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</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="com/hibernate/util/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.student.class代码为:
package com.hibernate.util;
public class Student {
private String name;
private int lian;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the lian
*/
public int getLian() {
return lian;
}
/**
* @param lian the lian to set
*/
public void setLian(int lian) {
this.lian = lian;
}
}
5.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 package="com.hibernate.util">
<class name="Student" >
<id name="name" column="name"/>
<property name="lian" column="lian"/>
</class>
</hibernate-mapping>
6.hibernate例子测试,测试代码在StudentTest类中,代码为:
package com.hibernate.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class StudentTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s =new Student();
s.setName("p5");
s.setLian(25);
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();
}
}
相关文章推荐
- 表单包含file上传文件提交,使用ajaxSubmint或者ajaxForm 遇到的问题以及解决方法
- poj 1388 Hinge Node Problem floyd算法的扩展运用
- 有两个不同版本的jQuery库,冲突的解决办法。
- js实现延时加载Flash的方法
- android删除未使用的资源文件
- Android:完美解决 — 表情键盘与输入法键盘显示冲突方案
- Ivy管理项目,希望对大家有所帮助
- iOS 数据持久化 -- Core Data
- java file to inputstream以及New Document From InputStream读取和写入文件操作实例代码
- spring的aop
- Bootstrap tabs选项卡实现
- 安装jad仍不能反编译的问题
- 折腾:瀑布流布局(基于多栏列表流体布局实现)
- C++重载<<和>>(输入输出运算符)
- Android开发总结笔记 SearchView&Search(搜索相关) 1-3-7
- linux svn 服务器搭建
- MSDN 学习网站
- Objective-C官方文档翻译 Block
- AbpKernelModule
- php or || 和 and &&