1、hibernate必备jar包
antlr-2.7.6.jar
asm.jar
c3p0-0.9.0.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.2.jar
hibernate3.jar
junit-4.10.jar
log4j-1.2.11.jar
jta.jar
mysql-connector-java-5.1.14.jar
2、hibernate必须配置文件
hibernate.cfg.xml
log4j.properties
并且将这两个文件放在src目录下
3、通过通过Bean和hbm.xml配置文件生成数据表
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(false, true);
4、实例HelloWord
(1)配置hibernate.cfg.xml文件
<hibernate-configuration>
<session-factory name="foo">
<!-- 数据库连接配置信息 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///hibernate_test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 数据库连接池配置信息 -->
<property name="hibernate.c3p0.max_size">2</property>
<property name="hibernate.c3p0.min_size">2</property>
<property name="hibernate.c3p0.timeout">5000</property>
<property name="hibernate.c3p0.max_statements">100</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 映射hbm.xml配置文件 -->
<mapping resource="com/uwaysoft/hibernate/bean/Message.hbm.xml"/>
</session-factory>
</hibernate-configuration>
(2)hbm.xml配置文件
<hibernate-mapping package="com.uwaysoft.hibernate.bean">
<class name="Message" table="t_message">
<id name="id">
<!-- id自增 -->
<generator class="increment"/>
</id>
<property name="text" length="100" not-null="true"/>
<many-to-one name="nextMessage" cascade="all" column="next_id"
foreign-key="fk_next_message"></many-to-one>
</class>
</hibernate-mapping>
(3)在mysql数据库中创建数据库hibernate_test
create database hibernate_test
(4)自动生成数据表
public static void schemaExport(){
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(false, true);
}
(5)保存数据
/***
* sessionFactory
*/
private SessionFactory factory = new Configuration().configure().buildSessionFactory();
/**
* 保存对象
* @param message
*/
public void insert(Message message){
/** 获得session **/
Session session = factory.openSession();
/** 开启事务 **/
Transaction transaction = session.beginTransaction();
/** 保存数据 **/
session.persist(message);
/** 提交事务 **/
transaction.commit();
/** 关闭session **/
session.close();
}
附件:由于jar太多,上传附件中不包含jar包
分享到:
相关推荐
hibernate helloword annotation version
spark-windows helloword入门例子 spark-windows helloword入门例子
适合初学struts2者使用,不正确之处请指正
NULL 博文链接:https://java110eye.iteye.com/blog/968942
ubuntu编译中的第一个内核,菜鸟中的解释部分,包含helloworld。哈哈
storm1.2.1-helloword storm1.2.1-helloword storm1.2.1-helloword storm1.2.1-helloword
storm1.2.1-helloword可靠消息storm1.2.1-helloword可靠消息storm1.2.1-helloword可靠消息storm1.2.1-helloword可靠消息
NULL 博文链接:https://javaeedevelop.iteye.com/blog/1478359
FISCO BCOS JAVA SDK(以HelloWorld合约为例)源代码
一个小练习,Hibernate HelloWord,仅供初学者使用。 使用的是:hibernate-release-4.3.11.Final
NULL 博文链接:https://kingding.iteye.com/blog/2205228
springboot入门helloworld教程;springboot自带tomcat所以IDEA打包无法启动本篇code重点描述tomcat外置启动。
gef的入门程序helloword 详细讲解说明见博客 http://blog.csdn.net/wm198966/article/details/9813615
vuepro入门案例-Todolist(helloword级别) 项目难点: X.vue实例中template: '<App/>',这样写是什么意思 X.什么是localStorage、sessionStorage; X.watch:{}用法;
gn-helloword.tar.gz是使用gn进行编译的测试程序,此程序中包含了gn编译的相关设置和说明,为后续学习gn提供参考。
NULL 博文链接:https://liuzidong.iteye.com/blog/591365
SprongBorr HelloWord的建立,到成功发布的流程,是一个很小的Demo
java代码-HelloWord