首先導入三大框架全部的jia包,放這裏了http://pan.baidu.com/s/1mgjxACKhtml
包含了sqlserver和oracle驅動jar包。java
接下來是配置文件:web
web.xml:spring
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 加載全部application-*.xml --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath*:application-*.xml </param-value> </context-param> <!-- 添加spring的啓動器 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 設置字符集亂碼的filter ,注意放在struts2的過濾器的前面--> <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> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 關於數據庫session在VIEW層顯示後關閉的過濾器 它容許在事務提交以後延遲加載顯示所須要的對象。 --> <filter> <filter-name>openSessionInViewFilter</filter-name> <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>openSessionInViewFilter</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>
接下來的配置文件放在src目錄下:sql
application-config.xml 這是spring的配置文件,管理了hibernate:數據庫
<?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/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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置由spring管理的hibernate的sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"><!-- mappingResources --> <list> <value>com.kjlink.crm.service.entity.CstService</value> <value>com.kjlink.crm.service.entity.CstCustomer</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.SQLServerDialect hibernate.show_sql=true hibernate.format_sql=true hibernate.hbm2ddl.auto=update </value> </property> </bean> <!-- 加入spring 的事務支持 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="get*" read-only="true" /> <tx:method name="*" /> </tx:attributes> </tx:advice> <!-- 配置spring的事務管理器 --> <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> <!-- <property name="sessionFactory" ref="sessionFactorySale"/> --> </bean> <!-- 配置鏈接點 --> <aop:config> <aop:pointcut id="serviceOperation" expression="execution(* com.kjlink.crm.*.service.*.*(..))" /> <aop:advisor pointcut-ref="serviceOperation" advice-ref="txAdvice" /> </aop:config> <!-- 配置dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 鏈接池啓動時的初始值 --> <property name="initialSize" value="10" /> <!-- 鏈接池的最大值 --> <property name="maxActive" value="100" /> <!-- 最大空閒值.當通過一個高峯時間後,鏈接池能夠慢慢將已經用不到的鏈接慢慢釋放一部分,一直減小到maxIdle爲止 --> <property name="maxIdle" value="50" /> <!-- 最小空閒值.當空閒的鏈接數少於閥值時,鏈接池就會預申請去一些鏈接,以避免洪峯來時來不及申請 --> <property name="minIdle" value="10" /> </bean> <context:property-placeholder location="classpath*:jdbc.properties" /> </beans>
application-service.xm,這是l針對service模塊獨立出來的配置文件,開發時,各自維護本身的配置文件,方便開發,下降耦合:express
<?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/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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="serviceDao" class="com.kjlink.crm.service.dao.imp.ServiceDaoImp"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <bean id="serviceService" class="com.kjlink.crm.service.service.imp.ServiceServiceImp"> <property name="serviceDao" ref="serviceDao" /> </bean> <bean id="serviceAction" class="com.kjlink.crm.service.action.ServiceAction" scope="prototype"> <property name="serviceService" ref="serviceService"/> </bean> </beans>
jdbc.properties,數據庫鏈接的初始參數,數據庫名稱爲 haitun:apache
jdbc.url=jdbc:sqlserver://localhost:1433; DatabaseName=haitun #jdbc:oracle:thin:@192.168.70.55:1521:orcl jdbc.username=sa jdbc.password=sqlserver jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
struts.xml,Struts2的配置文件:tomcat
<?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 name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- value="true"時修改配置後,沒必要重啓tomcat --> <constant name="struts.devMode" value="false" /> <!-- 對擴展名爲何樣的文件 .action .do .空 .kjlink --> <constant name="struts.action.extension" value="action,do,,kjlink" /> <!-- 由spring來管理struts2 --> <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" /> <package name="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <global-results> <result name="error">/error.jsp</result> </global-results> <action name="index"> <result>index.jsp</result> </action> </package> <include file="service.xml"></include> </struts>
包含的service.xml:session
<?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> <package name="service" namespace="/" extends="struts-default"> <action name="serviceAction" class="serviceAction" > <result name="success">/WEB-INF/jsp/service/welcome.jsp</result> </action> </package> </struts>
三大框架的核心就這些配置文件,至於java文件和頁面,那都已經很簡單了:
直接放這裏面了,一個個貼出來沒意思,都是函數調用: http://pan.baidu.com/s/1eQGJOrw
最後,兩個頁面:index.jsp:
<body> <a href="serviceAction!add?service.svrCustName=lyj">添加</a> </body>
welcome.jsp:
<body> 添加成功 <br> </body>
數據庫建好庫,tomocat啓動時,會自動生成表,這是sqlserver生成的:
點擊頁面的「添加」,兩張表會各添加進一條數據,關係爲多對一,service多,customer一。