SSM框架配置


一、新建Maven工程

二、修改目录

三、修改pom.xml

添加SSM所有依赖。

pom.xml:

  1   <dependencies>
  2     
  3     <dependency>
  4       <groupId>org.springframeworkgroupId>
  5       <artifactId>spring-contextartifactId>
  6       <version>${spring.version}version>
  7     dependency>
  8     <dependency>
  9       <groupId>org.springframeworkgroupId>
 10       <artifactId>spring-beansartifactId>
 11       <version>${spring.version}version>
 12     dependency>
 13     <dependency>
 14       <groupId>org.springframeworkgroupId>
 15       <artifactId>spring-webmvcartifactId>
 16       <version>${spring.version}version>
 17     dependency>
 18     <dependency>
 19       <groupId>org.springframeworkgroupId>
 20       <artifactId>spring-jdbcartifactId>
 21       <version>${spring.version}version>
 22     dependency>
 23     <dependency>
 24       <groupId>org.springframeworkgroupId>
 25       <artifactId>spring-aspectsartifactId>
 26       <version>${spring.version}version>
 27     dependency>
 28     <dependency>
 29       <groupId>org.springframeworkgroupId>
 30       <artifactId>spring-jmsartifactId>
 31       <version>${spring.version}version>
 32     dependency>
 33     <dependency>
 34       <groupId>org.springframeworkgroupId>
 35       <artifactId>spring-context-supportartifactId>
 36       <version>${spring.version}version>
 37     dependency>
 38     <dependency>
 39       <groupId>org.springframeworkgroupId>
 40       <artifactId>spring-testartifactId>
 41       <version>${spring.version}version>
 42     dependency>
 43     
 44     
 45     <dependency>
 46       <groupId>org.mybatisgroupId>
 47       <artifactId>mybatisartifactId>
 48       <version>${mybatis.version}version>
 49     dependency>
 50     <dependency>
 51       <groupId>org.mybatisgroupId>
 52       <artifactId>mybatis-springartifactId>
 53       <version>${mybatis.spring.version}version>
 54     dependency>
 55     <dependency>
 56       <groupId>com.github.miemiedevgroupId>
 57       <artifactId>mybatis-paginatorartifactId>
 58       <version>${mybatis.paginator.version}version>
 59     dependency>
 60     <dependency>
 61       <groupId>com.github.pagehelpergroupId>
 62       <artifactId>pagehelperartifactId>
 63       <version>${pagehelper.version}version>
 64     dependency>
 65     
 66     <dependency>
 67       <groupId>mysqlgroupId>
 68       <artifactId>mysql-connector-javaartifactId>
 69       <version>${mysql.version}version>
 70     dependency>
 71     
 72     <dependency>
 73       <groupId>com.alibabagroupId>
 74       <artifactId>druidartifactId>
 75       <version>${druid.version}version>
 76     dependency>
 77 
 78     
 79     <dependency>
 80       <groupId>junitgroupId>
 81       <artifactId>junitartifactId>
 82       <version>${junit.version}version>
 83       <scope>testscope>
 84     dependency>
 85 
 86     
 87     <dependency>
 88       <groupId>jstlgroupId>
 89       <artifactId>jstlartifactId>
 90       <version>${jstl.version}version>
 91     dependency>
 92     <dependency>
 93       <groupId>javax.servletgroupId>
 94       <artifactId>javax.servlet-apiartifactId>
 95       <version>3.0.1version>
 96       <scope>providedscope>
 97     dependency>
 98     <dependency>
 99       <groupId>javax.servletgroupId>
100       <artifactId>jsp-apiartifactId>
101       <scope>providedscope>
102       <version>${jsp-api.version}version>
103     dependency>
104     
105     
106     <dependency>
107       <groupId>com.fasterxml.jackson.coregroupId>
108       <artifactId>jackson-databindartifactId>
109       <version>${jackson.version}version>
110     dependency>
111     <dependency>
112       <groupId>org.jsongroupId>
113       <artifactId>jsonartifactId>
114       <version>20140107version>
115     dependency>
116 
117     
118     <dependency>
119       <groupId>commons-iogroupId>
120       <artifactId>commons-ioartifactId>
121       <version>2.4version>
122     dependency>
123     <dependency>
124       <groupId>commons-fileuploadgroupId>
125       <artifactId>commons-fileuploadartifactId>
126       <version>1.3.1version>
127     dependency>
128   dependencies>
129 
130   
131   <build>
132     <plugins>
133       <plugin>
134         <groupId>org.apache.maven.pluginsgroupId>
135         <artifactId>maven-compiler-pluginartifactId>
136         <configuration>
137           <source>1.8source>
138           <target>1.8target>
139           <encoding>UTF-8encoding>
140         configuration>
141       plugin>
142     plugins>
143     
144     
145     <resources>
146       <resource>
147         <directory>src/main/javadirectory>
148         <includes>
149           <include>**/*.propertiesinclude>
150           <include>**/*.xmlinclude>
151         includes>
152         <filtering>falsefiltering>
153       resource>
154       <resource>
155         <directory>src/main/resourcesdirectory>
156         <includes>
157           <include>**/*.propertiesinclude>
158           <include>**/*.xmlinclude>
159         includes>
160         <filtering>falsefiltering>
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>encodefilter-name>
11         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
12         <init-param>
13             <param-name>encodingparam-name>
14             <param-value>UTF-8param-value>
15         init-param>
16         <init-param>
17             <param-name>forceRequestEncodingparam-name>
18             <param-value>trueparam-value>
19         init-param>
20         <init-param>
21             <param-name>forceResponseEncodingparam-name>
22             <param-value>trueparam-value>
23         init-param>
24     filter>
25     <filter-mapping>
26         <filter-name>encodefilter-name>
27         <url-pattern>/*url-pattern>
28     filter-mapping>
29 
30     
31     <servlet>
32         <servlet-name>springmvcservlet-name>
33         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
34         <init-param>
35             <param-name>contextConfigLocationparam-name>
36             <param-value>classpath:springmvc.xmlparam-value>
37         init-param>
38     servlet>
39     <servlet-mapping>
40         <servlet-name>springmvcservlet-name>
41         <url-pattern>*.actionurl-pattern>
42     servlet-mapping>
43 
44     
45     <listener>
46         <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
47     listener>
48     <context-param>
49         <param-name>contextConfigLocationparam-name>
50         <param-value>classpath:applicationContext_*.xmlparam-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     
 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     
19     <bean class="org.mybatis.spring.SqlSessionFactoryBean">
20         
21         <property name="dataSource" ref="dataSource">property>
22         
23         <property name="configLocation" value="classpath:SqlMapConfig.xml">property>
24         
25         <property name="typeAliasesPackage" value="xxx.xxx.pojo">property>
26     bean>
27 
28     
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     
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>

十、配置文件目录

SSM