背景:接上一篇的操作,涉及到查询数据库,所以想把hibernate整合进去(使用hibernate的原因及流程可查看http://www.cnblogs.com/javaNewegg/archive/2011/08/28/2156521.html感觉说的相当好,膜拜)。
首先:导入相关的jar包(将解压后的Hibernate内的lib/required下的jar包和lib/jpa下的jar包复制到项目里的lib目录下)
第二:创建hibernate连接的数据源配置文件hibernate.cfg.xml放在src目录下和sruts2.xml同级
<?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://localhost:3306/malltest</property>
<property name="connection.username">cdy</property>
<property name="connection.password">cdy</property>
<!-- JDBC connection pool (use the built-in) -->
<!-- <property name="connection.pool_size">1</property> -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<!-- <property name="current_session_context_class">thread</property> -->
<!-- Disable the second-level cache -->
<!-- <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> -->
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>
</hibernate-configuration>
第三:创建与数据库表对应的beans类(Goods.java)以及描述对应关系的xml(Goods.hbm.xml)文件,关于数据库类型与hibernate类型的对应可以参照文章http://blog.csdn.net/javacoffe/article/details/1667224
Goods.java
package com.eelly.imagesearch.beans;
public class Goods {
/* 商品id */
private int goods_id;
/* 默认封面图 */
private String default_image;
/* 是否上架 */
private int if_show;
public int getIf_show() {
return if_show;
}
public void setIf_show(int if_show) {
this.if_show = if_show;
}
public int getGoods_id() {
return goods_id;
}
public void setGoods_id(int goods_id) {
this.goods_id = goods_id;
}
public String getDefault_image() {
return default_image;
}
public void setDefault_image(String default_image) {
this.default_image = default_image;
}
}
Goods.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.eelly.imagesearch.beans">
<class name="Goods" table="ecm_goods">
<id name="goods_id" column="goods_id">
<generator class="native"></generator>
</id>
<property name="default_image" column="default_image" type="java.lang.String"
not-null="true" length="255">
</property>
<property name="if_show" column="if_show" type="java.lang.Integer"
not-null="true" length="3">
</property>
</class>
</hibernate-mapping>
第四:将配置的映射关系添加到
hibernate.cfg.xml中,并创建session创建工厂类HibernateSessionFactory.java文件(整合spring的时候此文件可以删除)
1.在hibernate.cfg.xml文件</session-factory>节点内添加<mapping resource="com/eelly/imagesearch/beans/Goods.hbm.xml"/>
2.HibernateSessionFactory.java
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static final String CFG_FILE_LOCATION = "/Hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static final Configuration cfg = new Configuration()
.configure(CFG_FILE_LOCATION);
private static SessionFactory sessionFactory;
public static Session currentSession() throws HibernateException {
Session session = threadLocal.get();
if (session == null || session.isOpen() == false) {
if (sessionFactory == null) {
try {
sessionFactory = cfg.buildSessionFactory();
} catch (Exception e) {
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession() throws HibernateException {
Session session = threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
}
最后:编写dao接口ImgSearchDao以及相应的接口daoImpl实现类ImgSearchDaoImpl
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import com.eelly.imagesearch.beans.Goods;
public interface ImgSearchDao {
public List<Goods> imgSearch() throws HibernateException;
public Session getSession();
public void setSession(Session session);
}
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.eelly.imagesearch.beans.Goods;
import com.eelly.imagesearch.dao.ImgSearchDao;
public class ImgSearchDaoImpl implements ImgSearchDao {
private Session session;
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
@Override
public List<Goods> imgSearch() {
Session session = HibernateSessionFactory.currentSession();
// 获取事务
session.beginTransaction();
List<Goods> list = null;
try {
Query query=session.createQuery("from Goods as g where g.if_show=1");
list = query.list();
session.getTransaction().commit();
// 关闭Session
HibernateSessionFactory.closeSession();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
最后:在Action中调用daoImpl实现类(没有整合spring进来,又省略了service层所以此处用了最简单的new来调用)
ImgSearchDaoImpl isd = new ImgSearchDaoImpl();
// 获取数据库商品相关图片信息,作为创建图片库索引的源数据
List<Goods> goods = isd.imgSearch();
总结:这个配置的流程主要参照的还是
http://blog.csdn.net/yeohcooller/article/details/9316923
配置过程中报了一些错误,影响较深的一个是Failed to load class "org.slf4j.impl.StaticLoggerBinder"这个,谷歌之后下载了最新的相应jar包放入就可以了,详细的可以查看http://modiliany.iteye.com/blog/815441