某位大神曾经教导过我,能粘贴复制的就别装逼手写,能抄的就抄,不然迟早把自己写死了,写完了还出了bug,改半天还没有收获,以下代码:
1、applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "> <!-- 注解扫描 --> <context:component-scan base-package="com.iteason"></context:component-scan> <!-- 配置c3p0连接池 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"></bean> <!-- 核心事务管理器 --> <bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> <tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true" /> <tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" /> </tx:attributes> </tx:advice> <!-- 织入切点 --> <aop:config> <aop:pointcut expression="execution(* com.iteason.serviceimp.*ServiceImp.*(..))" id="txPc"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/> </aop:config> <!-- 开启注解事务 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 将sessionFactory配置到spring容器中 --> <!-- 加载配置 方案1、使用外部的hibernatr.cfg.xml配置信息--> <!-- <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> --> <!-- 加载配置 方案2、在spring容器中配置 --> <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <!-- 将连接池注入到sessionFactory中 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置基本信息 --> <property name="hibernateProperties"> <props> <!-- <prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop> <prop key="hibernate.connection.url">jdbc:mysql:///ssh_crm</prop> <prop key="hibernate.connection.username">root</prop> <prop key="hibernate.connection.password">123</prop> --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> <!-- 引入orm元数据 ,指定orm元数据所在的包路径即可--> <property name="mappingDirectoryLocations" value="classpath:com/iteason/domain"></property> </bean> <!-- 配置dao --> <bean name="userDao" class="com.iteason.daoimp.UserDaoImp"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置service --> <bean name="customerService" class="com.iteason.serviceimp.CustomerServiceImp"> <property name="cd" ref="customerDao"></property> </bean> <!-- 配置action--> <bean name="userAction" class="com.iteason.web.action.UserAction" scope="prototype"></bean> </beans>
2、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"> <display-name>ssh_crm</display-name> <!-- 配置spirng随项目的启动而启动 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置spring配置文件位置参数 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 扩大session作用范围 --> <filter> <filter-name>openSessionInView</filter-name> <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInView</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- struts2核心过滤器 --> <!-- 配置strur2核心过滤器 --> <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.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
3、struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- constant配置常量配置 国际化 解决乱码 --> <constant name="struts.i18n.encoding" value="UTF-8"></constant> <!-- 配置开发者常量 1、热部署 2、提供更多的错误提示 --> <constant name="struts.devMode" value="true"></constant> <!-- 动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!-- 将struts2交给spring管理 --> <constant name="struts.objectFactory" value="spring"></constant> <!-- class不要再以类路径了,用applicationContext.xml中配置的bean名 --> <package name="crm" namespace="/" extends="struts-default"> <global-exception-mappings> <exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping> </global-exception-mappings> <action name="UserAction_*" class="userAction" method="{1}" > <result name="success" >/hello.jsp</result> </action> </package> </struts>
4、c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///ssh_crm</property> <property name="user">root</property> <property name="password">123</property> <property name="initialPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> <named-config name="oracle"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///web_07</property> <property name="user">root</property> <property name="password">123</property> </named-config> </c3p0-config>