hibernate-4.3.5安装配置
起初照着官方文档配,一直出错,貌似官方的文档时错的,查了非常多资料,综合整理了一个可行的方案,例如以下:
0.1包结构
test.demo
test.domain //实体类
test.util//工具类
0.2导如的jar包
hibernate-4.3.5的required包中的全部
optional包中的c3p0中的全部
下载slf4j,导入slf4j-api.jar 和 slf4j-log4j.jar
下载log4j, 导入log4j.jar
导入mysql-connector-java.jar
Log4j和slf4j的关系:http://blog.csdn.net/lifuxiangcaohui/article/details/7278595
apache-log4j-1.2.17 http://pan.baidu.com/share/link?shareid=121565833&uk=2047106924
slf4j-1.7.7 http://pan.baidu.com/share/link?shareid=123250651&uk=2047106924
1.POJO例如以下
package test.domain;
public class Message {
private Long id;
private String text;
private Message nextMessage;
public Message(String text){
this.text = text;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Message getNextMessage() {
return nextMessage;
}
public void setNextMessage(Message nextMessage) {
this.nextMessage = nextMessage;
}
}
2.与之相应的映射文件Message.hbm.xml,注意,要与POJO放在一起
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping>
<class name="test.domain.Message" table="MESSAGES">
<id name="id" column="MESSAGE_ID">
<generator class="increment"></generator>
</id>
<property name="text" column="MESSAGE_TEXT"></property>
<many-to-one name="nextMessage" cascade="all" column="NEXT_MESSAGE_ID"
foreign-key="FK_NEXT_MESSAGE"></many-to-one> </class> </hibernate-mapping>
3.hibernate.cfg.xml,即hibernate的配置文件,放在src的根文件夹下
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/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://127.0.0.1:3306/hibernate
</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property> <!-- Use the c3p0 connection pool provider -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property> <!-- Show and print nice SQL on stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property> <property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hbm2ddl.auto">update</property> <!-- List of XML mapping files -->
<mapping resource="test/domain/Message.hbm.xml" /> </session-factory> </hibernate-configuration>
4.HibernateUtil.java工具类
package test.util; import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() {
try {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
SessionFactory sessionFactory = configuration
.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
} public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
5.測试
package test.demo; import org.hibernate.Session; import test.domain.Message;
import test.util.HibernateUtil; public class HelloWorld {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction(); Message message = new Message("Hello,world"); session.save(message);
session.getTransaction().commit();
}
}
hibernate-4.3.5安装配置的更多相关文章
- 概述hibernate入门安装配置
1.jdbc连接的优缺点 JDBC的优点 直接底层操作,提供了很简单.便捷的访问数据库的方法,跨平台性比较强.灵活性比较强,可以写很复杂的SQL语句. JDBC的缺点 1).因为JAVA是面向对象的, ...
- mysql5.7安装配置,常用命令,常见问题
1.安装配置 参考:http://www.cnblogs.com/Fiona20170420/p/6738185.html 1. 下载 2. 解压缩 3. 添加path环境变量,路径指向mysql所在 ...
- 转载maven安装,配置,入门
转载:http://www.cnblogs.com/dcba1112/archive/2011/05/01/2033805.html 本书代码下载 大家可以从我的网站下载本书的代码:http://ww ...
- Hive安装配置指北(含Hive Metastore详解)
个人主页: http://www.linbingdong.com 本文介绍Hive安装配置的整个过程,包括MySQL.Hive及Metastore的安装配置,并分析了Metastore三种配置方式的区 ...
- Hive on Spark安装配置详解(都是坑啊)
个人主页:http://www.linbingdong.com 简书地址:http://www.jianshu.com/p/a7f75b868568 简介 本文主要记录如何安装配置Hive on Sp ...
- ADFS3.0与SharePoint2013安装配置(原创)
现在越来越多的企业使用ADFS作为单点登录,我希望今天的内容能帮助大家了解如何配置ADFS和SharePoint 2013.安装配置SharePoint2013这块就不做具体描述了,今天主要讲一下怎么 ...
- Hadoop的学习--安装配置与使用
安装配置 系统:Ubuntu14.04 java:1.7.0_75 相关资料 官网 下载地址 官网文档 安装 我们需要关闭掉防火墙,命令如下: sudo ufw disable 下载2.6.5的版本, ...
- Intellij Idea 15 下新建 Hibernate 项目以及如何添加配置
1.说明:Idea 下,项目对应于 Eclipse 下的 workspace,Module 对应于 Eclipse 下的项目.Idea 下,新添加的项目既可以单独作为一个 Project,也可以作为一 ...
- redis的安装配置
主要讲下redis的安装配置,以及以服务的方式启动redis 1.下载最新版本的redis-3.0.7 到http://redis.io/download中下载最新版的redis-3.0.7 下载后 ...
- Windows环境下的NodeJS+NPM+Bower安装配置
npm作为一个NodeJS的模块管理,之前我由于没有系统地看资料所以导致安装配置模块的时候走了一大段弯路,所以现在很有必要列出来记录下.我们要先配置npm的全局模块的存放路径以及cache的路径,例如 ...
随机推荐
- BNU10792:沙漠旅行者
有个旅行者计划横穿沙漠,沙漠中水资源很匮乏.旅行者需要依靠补给站的支持,才能横穿整个沙漠.假设所有的补给站都在一条直线上,而且旅行者一定沿着这条直线走.起点在1号补给站,终点在第N号补给站,起点和终点 ...
- REST、SOA、SOAP、RPC、ICE、ESB、BPM知识汇总及理解
转载自处blog.csdn.net/tantexian. SOA: 维基百科解释:SOA:面向服务的软件架构(Service Oriented Architecture),是一种计算机软件的设计模式, ...
- android高仿微信UI点击头像显示大图片效果
用过微信的朋友朋友都见过微信中点击对方头像显示会加载大图,先贴两张图片说明下: 这种UI效果对用户的体验不错,今天突然有了灵感,试着去实现,结果就出来了.. 下面说说我的思路: 1.点击图片时跳转到另 ...
- 【nodejs学习】3.进程管理及异步编程
进程管理 1.调用终端命令实现目录目录拷贝 var child_procress = require('child_procress'); var util = require('util'); fu ...
- C/C++中虚函数的调用
代码: #include <iostream> using namespace std; class A{ public: virtual void print(){ cout<&l ...
- DSP TMS320C6000基础学习(3)——CCS v5软件开发环境搭建
================================================== DSP CCS工程文件构成 =================================== ...
- C#模拟登录的htmlHelper类
public class HTMLHelper { /// <summary> /// 获取CooKie /// /// </summary> /// /// <para ...
- PHP 汉字转拼音(首拼音,所有拼音)
<?php /** +------------------------------------------------------ * PHP 汉字转拼音 +------------------ ...
- Flask学习记录之Flask-Migrate
一.配置Flask-Migrate from flask.ext.migrate import Migrate, MigrateCommand migrate = Migrate(app,db) #第 ...
- “#ifdef __cplusplus extern "C" { #endif”的定义
平时我们在linux c平台开发的时候,引用了一些Cpp或者C的代码库,发现一些头文件有如下代码条件编译. #ifdef __cplusplus extern "C" { #end ...