1. 程式人生 > >使用myeclipse搭建SSM(springMVC+spring+mybatis)

使用myeclipse搭建SSM(springMVC+spring+mybatis)

一、SSM搭建

    1、使用的工具:  myeclipse+jdk8+maven+MySQL+Tomcat8.5

    2、配製檔案截:

          

二、配置檔案

    1、web.xml

        

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 3
<display-name>Tomm</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9
<welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <listener> 13 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 14 </listener> 15 <listener> 16
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 17 </listener> 18 <context-param> 19 <param-name>contextConfigLocation</param-name> 20 <param-value>classpath:applicationContext.xml</param-value> 21 </context-param> 22 <filter> 23 <filter-name>charsetEncoding</filter-name> 24 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 25 <init-param> 26 <param-name>encoding</param-name> 27 <param-value>UTF-8</param-value> 28 </init-param> 29 <init-param> 30 <param-name>forceEncoding</param-name> 31 <param-value>true</param-value> 32 </init-param> 33 </filter> 34 <filter-mapping> 35 <filter-name>charsetEncoding</filter-name> 36 <url-pattern>/*</url-pattern> 37 </filter-mapping> 38 <servlet> 39 <servlet-name>DruidStatView</servlet-name> 40 <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class> 41 </servlet> 42 <servlet-mapping> 43 <servlet-name>DruidStatView</servlet-name> 44 <url-pattern>/druid/*</url-pattern> 45 </servlet-mapping> 46 <servlet> 47 <servlet-name>springmvc</servlet-name> 48 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 49 <init-param> 50 <param-name>contextConfigLocation</param-name> 51 <param-value> 52 classpath:spring-mvc.xml 53 </param-value> 54 </init-param> 55 <load-on-startup>1</load-on-startup> 56 </servlet> 57 <servlet-mapping> 58 <servlet-name>springmvc</servlet-name> 59 <url-pattern>/</url-pattern> 60 </servlet-mapping> 61 62 63 <servlet> 64 <servlet-name>DisplayChart</servlet-name> 65 <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> 66 </servlet> 67 <servlet-mapping> 68 <servlet-name>DisplayChart</servlet-name> 69 <url-pattern>/chart</url-pattern> 70 </servlet-mapping> 71 </web-app>
View Code

 

    2、pom.xml

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2   <modelVersion>4.0.0</modelVersion>
  3   <groupId>Tomm</groupId>
  4   <artifactId>Tomm</artifactId>
  5   <version>0.0.1-SNAPSHOT</version>
  6   <packaging>war</packaging>
  7   <name>Tomm</name>
  8   <description/>
  9    <properties>
 10     <webVersion>3.1</webVersion>
 11     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 12   </properties>
 13   
 14    <build>
 15          <resources>
 16         <resource>
 17             <directory>src/main/java</directory>
 18             <includes>
 19                 <include>**/*.xml </include>
 20             </includes>
 21         </resource>
 22         <resource>
 23             <directory>src/main/resources</directory>
 24             <includes>
 25                 <include>**/*.* </include>
 26             </includes>
 27         </resource>
 28         <resource>
 29             <directory>src/main/webapp/WEB-INF</directory>
 30             <includes>
 31                 <include>*.xml </include>
 32                 <include>*.properties </include>
 33             </includes>
 34         </resource>
 35     </resources>
 36     
 37     <plugins>
 38       <plugin>
 39         <artifactId>maven-compiler-plugin</artifactId>
 40         <version>2.3.2</version>
 41         <configuration>
 42           <source>1.8</source>
 43           <target>1.8</target>
 44         </configuration>
 45       </plugin>
 46       <plugin>
 47         <artifactId>maven-war-plugin</artifactId>
 48         <version>2.6</version>
 49         <configuration>
 50           <failOnMissingWebXml>false</failOnMissingWebXml>
 51         </configuration>
 52       </plugin>
 53     </plugins>
 54   </build>
 55  
 56   <dependencies>
 57     <dependency>
 58       <groupId>javax.servlet</groupId>
 59       <artifactId>jstl</artifactId>
 60       <version>1.2</version>
 61     </dependency>
 62     <dependency>
 63       <groupId>javax.servlet</groupId>
 64       <artifactId>javax.servlet-api</artifactId>
 65       <version>3.1.0</version>
 66     </dependency>
 67     <dependency>
 68       <groupId>org.springframework</groupId>
 69       <artifactId>spring-aop</artifactId>
 70       <version>4.1.0.RELEASE</version>
 71     </dependency>
 72     <dependency>
 73       <groupId>org.springframework</groupId>
 74       <artifactId>spring-aspects</artifactId>
 75       <version>4.1.0.RELEASE</version>
 76     </dependency>
 77     <dependency>
 78       <groupId>org.springframework</groupId>
 79       <artifactId>spring-beans</artifactId>
 80       <version>4.1.0.RELEASE</version>
 81     </dependency>
 82     <dependency>
 83       <groupId>org.springframework</groupId>
 84       <artifactId>spring-context</artifactId>
 85       <version>4.1.0.RELEASE</version>
 86     </dependency>
 87     <dependency>
 88       <groupId>org.springframework</groupId>
 89       <artifactId>spring-context-support</artifactId>
 90       <version>4.1.0.RELEASE</version>
 91     </dependency>
 92     <dependency>
 93       <groupId>org.springframework</groupId>
 94       <artifactId>spring-expression</artifactId>
 95       <version>4.1.0.RELEASE</version>
 96     </dependency>
 97     <dependency>
 98       <groupId>org.springframework</groupId>
 99       <artifactId>spring-instrument</artifactId>
100       <version>4.1.0.RELEASE</version>
101     </dependency>
102     <dependency>
103       <groupId>org.springframework</groupId>
104       <artifactId>spring-instrument-tomcat</artifactId>
105       <version>4.1.0.RELEASE</version>
106     </dependency>
107     <dependency>
108       <groupId>org.springframework</groupId>
109       <artifactId>spring-core</artifactId>
110       <version>4.1.0.RELEASE</version>
111     </dependency>
112     <dependency>
113       <groupId>aopalliance</groupId>
114       <artifactId>aopalliance</artifactId>
115       <version>1.0</version>
116     </dependency>
117     <dependency>
118       <groupId>commons-pool</groupId>
119       <artifactId>commons-pool</artifactId>
120       <version>1.6</version>
121     </dependency>
122     <dependency>
123       <groupId>log4j</groupId>
124       <artifactId>log4j</artifactId>
125       <version>1.2.17</version>
126     </dependency>
127     <dependency>
128       <groupId>org.aspectj</groupId>
129       <artifactId>aspectjweaver</artifactId>
130       <version>1.8.2</version>
131     </dependency>
132     <dependency>
133       <groupId>org.springframework</groupId>
134       <artifactId>spring-web</artifactId>
135       <version>4.1.0.RELEASE</version>
136     </dependency>
137     <dependency>
138       <groupId>org.springframework</groupId>
139       <artifactId>spring-webmvc</artifactId>
140       <version>4.1.0.RELEASE</version>
141     </dependency>
142     <dependency>
143       <groupId>org.springframework</groupId>
144       <artifactId>spring-webmvc-portlet</artifactId>
145       <version>4.1.0.RELEASE</version>
146     </dependency>
147     <dependency>
148       <groupId>org.springframework</groupId>
149       <artifactId>spring-jms</artifactId>
150       <version>4.1.0.RELEASE</version>
151     </dependency>
152     <dependency>
153       <groupId>org.springframework</groupId>
154       <artifactId>spring-oxm</artifactId>
155       <version>4.1.0.RELEASE</version>
156     </dependency>
157     <dependency>
158       <groupId>org.springframework.security</groupId>
159       <artifactId>spring-security-web</artifactId>
160       <version>3.2.5.RELEASE</version>
161     </dependency>
162     <dependency>
163       <groupId>org.springframework.security</groupId>
164       <artifactId>spring-security-core</artifactId>
165       <version>3.2.5.RELEASE</version>
166     </dependency>
167     <dependency>
168       <groupId>org.springframework.security</groupId>
169       <artifactId>spring-security-acl</artifactId>
170       <version>3.2.5.RELEASE</version>
171     </dependency>
172     <dependency>
173       <groupId>org.springframework.security</groupId>
174       <artifactId>spring-security-aspects</artifactId>
175       <version>3.2.5.RELEASE</version>
176     </dependency>
177     <dependency>
178       <groupId>org.springframework.security</groupId>
179       <artifactId>spring-security-cas</artifactId>
180       <version>3.2.5.RELEASE</version>
181     </dependency>
182     <dependency>
183       <groupId>org.springframework.security</groupId>
184       <artifactId>spring-security-config</artifactId>
185       <version>3.2.5.RELEASE</version>
186     </dependency>
187     <dependency>
188       <groupId>org.springframework.security</groupId>
189       <artifactId>spring-security-crypto</artifactId>
190       <version>3.2.5.RELEASE</version>
191     </dependency>
192     <dependency>
193       <groupId>org.springframework.security</groupId>
194       <artifactId>spring-security-ldap</artifactId>
195       <version>3.2.5.RELEASE</version>
196     </dependency>
197     <dependency>
198       <groupId>org.springframework.security</groupId>
199       <artifactId>spring-security-openid</artifactId>
200       <version>3.2.5.RELEASE</version>
201     </dependency>
202     <dependency>
203       <groupId>org.springframework.security</groupId>
204       <artifactId>spring-security-remoting</artifactId>
205       <version>3.2.5.RELEASE</version>
206     </dependency>
207     <dependency>
208       <groupId>org.springframework.security</groupId>
209       <artifactId>spring-security-taglibs</artifactId>
210       <version>3.2.5.RELEASE</version>
211     </dependency>
212     <dependency>
213       <groupId>org.springframework.webflow</groupId>
214       <artifactId>spring-webflow</artifactId>
215       <version>2.4.0.RELEASE</version>
216     </dependency>
217     <dependency>
218       <groupId>org.springframework.webflow</groupId>
219       <artifactId>spring-binding</artifactId>
220       <version>2.4.0.RELEASE</version>
221     </dependency>
222     <dependency>
223       <groupId>org.springframework.webflow</groupId>
224       <artifactId>spring-js</artifactId>
225       <version>2.4.0.RELEASE</version>
226     </dependency>
227     <dependency>
228       <groupId>org.springframework.webflow</groupId>
229       <artifactId>spring-js-resources</artifactId>
230       <version>2.4.0.RELEASE</version>
231     </dependency>
232     <dependency>
233       <groupId>opensymphony</groupId>
234       <artifactId>ognl</artifactId>
235       <version>2.6.11</version>
236     </dependency>
237     <dependency>
238             <groupId>org.mybatis</groupId>
239             <artifactId>mybatis</artifactId>
240             <version>3.4.6</version>
241         </dependency>
242         <dependency>
243             <groupId>org.mybatis</groupId>
244             <artifactId>mybatis-spring</artifactId>
245             <version>1.3.2</version>
246         </dependency>
247         <dependency>
248             <groupId>mysql</groupId>
249             <artifactId>mysql-connector-java</artifactId>
250             <version>5.1.26</version>
251         </dependency>
252         <dependency>
253             <groupId>com.alibaba</groupId>
254             <artifactId>druid</artifactId>
255             <version>0.2.26</version>
256         </dependency>
257         
258         <dependency>
259             <groupId>org.aspectj</groupId>
260             <artifactId>aspectjweaver</artifactId>
261             <version>1.7.3</version>
262         </dependency>
263         
264         <dependency>
265             <groupId>commons-fileupload</groupId>
266             <artifactId>commons-fileupload</artifactId>
267             <version>1.3</version>
268         </dependency>
269         <dependency>
270             <groupId>log4j</groupId>
271             <artifactId>log4j</artifactId>
272             <version>1.2.17</version>
273         </dependency>
274         <dependency>
275             <groupId>com.alibaba</groupId>
276             <artifactId>fastjson</artifactId>
277             <version>1.2.4</version>
278         </dependency>
279 
280         <dependency>
281             <groupId>junit</groupId>
282             <artifactId>junit</artifactId>
283             <version>4.11</version>
284         </dependency>
285         <dependency>
286             <groupId>commons-io</groupId>
287             <artifactId>commons-io</artifactId>
288             <version>2.5</version>
289         </dependency>
290         <dependency>
291             <groupId>commons-logging</groupId>
292             <artifactId>commons-logging</artifactId>
293             <version>1.2</version>
294         </dependency>
295         <dependency>
296             <groupId>jfree</groupId>
297             <artifactId>jcommon</artifactId>
298             <version>1.0.16</version>
299         </dependency>
300         <dependency>
301             <groupId>jfree</groupId>
302             <artifactId>jfreechart</artifactId>
303             <version>1.0.13</version>
304         </dependency>
305   </dependencies>
306  </project>
View Code

    3、applicationcontext.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:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop" 
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9     http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
10     http://www.springframework.org/schema/context   
11             http://www.springframework.org/schema/context/spring-context.xsd   
12     http://www.springframework.org/schema/aop
13     http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
14     http://www.springframework.org/schema/tx
15     http://www.springframework.org/schema/tx/spring-tx-4.1.xsd ">
16 
17     <!-- 掃描service、dao元件 -->
18     <context:component-scan base-package="com.service" />
19     <context:component-scan base-package="com.dao" />
20    
21     <!-- 引入properties的檔案 -->
22     <bean
23         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
24         <property name="locations">
25             <list>
26                 <value>classpath:*.properties</value>
27             </list>
28         </property>
29     </bean>
30      <!-- 阿里 druid資料庫連線池 -->
31     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">  
32          <!-- 資料庫基本資訊配置 -->
33          <property name="url" value="${url}" />  
34          <property name="username" value="${username}" />  
35          <property name="password" value="${password}" />  
36          <property name="driverClassName" value="${driverClassName}" />  
37          <property name="filters" value="${filters}" />  
38          <!-- 最大併發連線數 -->
39          <property name="maxActive" value="${maxActive}" />
40         
41     </bean>
42 
43 
44     <!-- 配置SqlSessionFactoryBean -->
45     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
46         <!-- 引用資料來源元件 -->
47         <property name="dataSource" ref="dataSource" />
48         <!-- 引用MyBatis配置檔案中的配置 -->
49         <property name="configLocation" value="classpath:mybatis-config.xml" />
50         <!-- 配置SQL對映檔案資訊 -->
51         <property name="mapperLocations">
52             <list>
53                 <value>classpath:com/dao/impl/*Mapper.xml</value>
54             </list>
55         </property>
56     </bean>
57     <!-- DAO介面所在包名,Spring會自動查詢其下的類 -->
58     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
59         <property name="basePackage" value="com.dao" />
60         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
61     </bean>
62     <!-- 配置註解事務 -->
63     <bean id="transactionManager"
64         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
65         <property name="dataSource" ref="dataSource" />
66     </bean>
67     <tx:annotation-driven transaction-manager="transactionManager" />
68         
69 
70 
71 </beans>
View Code

 

    4、db.properties

 1 url:jdbc:mysql://主機名:3306/連線的資料名?useUnicode=true&characterEncoding=utf8
 2 driverClassName:com.mysql.jdbc.Driver
 3 username:使用者名稱
 4 password:密碼
 5  
 6 
 7 filters:stat
 8 
 9 maxActive:20
10 initialSize:1
11 maxWait:60000
12 minIdle:10
13 maxIdle:15
14  
View Code

    5、log4j.properties

 1 log4j.rootLogger=DEBUG,CONSOLE,fil
 2 #log4j.rootLogger=ERROR,ROLLING_FILE
 3 log4j.logger.cn.smbms.dao=debug
 4 log4j.logger.com.ibatis=debug 
 5 log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug 
 6 log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug 
 7 log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug 
 8 log4j.logger.java.sql.Connection=debug 
 9 log4j.logger.java.sql.Statement=debug 
10 log4j.logger.java.sql.PreparedStatement=debug 
11 log4j.logger.java.sql.ResultSet=debug 
12 log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug
13 
14 ######################################################################################
15 # Console Appender  \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
16 ######################################################################################
17 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
18 log4j.appender.Threshold=error
19 log4j.appender.CONSOLE.Target=System.out
20 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
21 log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n
22 
23 
24 ######################################################################################
25 # DailyRolling File  \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
26 ######################################################################################
27 log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
28 log4j.appender.file.DatePattern=yyyy-MM-dd
29 log4j.appender.file.File=log.log
30 log4j.appender.file.Append=true
31 log4j.appender.file.Threshold=error
32 log4j.appender.file.layout=org.apache.log4j.PatternLayout
33 log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
34 
35 
36 
37 log4j.logger.com.opensymphony.xwork2=error  
View Code

    6.log4j.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <Loggers>  
 3     *<logger name="com.dao" level="DEBUG"/>*
 4     <Root level="error">  
 5       <AppenderRef ref="Console"/>  
 6     </Root>  
 7     <Logger name="com.dao.impl" level="TRACE" additivity="false">       //為dao mapper所在的包,level為TRACE  
 8       <AppenderRef ref="Console"/>  
 9     </Logger>  
10   </Loggers>
View Code

    7.mybatis-config.xml

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 3     "http://mybatis.org/dtd/mybatis-3-config.dtd">
 4 <configuration>
 5     <settings>
 6         <setting name="lazyLoadingEnabled" value="false" />
 7         <setting name="cacheEnabled" value="false" />
 8         <setting name="logImpl" value="STDOUT_LOGGING" />
 9     </settings>
10     <!--類型別名 -->
11     <typeAliases>
12             <typeAlias type="com.pojo.User" alias="user" />
13             <!--  <typeAlias type="com.pojo.Books" alias="book" />-->
14     </typeAliases>
15     <!-- <typeAliases>
16         <typeAlias type="com.pojo.BookUser" alias="buser" />
17         <typeAlias type="com.pojo.Book" alias="book"/>
18     </typeAliases> -->
19 </configuration>
View Code

 

    8.spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
 5     xmlns:p="http://www.springframework.org/schema/p" 
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xsi:schemaLocation="
 8         http://www.springframework.org/schema/beans
 9         http://www.springframework.org/schema/beans/spring-beans.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd
12         http://www.springframework.org/schema/mvc
13         http://www.springframework.org/schema/mvc/spring-mvc.xsd">
14     <!-- 配置檔案 <bean name="/index.html" class="com.web.HelloAction"></bean> -->
15   
16     <context:component-scan base-package="com.controller" />
17     
18     <mvc:annotation-driven>
19         <mvc:message-converters>
20             <bean class="org.springframework.http.converter.StringHttpMessageConverter">
21                 <property name="supportedMediaTypes">
22                     <list>
23                         <value>application/json;charset=UTF-8</value>
24                     </list>
25                 </property>
26             </bean>
27     
28             <bean
29                 class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
30                 <property name="supportedMediaTypes">
31                     <list>
32                         <value>text/html;charset=UTF-8</value>
33                         <value>application/json</value>
34                     </list>
35                 </property>    
36             </bean>
37         </mvc:message-converters>
38     </mvc:annotation-driven>
39     <!-- 對映到根目錄下,檔案引用靜態資源時就加上static.. -->          
40     <mvc:resources mapping="/static/**" location="WEB-INF/static/" />
41 
42 
43     
44     <!-- 完成檢視的對應 -->
45     <!-- 對轉向頁面的路徑解析。prefix:字首, suffix:字尾 -->
46     <bean
47         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
48         <property name="prefix" value="/WEB-INF/jsp/" />
49         <property name="suffix" value=".jsp" />
50     </bean>
51 
52     <!-- 上傳檔案攔截,設定最大上傳檔案大小 10M=10*1024*1024(B)=10485760 bytes -->
53     <bean id="multipartResolver"
54         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
55         <property name="maxUploadSize" value="10485760 " />   <!-- 最大檔案大小限制 -->
56         <property name="defaultEncoding" value="UTF-8" />
57 
58     </bean>
59 </beans>
View Code