1.新建一个web project
2.首先生成Hibernate Facet
3.Hibernate Facet 安装步骤
4.然后是spring facet安装步骤
5.最后是struts facet 的配置
6.最后的整体布局如下所示
7.在服务器上运行,发现如下错误:
严重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.service.UnknownUnwrapTypeException: Cannot unwrap to requested type [javax.sql.DataSource] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) ........
主要是没有在applicationContext.xml中配置 DataSource 这个bean,配置好后将其注入sessionFactory
hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=false hibernate.cache.use_second_level_cache=true hibernate.cache.use_query_cache=false hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.current_session_context_class=thread
8.新建PersonDao(DAO层),它的实现类PersonDaoImpl,以及 Action类 LoginAction
在applicationContext.xml中添加如下代码:
9.POJO 与 hibernate层:新建POJO(普通java类, 对于每一个变量拥有getter 和 setter方法),新建 映射hibernate持久化类person.cfg.xml文件如下:
然后在applicationContext.xml中的sessionFactory(bean)中加载该映射文件如下:
....... ......
person.cfg.xml
10.运行服务器,问题又来了,如下:
org.hibernate.HibernateException: No Session found for current thread org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041) com.xunchang.PersonDaoImpl.getSession(PersonDaoImpl.java:16) com.xunchang.PersonDaoImpl.findAllPerson(PersonDaoImpl.java:63) com.xunchang.LoginAction.execute(LoginAction.java:20) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Hibernate4 No Session found for current thread原因:
解决方法:在applicationContext.xml中的sessionFactory 中<property name="hibernateProperties"></property>加入 hibernate.current_session_context_class=thread
...... hibernate.current_session_context_class=thread
11.最后一个问题就是hibernate 中文乱码,解决方案如下:
//写一个字符集过滤器 import java.io.IOException;import javax.servlet.Filter;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.http.HttpServlet;public class SetCharacterEncodingFilter extends HttpServlet implements Filter { public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); chain.doFilter(request, response); } public void init(FilterConfig config) throws ServletException{ } public void destroy(){ } } //在web.xml中加入如下代码,问题解决
<filter>
<filter-name>encodingFilter</filter-name> <filter-class>com.xunchang.SetCharacterEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>