1. 程式人生 > 其它 >SSM框架配置

SSM框架配置

一、新建Maven工程

 

二、修改目錄

 

三、修改pom.xml

新增SSM所有依賴。

pom.xml:

  1   <dependencies>
  2     <!-- spring -->
  3     <dependency>
  4       <groupId>org.springframework</groupId>
  5       <artifactId>spring-context</artifactId>
  6       <version>${spring.version}</
version> 7 </dependency> 8 <dependency> 9 <groupId>org.springframework</groupId> 10 <artifactId>spring-beans</artifactId> 11 <version>${spring.version}</version> 12 </dependency> 13 <dependency> 14 <
groupId>org.springframework</groupId> 15 <artifactId>spring-webmvc</artifactId> 16 <version>${spring.version}</version> 17 </dependency> 18 <dependency> 19 <groupId>org.springframework</groupId> 20 <artifactId
>spring-jdbc</artifactId> 21 <version>${spring.version}</version> 22 </dependency> 23 <dependency> 24 <groupId>org.springframework</groupId> 25 <artifactId>spring-aspects</artifactId> 26 <version>${spring.version}</version> 27 </dependency> 28 <dependency> 29 <groupId>org.springframework</groupId> 30 <artifactId>spring-jms</artifactId> 31 <version>${spring.version}</version> 32 </dependency> 33 <dependency> 34 <groupId>org.springframework</groupId> 35 <artifactId>spring-context-support</artifactId> 36 <version>${spring.version}</version> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework</groupId> 40 <artifactId>spring-test</artifactId> 41 <version>${spring.version}</version> 42 </dependency> 43 44 <!-- Mybatis --> 45 <dependency> 46 <groupId>org.mybatis</groupId> 47 <artifactId>mybatis</artifactId> 48 <version>${mybatis.version}</version> 49 </dependency> 50 <dependency> 51 <groupId>org.mybatis</groupId> 52 <artifactId>mybatis-spring</artifactId> 53 <version>${mybatis.spring.version}</version> 54 </dependency> 55 <dependency> 56 <groupId>com.github.miemiedev</groupId> 57 <artifactId>mybatis-paginator</artifactId> 58 <version>${mybatis.paginator.version}</version> 59 </dependency> 60 <dependency> 61 <groupId>com.github.pagehelper</groupId> 62 <artifactId>pagehelper</artifactId> 63 <version>${pagehelper.version}</version> 64 </dependency> 65 <!-- MySql --> 66 <dependency> 67 <groupId>mysql</groupId> 68 <artifactId>mysql-connector-java</artifactId> 69 <version>${mysql.version}</version> 70 </dependency> 71 <!-- 連線池 --> 72 <dependency> 73 <groupId>com.alibaba</groupId> 74 <artifactId>druid</artifactId> 75 <version>${druid.version}</version> 76 </dependency> 77 78 <!-- junit --> 79 <dependency> 80 <groupId>junit</groupId> 81 <artifactId>junit</artifactId> 82 <version>${junit.version}</version> 83 <scope>test</scope> 84 </dependency> 85 86 <!-- JSP相關 --> 87 <dependency> 88 <groupId>jstl</groupId> 89 <artifactId>jstl</artifactId> 90 <version>${jstl.version}</version> 91 </dependency> 92 <dependency> 93 <groupId>javax.servlet</groupId> 94 <artifactId>javax.servlet-api</artifactId> 95 <version>3.0.1</version> 96 <scope>provided</scope> 97 </dependency> 98 <dependency> 99 <groupId>javax.servlet</groupId> 100 <artifactId>jsp-api</artifactId> 101 <scope>provided</scope> 102 <version>${jsp-api.version}</version> 103 </dependency> 104 105 <!-- Jackson Json處理工具包 --> 106 <dependency> 107 <groupId>com.fasterxml.jackson.core</groupId> 108 <artifactId>jackson-databind</artifactId> 109 <version>${jackson.version}</version> 110 </dependency> 111 <dependency> 112 <groupId>org.json</groupId> 113 <artifactId>json</artifactId> 114 <version>20140107</version> 115 </dependency> 116 117 <!-- 檔案非同步上傳使用的依賴--> 118 <dependency> 119 <groupId>commons-io</groupId> 120 <artifactId>commons-io</artifactId> 121 <version>2.4</version> 122 </dependency> 123 <dependency> 124 <groupId>commons-fileupload</groupId> 125 <artifactId>commons-fileupload</artifactId> 126 <version>1.3.1</version> 127 </dependency> 128 </dependencies> 129 130 <!-- 外掛配置 --> 131 <build> 132 <plugins> 133 <plugin> 134 <groupId>org.apache.maven.plugins</groupId> 135 <artifactId>maven-compiler-plugin</artifactId> 136 <configuration> 137 <source>1.8</source> 138 <target>1.8</target> 139 <encoding>UTF-8</encoding> 140 </configuration> 141 </plugin> 142 </plugins> 143 144 <!--識別所有的配置檔案--> 145 <resources> 146 <resource> 147 <directory>src/main/java</directory> 148 <includes> 149 <include>**/*.properties</include> 150 <include>**/*.xml</include> 151 </includes> 152 <filtering>false</filtering> 153 </resource> 154 <resource> 155 <directory>src/main/resources</directory> 156 <includes> 157 <include>**/*.properties</include> 158 <include>**/*.xml</include> 159 </includes> 160 <filtering>false</filtering> 161 </resource> 162 </resources> 163 </build>

 

