hibernate学习笔记1
来源:互联网 发布:cpu跑分用什么软件 编辑:程序博客网 时间:2024/05/01 22:27
今天开始hibernate框架的学习
1.新建一个项目
2.引入hibernate依赖的库及sqlServer数据库驱动
* hibernate/lib下所有的jar包
* hibernate根下的hibernate3.jar
* sqlServer的jdbc驱动
3.创建hibernate配置文件:hibernate_cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="show_sql">true</property> <property name="myeclipse.connection.profile">asdf</property> <property name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=SAMPLEDB</property> <property name="connection.username">sa</property> <property name="connection.password">123</property> <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property> <mapping resource="com/hibernate/libw/User1.hbm.xml"/> </session-factory>
</hibernate-configuration>
4.定义实体类user1 并创建user1.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.hibernate.libw.User1"> <id name="id"> <generator class="uuid"/> </id> <property name="name"/> <property name="password"/> <property name="createTime"/> <property name="expireTime"/> </class> </hibernate-mapping>
5.将user1.hbm.xml添加到hibernate_cfg.xml中
6.创建数据库表,利用hibernate的工具类,将实体类映射导出到数据库
public class ExportDB {
public static void main(String[] args) { // TODO Auto-generated method stub
Configuration cfg=new Configuration().configure(); SchemaExport export=new SchemaExport(cfg); export.create(true, true); }
}
7.测试:
public class UserTest {
public static void main(String[] args) { // TODO Auto-generated method stub
//读取配置文件
Configuration cfg=new Configuration().configure();
//创建sessionFactory SessionFactory factory=cfg.buildSessionFactory();
//获取session Session session=factory.openSession();
//开启事务 session.beginTransaction();
User1 user=new User1();
user.setName("libaowei"); user.setPassword("123456"); user.setCreateTime(new Date()); user.setExpireTime(new Date()); //保存数据 session.save(user); //提交事务 session.getTransaction().commit(); //关闭session if(session.isOpen()) { session.close(); } }
}
- hibernate学习笔记1
- Hibernate学习笔记(1)
- Hibernate 学习笔记 1
- hibernate学习笔记1
- Hibernate学习笔记1
- Hibernate学习笔记1
- Hibernate学习笔记1
- hibernate学习笔记-1
- hibernate学习笔记(1)
- hibernate学习笔记 1
- Hibernate学习笔记(1)
- Hibernate学习笔记1
- hibernate学习笔记1
- Hibernate学习笔记1
- Hibernate学习笔记1
- hibernate学习笔记1
- hibernate学习笔记(1)
- Hibernate学习笔记1
- 如何为网页添加背景音乐
- Listener的使用
- Linux虚拟机下如何共享ADSL拨号上网
- This application is currently offline. To enable the application, remove the app_offline.htm
- 杂志
- hibernate学习笔记1
- Fedora 9 系统语言设置为英文后不能使用SCIM的解决
- 什么样的人最幸福?
- SOA是成长型ERP的救星吗
- java数据库设计中的14个技巧
- 寻找知识的源头
- 我可以编程序了吗?
- 解析#pragma指令
- 多数据库分页原理介绍