Maven搭建struts2+spring+hibernate环境(二)

上一篇 《Maven 搭建struts2+spring+hibernate环境(一)》

一、修改项目结构

上一篇中我们已经完成了jar的引入,现在开始构建测试程序。刚刚完成的project图标上可能有一个红色的叉,只需要把项目复制–粘贴–重命名。即可解决,并不是项目搭建错误所致,这可能是IDE的一个bug吧,在MyEclipse中搭建也出现这种情况,解决方法一样。

但是,构建的MSSH项目的结构并不是一个标准的Maven结构,我们需要手动新建几个Source Folder(注意:是source folder,不是package!!)。

完成后的结构如下:

src/main.java:存放java源文件

src/main/resources:存放项目配置文件,如spring.xml,hibernate.cfg.xml。。。

src/test/java:存放test的java文件

src/test/resources:存放test时候所需的配置文件

二、编写程序

建立如下的结构

编写配置文件,各种配置的含义我在此就不累赘了,有ssh使用基础的同学都能看懂的。

struts.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" ""><struts><!– 将Action交给spring容器管理 –><constant name="struts.objectFactory" value="spring" /><!– 模式为开发模式,修改xml配置文件会自动加载,项目交付的时候设置为false,以免影响性能 –><constant name="struts.devMode" value="true" /><constant name="struts.configuration.xml.reload" value="true" /><!– 字符集编码 –><constant name="struts.i18n.encoding" value="utf-8" /><package name="defaultPackage" namespace="/" extends="struts-default"><action name="userAction" class="userAction" method="reg"><result name="success">/success.jsp</result><result name="error">/error.jsp</result></action></package></struts>spring.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi="" xmlns:context=""xsi:schemaLocation=" "><!– 引入属性文件 –><context:property-placeholder location="classpath:database.properties" /><!– 自动扫描dao和service包(自动注入) –><context:component-scan base-package="com.sgl.action,com.sgl.dao.impl,com.sgl.service" /></beans>spring-hibernate.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi="" xmlns:tx=""xmlns:aop=""xsi:schemaLocation=" "><!– 使用C3P0数据源,MySQL数据库 –><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close"><!– MySQL5 –><property name="driverClass" value="${driverClassName}"></property><property name="jdbcUrl" value="${url}"></property><property name="user" value="${username}"></property><property name="password" value="${password}"></property><property name="maxPoolSize" value="40"></property><property name="minPoolSize" value="1"></property><property name="initialPoolSize" value="1"></property><property name="maxIdleTime" value="20"></property></bean><!– session工厂 –><bean id="sessionFactory"class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="hibernateProperties"><props><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop></props></property><!– 注解方式配置 –><property name="packagesToScan"><list><value>com.sgl.model</value></list></property></bean><!– 配置事务 –><bean name="txManager"class="org.springframework.orm.hibernate4.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><tx:annotation-driven transaction-manager="txManager" /></beans>database.propertieshibernate.dialect=org.hibernate.dialect.MySQLDialectdriverClassName=com.mysql.jdbc.DrivervalidationQuery=SELECT 1url=jdbc:mysql://localhost:3306/mssh?useUnicode=true&characterEncoding=UTF-8username=rootpassword=sglhibernate.hbm2ddl.auto=updatehibernate.show_sql=truehibernate.format_sql=trueweb.xml<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns=""xmlns:xsi=""xsi:schemaLocation=" "><display-name></display-name><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring.xml,classpath:spring-hibernate.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><init-param><param-name>encoding</param-name><param-value>UTF-8</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!–Hibernate的session丢失解决方法 –><filter><filter-name>openSessionInView</filter-name><filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping><filter-name>openSessionInView</filter-name><url-pattern>/*</url-pattern></filter-mapping><!– struts2配置 –><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>编写java文件,采用MVC架构,使用hibernate,spring注解如果说,罗马是一座厚重和凝固的堡垒,

Maven搭建struts2+spring+hibernate环境(二)

相关文章:

你感兴趣的文章:

标签云: