idea+maven搭建ssh,支持事务,无hibernate文件整合
1.按照顺序选择点击,我的导入的插件比较多webapp特别不好找,,所以webapp在大概中间的位置也标出来了。
2.然后命名啥的,之后打开项目应该是这样的,要创建java文件夹和resources文件夹,并标记不同的路径,就成了这样的。
3.修改完项目结构之后首先更改pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.ssh</groupId> 8 <artifactId>ssh</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <packaging>war</packaging> 11 12 <name>ssh Maven Webapp</name> 13 <!-- FIXME change it to the project's website --> 14 <url>http://www.example.com</url> 15 16 <!--指定ssh版本--> 17 <properties> 18 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 19 <maven.compiler.source>1.8</maven.compiler.source> 20 <maven.compiler.target>1.8</maven.compiler.target> 21 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 22 <spring.version>5.0.2.RELEASE</spring.version> 23 <hibernate.version>5.2.12.Final</hibernate.version> 24 <struts.version>2.5.14.1</struts.version> 25 <jackson.version>2.5.0</jackson.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>junit</groupId> 31 <artifactId>junit</artifactId> 32 <version>4.11</version> 33 <scope>test</scope> 34 </dependency> 35 36 <!-- hibernate --> 37 <dependency> 38 <groupId>org.hibernate</groupId> 39 <artifactId>hibernate-core</artifactId> 40 <version>${hibernate.version}</version> 41 </dependency> 42 <!-- hibernate-c3p0 --> 43 <dependency> 44 <groupId>org.hibernate</groupId> 45 <artifactId>hibernate-c3p0</artifactId> 46 <version>${hibernate.version}</version> 47 </dependency> 48 49 <!-- struts2 --> 50 <dependency> 51 <groupId>org.apache.struts</groupId> 52 <artifactId>struts2-core</artifactId> 53 <version>${struts.version}</version> 54 </dependency> 55 56 <!-- struts2 spring 整合的核心包--> 57 <dependency> 58 <groupId>org.apache.struts</groupId> 59 <artifactId>struts2-spring-plugin</artifactId> 60 <version>${struts.version}</version> 61 </dependency> 62 63 <!-- spring --> 64 <dependency> 65 <groupId>org.springframework</groupId> 66 <artifactId>spring-core</artifactId> 67 <version>${spring.version}</version> 68 </dependency> 69 <dependency> 70 <groupId>org.springframework</groupId> 71 <artifactId>spring-context</artifactId> 72 <version>${spring.version}</version> 73 </dependency> 74 <dependency> 75 <groupId>org.springframework</groupId> 76 <artifactId>spring-jdbc</artifactId> 77 <version>${spring.version}</version> 78 </dependency> 79 <dependency> 80 <groupId>org.springframework</groupId> 81 <artifactId>spring-beans</artifactId> 82 <version>${spring.version}</version> 83 </dependency> 84 <dependency> 85 <groupId>org.springframework</groupId> 86 <artifactId>spring-web</artifactId> 87 <version>${spring.version}</version> 88 </dependency> 89 <dependency> 90 <groupId>org.springframework</groupId> 91 <artifactId>spring-expression</artifactId> 92 <version>${spring.version}</version> 93 </dependency> 94 <dependency> 95 <groupId>org.springframework</groupId> 96 <artifactId>spring-orm</artifactId> 97 <version>${spring.version}</version> 98 </dependency> 99 <dependency> 100 <groupId>cglib</groupId> 101 <artifactId>cglib-nodep</artifactId> 102 <version>2.1_3</version> 103 104 105 </dependency> 106 107 <!--spring aop包 注释方式使用事务管理 可以不引用--> 108 <dependency> 109 <groupId>aspectj</groupId> 110 <artifactId>aspectjrt</artifactId> 111 <version>1.5.0</version> 112 </dependency> 113 114 <dependency> 115 <groupId>aspectj</groupId> 116 <artifactId>aspectjweaver</artifactId> 117 <version>1.5.0</version> 118 </dependency> 119 120 <!-- 添加对数据库的支持 --> 121 <dependency> 122 <groupId>mysql</groupId> 123 <artifactId>mysql-connector-java</artifactId> 124 <version>5.0.5</version> 125 </dependency> 126 127 <!-- https://mvnrepository.com/artifact/c3p0/c3p0 --> 128 <dependency> 129 <groupId>c3p0</groupId> 130 <artifactId>c3p0</artifactId> 131 <version>0.9.1.2</version> 132 </dependency> 133 134 <dependency> 135 <groupId>org.projectlombok</groupId> 136 <artifactId>lombok</artifactId> 137 <version>1.16.18</version> 138 </dependency> 139 140 <!-- log4j --> 141 <dependency> 142 <groupId>log4j</groupId> 143 <artifactId>log4j</artifactId> 144 <version>1.2.17</version> 145 </dependency> 146 147 <!-- json --> 148 <dependency> 149 <groupId>com.alibaba</groupId> 150 <artifactId>fastjson</artifactId> 151 <version>1.2.3</version> 152 </dependency> 153 154 <dependency> 155 <groupId>com.fasterxml.jackson.core</groupId> 156 <artifactId>jackson-annotations</artifactId> 157 <version>${jackson.version}</version> 158 </dependency> 159 160 <dependency> 161 <groupId>com.fasterxml.jackson.core</groupId> 162 <artifactId>jackson-core</artifactId> 163 <version>${jackson.version}</version> 164 </dependency> 165 166 <dependency> 167 168 <groupId>javax.persistence</groupId> 169 170 <artifactId>persistence-api</artifactId> 171 172 <version>1.0.2</version> 173 174 <scope>provided</scope> 175 176 </dependency> 177 178 179 <!-- servlet --> 180 181 <dependency> 182 183 <groupId>javax.servlet</groupId> 184 185 <artifactId>servlet-api</artifactId> 186 187 <version>3.0-alpha-1</version> 188 189 <scope>provided</scope> 190 191 </dependency> 192 193 194 <dependency> 195 196 <groupId>commons-fileupload</groupId> 197 198 <artifactId>commons-fileupload</artifactId> 199 200 <version>1.2.1</version> 201 202 </dependency> 203 204 205 <dependency> 206 207 <groupId>javax.transaction</groupId> 208 209 <artifactId>jta</artifactId> 210 211 <version>1.1</version> 212 213 </dependency> 214 215 216 <dependency> 217 218 <groupId>antlr</groupId> 219 220 <artifactId>antlr</artifactId> 221 222 <version>2.7.7</version> 223 224 </dependency> 225 226 227 <dependency> 228 229 <groupId>org.javassist</groupId> 230 231 <artifactId>javassist</artifactId> 232 233 <version>3.20.0-GA</version> 234 235 </dependency> 236 237 238 <dependency> 239 240 <groupId>taglibs</groupId> 241 242 <artifactId>standard</artifactId> 243 244 <version>1.1.2</version> 245 246 </dependency> 247 248 249 <dependency> 250 251 <groupId>commons-io</groupId> 252 253 <artifactId>commons-io</artifactId> 254 255 <version>1.4</version> 256 257 </dependency> 258 259 260 <dependency> 261 262 <groupId>commons-codec</groupId> 263 264 <artifactId>commons-codec</artifactId> 265 266 <version>1.2</version> 267 268 </dependency> 269 270 271 <dependency> 272 273 <groupId>com.google.code.gson</groupId> 274 275 <artifactId>gson</artifactId> 276 277 <version>2.3.1</version> 278 279 </dependency> 280 <dependency> 281 <groupId>javax.servlet</groupId> 282 <artifactId>javax.servlet-api</artifactId> 283 <version>3.0.1</version> 284 <scope>provided</scope> 285 </dependency> 286 <dependency> 287 <groupId>javax.servlet.jsp</groupId> 288 <artifactId>jsp-api</artifactId> 289 <version>2.2</version> 290 <scope>provided</scope> 291 </dependency> 292 <dependency> 293 <groupId>javax.servlet</groupId> 294 <artifactId>jstl</artifactId> 295 <version>1.2</version> 296 </dependency> 297 298 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple --> 299 <dependency> 300 <groupId>org.slf4j</groupId> 301 <artifactId>slf4j-simple</artifactId> 302 <version>1.7.25</version> 303 </dependency> 304 </dependencies> 305 306 307 <build> 308 <finalName>ssh</finalName> 309 <!--编译资源文件--> 310 <resources> 311 <resource> 312 <directory>src/main/java</directory> 313 <includes> 314 <include>**/*.xml</include> 315 </includes> 316 </resource> 317 <resource> 318 <directory>src/main/resources</directory> 319 <includes> 320 <include>**/*.xml</include> 321 <include>**/*.properties</include> 322 </includes> 323 </resource> 324 </resources> 325 326 <!--tomcat7插件--> 327 <plugins> 328 <!-- Run the JUnit unit tests in an isolated classloader --> 329 <plugin> 330 <groupId>org.apache.tomcat.maven</groupId> 331 <artifactId>tomcat7-maven-plugin</artifactId> 332 <configuration> 333 <port>8080</port> 334 <path>/</path> 335 </configuration> 336 </plugin> 337 </plugins> 338 339 </build> 340 </project>
4.修改web.xml文件
1 <!DOCTYPE web-app PUBLIC 2 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 3 "http://java.sun.com/dtd/web-app_2_3.dtd" > 4 5 <web-app> 6 <display-name>ssh</display-name> 7 8 <context-param> 9 <param-name>contextConfigLocation</param-name> 10 <param-value>classpath:applicationContext.xml</param-value> 11 </context-param> 12 13 <!-- OpenSessionInView机制:会将会话到表现层,让会话在请求结束之后关闭,延迟了session关闭,需要放置到struts2的过滤器的前面 --> 14 <filter> 15 <filter-name>openSessionInViewFilter</filter-name> 16 <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class> 17 </filter> 18 <filter-mapping> 19 <filter-name>openSessionInViewFilter</filter-name> 20 <url-pattern>/*</url-pattern> 21 </filter-mapping> 22 23 <filter> 24 <filter-name>struts2</filter-name> 25 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> 26 </filter> 27 28 <filter-mapping> 29 <filter-name>struts2</filter-name> 30 <url-pattern>/*</url-pattern> 31 </filter-mapping> 32 33 <listener> 34 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 35 </listener> 36 </web-app>
5.在resources里创建struts.xml,里面的customerAction交给spring创建,这里的customerAction必须要对应spring文件里的id
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.5.dtd"> 5 <struts> 6 <!-- 简易模板 --> 7 <constant name="struts.ui.theme" value="simple"></constant> 8 <!-- 所有匹配*.action的请求都由struts2处理 --> 9 <constant name="struts.action.extension" value="action"/> 10 <!-- 是否启用开发模式 --> 11 <constant name="struts.devMode" value="true"/> 12 <!-- struts配置文件改动后,是否重新加载 --> 13 <constant name="struts.configuration.xml.reload" value="true"/> 14 <!-- 设置浏览器是否缓存静态内容 --> 15 <constant name="struts.serve.static.browserCache" value="false"/> 16 <!-- 请求参数的编码方式 --> 17 <constant name="struts.i18n.encoding" value="utf-8"/> 18 <!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 --> 19 <constant name="struts.i18n.reload" value="true"/> 20 <!-- 文件上传最大值 --> 21 <constant name="struts.multipart.maxSize" value="104857600"/> 22 <!-- 让struts2支持动态方法调用 --> 23 <constant name="struts.enable.DynamicMethodInvocation" value="true"/> 24 25 <!-- 允许标签中使用表达式语法 --> 26 <constant name="struts.tag.altSyntax" value="true"/> 27 <!-- 对于WebLogic,Orion,OC4J此属性应该设置成true --> 28 <constant name="struts.dispatcher.parametersWorkaround" value="false"/> 29 <!-- 处理url的转发 --> 30 <package name="ssh" extends="struts-default" namespace="/" strict-method-invocation="false"> 31 <!--2.3以后通过这种方法布置通配符 strict-method-invocation="false" 关闭--> 32 33 <action name="customer_*" class="customerAction" method="{1}"> 34 <result name="saveUI">/jsp/customer/add.jsp</result> 35 <result name="success">/jsp/customer/add.jsp</result> 36 </action> 37 </package> 38 </struts>
6.在resources里创建jdbc.properties
jdbc.driverClass=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql:///crm jdbc.username=root jdbc.password=123
7.在resources里创建log4j.properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c\:mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=info, stdout
总结:至此ssh大致创建完成,声明式事务也已经配置,关于代码的具体实现没有写,但是关于配置文件的书写与搭建过程已经说明。亲测可用,要是不可用,对不起!最后提供一下使用模板的dao层和service层和action层的截图,dao层必须继承Hiernate模板,然后使用模板操作
1.dao层
2.service层
3.action