Fork me on Github

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>

 

十、配置文件目录

posted @ 2022-03-18 22:55  昂昂呀  阅读(124)  评论(0)    收藏  举报