参考:http://blog.youkuaiyun.com/dsl815721035/article/details/52938084
http://blog.youkuaiyun.com/lfsf802/article/details/7357215
搭建hibernate环境
自动搭建
1、启动myeclipse,进入window->Open Perspective->Myeclipse Hibernate
2、在DB Brower区间右键选择New,创建数据连接源,在弹出的对话框中填写相应的信息
右键->open connection就可以连接数据库,连接成功之后可以看到所有数据库:
3、回到myeclipse工作空间,新建工程(我建的是web工程)然后进行如下操作:
4、选择版本,选择4.1
5、next
6、next
7、next
至此,hibernate环境搭建好了
手动搭建
自动搭建缺少灵活性,可以采用手动搭建
1、先下载hibernate包(我的是hibernate5.1.5),在lib/required目录中含有使用hibernate所必须的包
把这些包放到我们项目的lib目录,并且add to Build path(一般还要添加mysql驱动包)
2、在下载的hibernate包中找到一个hibernate.cfg.xml文件,介绍一下hibernate.cfg.xml内容,包含三方面的内容:
(1)连接数据库的信息
(2) 自己的属性配置
(3) 映射文件的位置
这里把hibernate.cfg.xml文件复制到我们项目中,做一些修改
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<!-- <property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost/TestDB</property> -->
<!--数据库驱动名字-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--数据库连接url-->
<property name="connection.url">jdbc:mysql://127.0.0.1:3306/sale</property>
<!--数据库用户名-->
<property name="connection.username">root</property>
<!--密码-->
<property name="connection.password">1234</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL 方言 -->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
使用hibernate
1、在使用之前修改一下hibernate.cfg.xml,增加以下几行代码
第一句是因为我后面有了getCurrentSession()获得session,后面两句目的是便于观察hibernate sql的生成
<property name="current_session_context_class">thread</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
2、自动生成类与文件的映射
在这里我以数据库sale中的表items举例,右击items表并选中…
弹出一个框,进行一下操作:
然后next,Id Generator选择identity,其他选择默认值
next之后然后finish,MyEclipse会自动生成Items.java和Items.hbm.xml两个文件
3、在Hibernate应用中,如果只使用一个数据库,则通常只需要一个SessionFactory对象。为了方便整个应用取得同一个SessionFactory对象,我们应用设计模式中的单态模式。编写一个SessionFactory的工具类HibernateSessionFactoryUtil,HibernateSessionFactoryUtil.java文件的内容如下
public class HibernateSessionFactoryUtil {
private static SessionFactory sessionFactory;
private static Session session;
static {
//创建Configuration对象,读取hibernate.cfg.xml文件,完成初始化
Configuration cof = new Configuration().configure();
//ServiceRegistry 是 Service 的注册表, 它为Service提供了一个统一的加载 /初始化 /存放 /获取机制.
ServiceRegistryBuilder ssrb = new ServiceRegistryBuilder()
.applySettings(cof.getProperties());
ServiceRegistry ssr=ssrb.buildServiceRegistry();
sessionFactory=cof.buildSessionFactory(ssr);
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
public static Session getSession(){
session=sessionFactory.openSession();
return session;
}
public static void closeSession(Session session){
if(session!=null)
session.close();
}
}
对其进行测试如下:
public class HibernateTest {
public void addItems(Items item){
Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
session.save(item);
HibernateSessionFactoryUtil.closeSession(session);
tx.commit();
}
public Items getItems(Integer id){
Session session=HibernateSessionFactory.getSession();
Transaction tx=session.beginTransaction();
Items items=(Items) session.get(Items.class, id);
HibernateSessionFactoryUtil.closeSession(session);
tx.commit();
return items;
}
public List<Items> getItems(){
Session session=HibernateSessionFactoryUtil.getSession();
Transaction tx=session.beginTransaction();
Query query=session.createQuery("from items");
List<Items> list=query.list();
HibernateSessionFactoryUtil.closeSession(session);
tx.commit();
return list;
}
public void add(Items item)
{
Session session=HibernateSessionFactoryUtil.getSession();
Transaction tx=session.beginTransaction();
session.save(item);
HibernateSessionFactoryUtil.closeSession(session);
tx.commit();
}
public void printItems(Items item){
System.out.println("id"+item.getId());
System.out.println("name"+item.getName());
System.out.println("city"+item.getCity());
}
public static void main(String[] args) {
HibernateTest test=new HibernateTest();
Items item=test.getItems(new Integer(1));
System.out.println("读取单一记录");
test.printItems(item);
}
}
测试结果:
Hibernate:
select
items0_.id as id0_0_,
items0_.name as name0_0_,
items0_.city as city0_0_,
items0_.price as price0_0_,
items0_.number as number0_0_,
items0_.picture as picture0_0_
from
sale.items items0_
where
items0_.id=?
读取单一记录
id1
name沃特篮球鞋
city佛山
后面的就是数据库中的内容