@(SPRing)[Spring, hibernate, struts2, 框架整合]
SSH框架整合基于注解SSH框架整合第一步导入Jar包第二步导入配置文件第三步创建相关的包和类第四步创建界面第五步配置组件注解扫描第六步配置Action注解第七步配置业务层接口和实现类第八步配置实体类和映射注解第九步配置会话工厂第十步配置Hibernate模板第十一步配置事务管理器和事务注解扫描第十二步配置持久层接口和实现类配置OpensessionInView过滤器配置全站编码过滤器
导入的jar包在《SSH框架整合——基于xml配置文件》博文中都有所介绍,这里不再赘述。唯一不同的是,基于注解的SSH整合还需要导入Struts2
注解整合包struts2-convention-plugin-x.x.x.jar
。
db.properties
jdbc.driverClass=com.MySQL.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/testjdbc.user=rootjdbc.passWord=123456applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置外部属性持有对象 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置c3p0数据源--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> </bean></beans>web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- 配置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> <!-- 配置Spring核心监听器 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener></web-app>log4j.properties
### direct log messages to stdout ###log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.errlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### direct messages to file mylog.log ###log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=c/:mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n### set log levels - for more verbose logging change 'info' to 'debug' ###log4j.rootLogger=info, stdoutapplicationContext.xml
中添加<!-- 开启注解扫描 --><context:component-scan base-package="com.pc.crm.web.action"/><context:component-scan base-package="com.pc.crm.*.impl"/>applicationContext.xml
中添加<!-- 配置Hibernate的SessionFactory,无hibernate配置文件 --><bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 配置数据源 --> <property name="dataSource" ref="dataSource"/> <!-- 配置hibernate属性--> <property name="hibernateProperties"> <props> <!-- 数据库的方言 --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <!-- 是否显示SQL语句 --> <prop key="hibernate.show_sql">true</prop> <!-- 是否格式化显示SQL语句 --> <!-- <prop key="hibernate.format_sql">true</prop> --> <!-- 采用何种策略来创建表结构: --> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 配置实体注解扫描 --> <property name="packagesToScan"> <list> <value>com.pc.crm.domain</value> </list> </property></bean>applicationContext.xml
中添加<!-- 配置hibernate模板 --><bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"/></bean>applicationContext.xml
中添加<!-- 配置事务管理器 --><bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/></bean><!-- 配置事务注解扫描 --><tx:annotation-driven transaction-manager="transactionManager"/>web.xml
中添加,注意要放在Struts2
核心过滤器上面<!-- 配置OpenSessionInview过滤器 --><filter> <filter-name>OpenSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class></filter><filter-mapping> <filter-name>OpenSessionInViewFilter</filter-name> <url-pattern>*.action</url-pattern></filter-mapping>web.xml
中添加,注意要放在Struts2
核心过滤器上面<!-- 配置Spring的字符编码过滤器 --><filter> <filter-name>CharacterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param></filter><filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>新闻热点
疑难解答