首先导入三大框架所有的jia包,放这里了http://pan.baidu.com/s/1mgjxACK
包含了sqlserver和oracle驱动jar包。
接下来是配置文件:
web.xml:
<?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目录下:
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模块独立出来的配置文件,开发时,各自维护自己的配置文件,方便开发,降低耦合:
<?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:
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的配置文件:
<?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:
<?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一。