Hibernate学习第一天 配置环境和helloworld
来源:互联网 发布:淘宝怎么举报刷销量 编辑:程序博客网 时间:2024/06/08 00:33
需要下载的东西
hibernate-annotations-3.4.0.ga.zip
hibernate-distribution-3.3.2.ga-dist.zip
slf4j-1.5.8.zip
参考文档
www.hibernate.org
hibernate-distribution-3.3.2.GA\documentation\manual\zh-CN\html_single\index.html
hibernate-annotations-3.4.0.GA\doc\reference\zh_cn\html_single\index.html
在windows-preferences-java-buildpath-add user libraries
需要的有 hibernate3.jar
lib下的 required下的所有包
和sl4j相对应版本的包 slf4j-nop-1.5.8.jar
和mysql的驱动jar
hibernate-annotation-3.0.4.ga/hibernate-annotation.jar
和lib下的ejb-persistions和 hibernate-commons-annotation.jar
在mysql中建立对应的数据库表
简历hibernate配置文件 hibernate.cfg.xml
在hibernate文档中拷贝
<propertyname="connection.driver_class">com.mysql.jdbc.Driver</property>
<propertyname="connection.url">jdbc:mysql://localhost/hibernate</property>
<propertyname="connection.username">root</property>
<propertyname="connection.password">dongjialin</property>
连接数据库
<propertyname="dialect">org.hibernate.dialect.MySQLDialect</property>
Mysql的方言
<propertyname="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
create 可以自动创建数据库没有的表 update 数据库或者实体类改变可以自动生成新表 create-drop 关闭session后自动drop validate 插数据之前检查数据库中的表和文件是否能适应上
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
不设置二级缓存
<!-- Echo all executed SQL to stdout -->
<propertyname="show_sql">true</property>
显示sql执行语言
如果一个实体类 Student.java
也需要在同一个包下建立一个 student.hbm.xml
<?xmlversion="1.0"?>
<!DOCTYPEhibernate-mappingPUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mappingpackage="edu.neusoft">
<classname="Student">
<idname="id"></id>
<propertyname="name"></property>
<propertyname="age"></property>
</class>
</hibernate-mapping>
Id主键 property属性名
如果实体类和表中的名字不一致 ,在class后加table=”” 数据库中表名
在property里加 column=””数据库中行名
然后在hibernate.cfg.xml的session-factory标签里加
<mappingresource="edu/neusoft/Student.hbm.xml"/>
主函数中写
Student s = new Student();//new一个实体类\
s.setId(1);
s.setName("s1");
s.setAge(23);
Configuration cfg=newConfiguration();//新建一个configuration
SessionFactory sf =cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(s);
session.getTransaction().commit();
session.close();
sf.close();
Annotations
建立一个新的实体类 Teacher和对应的数据库表
privateintid;
private Stringname;
private Stringtitle;
在类前加@Entity表明这是一个实体类
在主键get方法上@Id
在hibernate-cfg.xml中加
<mapping class="edu.neusoft.Teacher"/>
注意是.
主方法中
Teacher t = new Teacher();
t.setId(1);
t.setName("name1");
t.setTitle("primary");
Configuration cfg = new AnnotationConfiguration();
SessionFactory sf =cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
sf.close();
- Hibernate学习第一天 配置环境和helloworld
- GTK3.0学习第一天---环境配置
- Hibernate 学习第一天
- Hibernate学习第一天
- hibernate学习第一天
- hibernate学习第一天——配置hibernate
- JAVA学习:第一阶段第一天(java基本概念和配置环境)
- 木瓜妮子多媒体开发教程---第一天---Android环境搭建和Helloworld项目测试
- Struts+hibernate学习第一天
- hibernate学习第一天总结
- hibernate学习第一天笔记
- Hibernate学习(第一天)--CURD
- Hibernate学习(第一天)
- 爬虫第一天:环境配置
- JSF2 学习日志,第一天,环境配置&hello world
- object-c 学习第一天(windows编译环境配置)
- 黑马程序员--Java基础学习(环境配置)第一天
- hadoop学习的第一天---配置linux的jdk环境
- opencv中facedetect例子浅析
- 在Visual Studio 2010中配置VC++目录
- 纯代码布局
- C#网络编程系列九:类似QQ的即时通信程序
- Variable Modifiers [变量调节器]
- Hibernate学习第一天 配置环境和helloworld
- c#获取当前路径的方法大全
- java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to XXX解决
- 关于#if和#ifdef对于编译的区别
- Ubuntu10.04 下架设tftp服务器
- Js验证(邮箱、手机、QQ号等) .
- 在Ubuntu中通过update-alternatives切换软件版本
- 【总结】2012年,很水的一年
- Windows 下 Eclipse+Maven插件的安装