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.ContextLoaderListener
org.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
<!--新加入 datasource --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/hjzgg_test?characterEncoding=UTF-8" /><property name="username" value="root" /><property name="password" value="hjzgg5211314" /></bean><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="configLocation"value="classpath:hibernate.cfg.xml"></property><!--新加入 将dataSource注入到sessionFactory中 --><property name="dataSource" ref="dataSource"></property><property name="hibernateProperties"><value>hibernate.dialect=org.hibernate.dialect.MySQL5Dialecthibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=falsehibernate.cache.use_second_level_cache=truehibernate.cache.use_query_cache=falsehibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider hibernate.current_session_context_class=thread <!-- 解决Hibernate4 No Session found for current thread -->
</value> </property>
8.新建PersonDao(DAO层),它的实现类PersonDaoImpl,以及 Action类 LoginAction
在applicationContext.xml中添加如下代码:
<bean id="personDao" class="com.xunchang.PersonDaoImpl"><!-- 采用依赖注入传入SessionFactory的引用 --><property name="sessionFactory" ref="sessionFactory"/></bean><bean id="login" class="com.xunchang.LoginAction"> <property name="personDao" ref="personDao"/></bean>
9.POJO 与 hibernate层:新建POJO(普通java类, 对于每一个变量拥有getter 和 setter方法),新建 映射hibernate持久化类person.cfg.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.xunchang"><class name="Person" table="home402"><!-- 定义持久化类的表示属性 --><id name="personId" column="personId" type="java.lang.Integer"><!-- 定义主键生成策略 --><generator class="identity"/></id><property name="person_name" column="person_name" type="java.lang.String"/><property name="person_age" column="person_age" type="java.lang.Integer"/></class> </hibernate-mapping>
然后在applicationContext.xml中的sessionFactory(bean)中加载该映射文件如下:
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="configLocation"value="classpath:hibernate.cfg.xml"></property><!--新加入 将dataSource注入到sessionFactory中 -->.............<!--通过配置文件的方式获取数据源--><property name="mappingResources"><list><!-- 以下用来列出所有的PO映射文件 --><value>person.cfg.xml</value></list></property> </bean>
10.运行服务器,问题又来了,如下:
org.hibernate.HibernateException: No Session found for current threadorg.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原因: http://www.yihaomen.com/article/java/466.htm
解决方法:在applicationContext.xml中的sessionFactory 中<property name="hibernateProperties"></property>加入 hibernate.current_session_context_class=thread
<property name="hibernateProperties"><value>......hibernate.current_session_context_class=thread <!-- 解决Hibernate4 No Session found for current thread --></value> </property>
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>