首页 > 代码库 > ssh整合注解
ssh整合注解
第一步 web.xml
添加struts2的过滤器和spring的监听器
<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>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/applicationContext*.xml</param-value>
</context-param>
第二步 application.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver"/>
<property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/ssh_demo2"/>
<property name="username" value="http://www.mamicode.com/root"/>
<property name="password" value="http://www.mamicode.com/123456"/>
</bean>
<!-- 配置sessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- <property name="mappingDirectoryLocations">
<array>
<value>classpath:com/luo/cn/bean</value>
</array>
</property> -->
<!-- 自动扫描实体 -->
<property name="packagesToScan">
<array>
<value>com.luo.cn.bean</value>
</array>
</property>
</bean>
<bean id="txMgr" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txMgr"/>
<!-- 自动扫描装配bean -->
<context:component-scan base-package="com.luo.cn"/>
</beans>
第三步 建立实体,以及对应的action service dao
@Service用于标注业务层组件, @Controller用于标注控制层组件(如struts中的action),@Controller默认产生的Bean的name就是类(UserAction)的第一个字母小写(userAction)。 当然你也可以自己设定啊,@Controller("uu") @Repository用于标注数据访问组件,即DAO组件, @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
ssh整合注解