最近写项目,由于公司需求,使用myeclispe来开发maven项目,关于maven就不再介绍,无论是jar包管理功能,还是作为版本构建工具,优点自然是很多,下面先贴出所需要的配置文件。
maven所需要的 pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.beichende</groupId> 5 <artifactId>hconly</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <packaging>war</packaging> 8 <build> 9 <plugins> 10 <!-- java编译插件 --> 11 <plugin> 12 <groupId>org.apache.maven.plugins</groupId> 13 <artifactId>maven-compiler-plugin</artifactId> 14 <version>3.2</version> 15 <configuration> 16 <source>1.7</source> 17 <target>1.7</target> 18 <encoding>UTF-8</encoding> 19 </configuration> 20 </plugin> 21 </plugins> 22 </build> 23 <dependencies> 24 <!-- SSH框架最小依赖 start --> 25 <!-- struts2的核心包,其依赖包会自动下载 --> 26 <dependency> 27 <groupId>org.apache.struts</groupId> 28 <artifactId>struts2-core</artifactId> 29 <version>2.2.1</version> 30 <exclusions> 31 <exclusion> 32 <groupId>commons-io</groupId> 33 <artifactId>commons-io</artifactId> 34 </exclusion> 35 </exclusions> 36 </dependency> 37 <!-- struts2与spring的整合插件 --> 38 <dependency> 39 <groupId>org.apache.struts</groupId> 40 <artifactId>struts2-spring-plugin</artifactId> 41 <version>2.2.1</version> 42 <exclusions> 43 <exclusion> 44 <groupId>org.springframework</groupId> 45 <artifactId>spring-core</artifactId> 46 </exclusion> 47 <exclusion> 48 <groupId>org.springframework</groupId> 49 <artifactId>spring-beans</artifactId> 50 </exclusion> 51 <exclusion> 52 <groupId>org.springframework</groupId> 53 <artifactId>spring-context</artifactId> 54 </exclusion> 55 <exclusion> 56 <groupId>org.springframework</groupId> 57 <artifactId>spring-web</artifactId> 58 </exclusion> 59 </exclusions> 60 </dependency> 61 <!-- spring的核心包,其他依赖包会自动下载 --> 62 <dependency> 63 <groupId>org.springframework</groupId> 64 <artifactId>spring-core</artifactId> 65 <version>3.0.5.RELEASE</version> 66 </dependency> 67 <dependency> 68 <groupId>org.springframework</groupId> 69 <artifactId>org.springframework.transaction</artifactId> 70 <version>3.0.5.RELEASE</version> 71 </dependency> 72 <dependency> 73 <groupId>org.springframework</groupId> 74 <artifactId>spring-orm</artifactId> 75 <version>3.0.5.RELEASE</version> 76 </dependency> 77 <dependency> 78 <groupId>org.springframework</groupId> 79 <artifactId>spring-web</artifactId> 80 <version>3.0.5.RELEASE</version> 81 </dependency> 82 <!-- 使用spring的aop,aop要依赖aspectjweaver --> 83 <dependency> 84 <groupId>org.springframework</groupId> 85 <artifactId>spring-aop</artifactId> 86 <version>3.0.5.RELEASE</version> 87 </dependency> 88 <dependency> 89 <groupId>org.aspectj</groupId> 90 <artifactId>aspectjweaver</artifactId> 91 <version>1.7.1</version> 92 </dependency> 93 <!-- hibernate配置 --> 94 <dependency> 95 <groupId>org.hibernate</groupId> 96 <artifactId>hibernate-ehcache</artifactId> 97 <version>3.3.2.GA</version> 98 <exclusions> 99 <exclusion> 100 <groupId>commons-collections</groupId> 101 <artifactId>commons-collections</artifactId> 102 </exclusion> 103 <exclusion> 104 <groupId>commons-logging</groupId> 105 <artifactId>commons-logging</artifactId> 106 </exclusion> 107 <exclusion> 108 <groupId>xml-apis</groupId> 109 <artifactId>xml-apis</artifactId> 110 </exclusion> 111 <exclusion> 112 <groupId>org.slf4j</groupId> 113 <artifactId>slf4j-api</artifactId> 114 </exclusion> 115 </exclusions> 116 </dependency> 117 <dependency> 118 <groupId>org.apache.struts</groupId> 119 <artifactId>struts2-convention-plugin</artifactId> 120 <version>2.2.1</version> 121 </dependency> 122 <dependency> 123 <groupId>javax.persistence</groupId> 124 <artifactId>persistence-api</artifactId> 125 <version>1.0.2</version> 126 </dependency> 127 <!-- 引入c3p0连接池 --> 128 <dependency> 129 <groupId>c3p0</groupId> 130 <artifactId>c3p0</artifactId> 131 <version>0.9.1.2</version> 132 </dependency> 133 <!-- 引入ali连接池 --> 134 <dependency> 135 <groupId>com.alibaba</groupId> 136 <artifactId>druid</artifactId> 137 <version>1.1.11</version> 138 </dependency> 139 <!-- SSH框架最小依赖 end --> 140 141 <!--引入Oracle数据库jdbc连接驱动 --> 142 <dependency> 143 <groupId>com.oracle.driver</groupId> 144 <artifactId>ojdbc</artifactId> 145 <version>11.2.0.3.0</version> 146 </dependency> 147 <!--引入log4j日志包 --> 148 <dependency> 149 <groupId>log4j</groupId> 150 <artifactId>log4j</artifactId> 151 <version>1.2.17</version> 152 </dependency> 153 <!-- 引入servlet jsp --> 154 <dependency> 155 <groupId>javax.servlet</groupId> 156 <artifactId>servlet-api</artifactId> 157 <version>2.5</version> 158 <scope>provided</scope> 159 </dependency> 160 <dependency> 161 <groupId>javax.servlet</groupId> 162 <artifactId>jsp-api</artifactId> 163 <version>2.0</version> 164 <scope>provided</scope> 165 </dependency> 166 <!--指定版本的相关jar --> 167 <dependency> 168 <groupId>org.slf4j</groupId> 169 <artifactId>slf4j-log4j12</artifactId> 170 <version>1.7.10</version> 171 <exclusions> 172 <exclusion> 173 <groupId>log4j</groupId> 174 <artifactId>log4j</artifactId> 175 </exclusion> 176 </exclusions> 177 </dependency> 178 <dependency> 179 <groupId>cglib</groupId> 180 <artifactId>cglib</artifactId> 181 <version>2.2.2</version> 182 </dependency> 183 <dependency> 184 <groupId>commons-lang</groupId> 185 <artifactId>commons-lang</artifactId> 186 <version>2.5</version> 187 </dependency> 188 <!--Struts做json处理相关jar --> 189 <dependency> 190 <groupId>net.sf.json-lib</groupId> 191 <artifactId>json-lib</artifactId> 192 <version>2.4</version> 193 <classifier>jdk15</classifier> 194 <exclusions> 195 <exclusion> 196 <groupId>commons-lang</groupId> 197 <artifactId>commons-lang</artifactId> 198 </exclusion> 199 </exclusions> 200 </dependency> 201 <dependency> 202 <groupId>org.apache.struts</groupId> 203 <artifactId>struts2-json-plugin</artifactId> 204 <version>2.2.1</version> 205 </dependency> 206 </dependencies> 207 </project>
注明:刚开始使用maven的小伙伴可能不知道exclusion标签的作用,用maven管理库依赖,有个好处就是连同库的依赖的全部jar文件一起下载,免去手工添加的麻烦,但同时也带来了同一个
jar会被下载了不同版本的问题,好在pom的配置里面允许用<exclusion>来排除一些不需要同时下载的依赖jar。 比如配置struts-core,它会同时下载javassist和asm相关的jar,但版本又不够新,这时可以排除它们,然后指定版本。
spring整合hibernate.cfg.xml的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx.xsd"> 15 16 <!--引入hibernate配置--> 17 <context:property-placeholder location="classpath:jdbc.properties"/> 18 <!--配置连接池--> 19 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> 20 <property name="driverClassName" value="${jdbc.driverclass}" /> 21 <property name="url" value="${jdbc.url}" /> 22 <property name="username" value="${jdbc.username}" /> 23 <property name="password" value="${jdbc.password}" /> 24 <!-- 配置初始化大小、最小、最大 --> 25 <property name="initialSize" value="10" /> 26 <property name="minIdle" value="10" /> 27 <property name="maxActive" value="20" /> 28 <property name = "timeBetweenEvictionRunsMillis" value ="10000" /> 29 <!-- 配置获取连接等待超时的时间 --> 30 <property name="maxWait" value="60000" /> 31 <!-- 配置监控统计拦截的filters --> 32 <property name="filters" value="stat" /> 33 </bean> 34 35 <!-- Spring整合Hibernate --> 36 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 37 <!-- 注入连接池 --> 38 <property name="dataSource" ref="dataSource"/> 39 <!--注入hibernate--> 40 <property name="hibernateProperties"> 41 <props> 42 <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 43 <prop key="hibernate.show_sql">false</prop> 44 <prop key="hibernate.format_sql">false</prop> 45 <prop key="hibernate.hbm2ddl.auto">update</prop> 46 </props> 47 </property> 48 <!-- 设置映射文件 --> 49 <property name="mappingResources"> 50 <list> 51 <value>配置xxx.hbm.xml路径</value> 52 </list> 53 </property> 54 </bean> 55 56 <!--配置事务管理器--> 57 <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 58 <property name="sessionFactory" ref="sessionFactory"/> 59 </bean> 60 <tx:annotation-driven transaction-manager="transactionManager"/> 61 </beans>
struts2相关配置
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 4 "http://struts.apache.org/dtds/struts-2.5.dtd"> 5 <struts> 6 <!-- strutsRMI apache struts基于安全性考虑,RMI默认是关闭的 --> 7 <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 8 9 <!-- 项目信息 --> 10 <package name="proInfo" namespace="/" extends="json-default"> 11 <action name="query_all_bank" class="proInfoAction" method="selBankList"> 12 <result type="json"> 13 <param name="root">root</param> 14 </result> 15 </action> 16 </package> 17 </struts>
注明:此处extends需要使用json-default,由于要返回需要的json数据,使用struts-default不能返回json,有兴趣的小伙伴可以研究一下源码
spring整合bean(实体类)的配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd 13 http://www.springframework.org/schema/tx 14 http://www.springframework.org/schema/tx/spring-tx.xsd"> 15 16 <!--导入hibernate相关配置--> 17 <import resource="hibernate.cfg.xml"/> 18 19 <!-- 项目信息 action --> 20 <bean id="proInfoAction" class="pm.web.action.ProInfoAction" scope="prototype"> 21 <property name="proInfoService"> 22 <ref bean="proInfoService"/> 23 </property> 24 </bean> 25 <!-- 项目信息service --> 26 <bean id="proInfoService" class="pm.service.ProInfoService"> 27 <property name="proInfoDao"> 28 <ref bean="proInfoDao"/> 29 </property> 30 </bean> 31 <!-- 项目信息dao --> 32 <bean id="proInfoDao" class="pm.dao.ProInfoDao"> 33 <property name="sessionFactory"> 34 <ref bean="sessionFactory"/> 35 </property> 36 </bean> 37 </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_3_0.xsd" version="3.0"> <display-name>Hconly</display-name> <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> <!--配置struts监听 --> <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>*.action</url-pattern> </filter-mapping> <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> </web-app>
jdbc.properties配置
1 jdbc.driverclass=oracle.jdbc.driver.OracleDriver 2 jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl 3 jdbc.username=SCOTT 4 jdbc.password=root
log4j简单配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <Configuration> 3 <Appenders> 4 <Console name="STDOUT" target="SYSTEM_OUT"> 5 <PatternLayout pattern="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/> 6 </Console> 7 </Appenders> 8 <Loggers> 9 <Logger name="com.opensymphony.xwork2" level="info"/> 10 <Logger name="org.apache.struts2" level="info"/> 11 <Logger name="org.demo.rest" level="debug"/> 12 <Root level="warn"> 13 <AppenderRef ref="STDOUT"/> 14 </Root> 15 </Loggers> 16 </Configuration>
配置文件到此就告一段落,还存在不完善的地方,望指正,接下来是后台代码......