四、配置web.xml

在web.xml檔案中註冊SSM框架,新增字元編碼過濾器。

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
 5          http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
 6          version="4.0">
 7 
 8     <!-- 新增字元編碼過濾器-->
 9     <filter>
10         <filter-name>encode</filter-name>
11         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
12         <init-param>
13             <param-name>encoding</param-name>
14             <param-value>UTF-8</param-value>
15         </init-param>
16         <init-param>
17             <param-name>forceRequestEncoding</param-name>
18             <param-value>true</param-value>
19         </init-param>
20         <init-param>
21             <param-name>forceResponseEncoding</param-name>
22             <param-value>true</param-value>
23         </init-param>
24     </filter>
25     <filter-mapping>
26         <filter-name>encode</filter-name>
27         <url-pattern>/*</url-pattern>
28     </filter-mapping>
29 
30     <!-- 註冊SpringMVC框架-->
31     <servlet>
32         <servlet-name>springmvc</servlet-name>
33         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
34         <init-param>
35             <param-name>contextConfigLocation</param-name>
36             <param-value>classpath:springmvc.xml</param-value>
37         </init-param>
38     </servlet>
39     <servlet-mapping>
40         <servlet-name>springmvc</servlet-name>
41         <url-pattern>*.action</url-pattern>
42     </servlet-mapping>
43 
44     <!-- 註冊Spring框架-->
45     <listener>
46         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
47     </listener>
48     <context-param>
49         <param-name>contextConfigLocation</param-name>
50         <param-value>classpath:applicationContext_*.xml</param-value>
51     </context-param>
52 
53 </web-app>

 

五、新建jdbc.properties

在resources目錄下新建jdbc.properties檔案來連線資料庫。

jdbc.properties:

1 jdbc.driver=com.mysql.cj.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/資料庫?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
3 jdbc.username=root
4 jdbc.password=密碼

 

六、新建SqlMapperConfig.xml

在resources目錄下新建SqlMapperConfig.xml檔案進行分頁外掛的配置。

SqlMapperConfig.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 <!--        分頁外掛的配置-->
6     <plugins>
7         <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
8     </plugins>
9 </configuration>

 

七、新建applicationContext_dao.xml

在resources目錄下新建applicationContext_dao.xml檔案進行資料訪問層的配置。

applicationContext_dao.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        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
 6 
 7     <!--    讀取jdbc.properties屬性檔案 -->
 8     <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
 9 
10     <!--    建立資料來源-->
11     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
12         <property name="driverClassName" value="${jdbc.driver}"></property>
13         <property name="url" value="${jdbc.url}"></property>
14         <property name="username" value="${jdbc.username}"></property>
15         <property name="password" value="${jdbc.password}"></property>
16     </bean>
17 
18     <!--    建立SqlSessionFactoryBean-->
19     <bean class="org.mybatis.spring.SqlSessionFactoryBean">
20         <!--    配置資料來源-->
21         <property name="dataSource" ref="dataSource"></property>
22         <!-- 配置Mybatis核心配置檔案-->
23         <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
24         <!-- 配置實體類-->
25         <property name="typeAliasesPackage" value="xxx.xxx.pojo"></property>
26     </bean>
27 
28     <!--    建立mapper檔案的掃描器-->
29     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
30         <property name="basePackage" value="xxx.xxx.mapper"></property>
31     </bean>
32 </beans>

 

八、新建applicationContext_service.xml

在resources目錄下新建applicationContext_service.xml檔案進行業務邏輯層的配置。

applicationContext_service.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:tx="http://www.springframework.org/schema/tx"
 6        xmlns:aop="http://www.springframework.org/schema/aop"
 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        https://www.springframework.org/schema/context/spring-context.xsd
11        http://www.springframework.org/schema/tx
12        http://www.springframework.org/schema/tx/spring-tx.xsd
13        http://www.springframework.org/schema/aop
14        https://www.springframework.org/schema/aop/spring-aop.xsd">
15 
16     <!-- 設定業務邏輯層的包掃描器,目的是在指定的路徑下,使用@Service註解的類,Spring負責建立物件,並新增依賴-->
17     <context:component-scan base-package="xxx.xxx.service"></context:component-scan>
18 
19     <!-- 設定事務管理器-->
20     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
21         <property name="dataSource" ref="dataSource"></property>
22     </bean>
23 
24     <!-- 新增事務的切面-->
25     <tx:advice id="myadvice" transaction-manager="transactionManager">
26         <tx:attributes>
27             <tx:method name="*select*" read-only="true"/>
28             <tx:method name="*find*" read-only="true"/>
29             <tx:method name="*get*" read-only="true"/>
30             <tx:method name="*search*" read-only="true"/>
31             <tx:method name="*insert*" propagation="REQUIRED"/>
32             <tx:method name="*save*" propagation="REQUIRED"/>
33             <tx:method name="*add*" propagation="REQUIRED"/>
34             <tx:method name="*delete*" propagation="REQUIRED"/>
35             <tx:method name="*remove*" propagation="REQUIRED"/>
36             <tx:method name="*clear*" propagation="REQUIRED"/>
37             <tx:method name="*update*" propagation="REQUIRED"/>
38             <tx:method name="*modify*" propagation="REQUIRED"/>
39             <tx:method name="*change*" propagation="REQUIRED"/>
40             <tx:method name="*set*" propagation="REQUIRED"/>
41             <tx:method name="*" propagation="SUPPORTS"/>
42         </tx:attributes>
43     </tx:advice>
44 
45     <!-- 完成切面和切入點的織入-->
46     <aop:config>
47         <aop:pointcut id="mypointcut" expression="execution(* xxx.xxx.service.*.*(..))"/>
48     </aop:config>
49 </beans>

 

九、新建springmvc.xml

在resources目錄下新建springmvc.xml檔案,配置springmvc的框架。

springmvc.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:mvc="http://www.springframework.org/schema/mvc"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
 7 
 8     <!-- 設定包掃描器-->
 9     <context:component-scan base-package="xxx.xxx.controller"></context:component-scan>
10 
11     <!-- 設定檢視解析器-->
12     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
13         <property name="prefix" value="/xxx/"></property>
14         <property name="suffix" value=".jsp"></property>
15     </bean>
16 
17     <!-- 設定檔案上傳核心元件-->
18     <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
19 
20     </bean>
21 
22     <!-- 設定註解驅動-->
23     <mvc:annotation-driven></mvc:annotation-driven>
24 
25 </beans>

 

十、配置檔案目錄