基于maven项目的SSM整合

狂风中的少年 提交于 2020-04-08 09:41:43

1.添加基本的依赖坐标(项目如需有具体别的依赖,根据实际情况添加)

  1 <!-- 统一管理jar包版本 -->
  2     <properties>
  3       <spring.version>5.0.2.RELEASE</spring.version>
  4       <slf4j.version>1.6.6</slf4j.version>
  5       <log4j.version>1.2.12</log4j.version>
  6       <shiro.version>1.2.3</shiro.version>
  7       <mysql.version>5.1.6</mysql.version>
  8       <mybatis.version>3.4.5</mybatis.version>
  9       <spring.security.version>5.0.1.RELEASE</spring.security.version>
 10     </properties>
 11     <!-- 锁定jar包版本 -->
 12     <dependencyManagement>
 13       <dependencies>
 14         <dependency>
 15           <groupId>org.springframework</groupId>
 16           <artifactId>spring-context</artifactId>
 17           <version>${spring.version}</version>
 18         </dependency>
 19         <dependency>
 20           <groupId>org.springframework</groupId>
 21           <artifactId>spring-web</artifactId>
 22           <version>${spring.version}</version>
 23         </dependency>
 24         <dependency>
 25           <groupId>org.springframework</groupId>
 26           <artifactId>spring-webmvc</artifactId>
 27           <version>${spring.version}</version>
 28         </dependency>
 29         <dependency>
 30           <groupId>org.springframework</groupId>
 31           <artifactId>spring-tx</artifactId>
 32           <version>${spring.version}</version>
 33         </dependency>
 34         <dependency>
 35           <groupId>org.springframework</groupId>
 36           <artifactId>spring-test</artifactId>
 37           <version>${spring.version}</version>
 38         </dependency>
 39         <dependency>
 40           <groupId>org.mybatis</groupId>
 41           <artifactId>mybatis</artifactId>
 42           <version>${mybatis.version}</version>
 43         </dependency>
 44       </dependencies>
 45     </dependencyManagement>
 46 
 47     <!-- 项目依赖jar包 -->
 48     <dependencies>
 49       <!-- spring -->
 50       <dependency>
 51         <groupId>org.aspectj</groupId>
 52         <artifactId>aspectjweaver</artifactId>
 53         <version>1.6.8</version>
 54       </dependency>
 55       <dependency>
 56         <groupId>org.springframework</groupId>
 57         <artifactId>spring-aop</artifactId>
 58         <version>${spring.version}</version>
 59       </dependency>
 60       <dependency>
 61         <groupId>org.springframework</groupId>
 62         <artifactId>spring-context</artifactId>
 63         <version>${spring.version}</version>
 64       </dependency>
 65       <dependency>
 66         <groupId>org.springframework</groupId>
 67         <artifactId>spring-context-support</artifactId>
 68         <version>${spring.version}</version>
 69       </dependency>
 70       <dependency>
 71         <groupId>org.springframework</groupId>
 72         <artifactId>spring-web</artifactId>
 73         <version>${spring.version}</version>
 74       </dependency>
 75       <dependency>
 76         <groupId>org.springframework</groupId>
 77         <artifactId>spring-orm</artifactId>
 78         <version>${spring.version}</version>
 79       </dependency>
 80       <dependency>
 81         <groupId>org.springframework</groupId>
 82         <artifactId>spring-beans</artifactId>
 83         <version>${spring.version}</version>
 84       </dependency>
 85       <dependency>
 86         <groupId>org.springframework</groupId>
 87         <artifactId>spring-core</artifactId>
 88         <version>${spring.version}</version>
 89       </dependency>
 90       <dependency>
 91         <groupId>org.springframework</groupId>
 92         <artifactId>spring-test</artifactId>
 93         <version>${spring.version}</version>
 94       </dependency>
 95       <dependency>
 96         <groupId>org.springframework</groupId>
 97         <artifactId>spring-webmvc</artifactId>
 98         <version>${spring.version}</version>
 99       </dependency>
100       <dependency>
101         <groupId>org.springframework</groupId>
102         <artifactId>spring-tx</artifactId>
103         <version>${spring.version}</version>
104       </dependency>
105       <dependency>
106         <groupId>junit</groupId>
107         <artifactId>junit</artifactId>
108         <version>4.12</version>
109         <scope>test</scope>
110       </dependency>
111       <dependency>
112         <groupId>mysql</groupId>
113         <artifactId>mysql-connector-java</artifactId>
114         <version>${mysql.version}</version>
115       </dependency>
116       <dependency>
117         <groupId>javax.servlet</groupId>
118         <artifactId>javax.servlet-api</artifactId>
119         <version>3.1.0</version>
120         <scope>provided</scope>
121       </dependency>
122       <dependency>
123         <groupId>javax.servlet.jsp</groupId>
124         <artifactId>jsp-api</artifactId>
125         <version>2.0</version>
126         <scope>provided</scope>
127       </dependency>
128       <dependency>
129         <groupId>jstl</groupId>
130         <artifactId>jstl</artifactId>
131         <version>1.2</version>
132       </dependency>
133       <!-- log start -->
134       <dependency>
135         <groupId>log4j</groupId>
136         <artifactId>log4j</artifactId>
137         <version>${log4j.version}</version>
138       </dependency>
139       <dependency>
140         <groupId>org.slf4j</groupId>
141         <artifactId>slf4j-api</artifactId>
142         <version>${slf4j.version}</version>
143       </dependency>
144       <dependency>
145         <groupId>org.slf4j</groupId>
146         <artifactId>slf4j-log4j12</artifactId>
147         <version>${slf4j.version}</version>
148       </dependency>
149       <!-- log end -->
150       <dependency>
151         <groupId>org.mybatis</groupId>
152         <artifactId>mybatis</artifactId>
153         <version>${mybatis.version}</version>
154       </dependency>
155       <dependency>
156         <groupId>org.mybatis</groupId>
157         <artifactId>mybatis-spring</artifactId>
158         <version>1.3.0</version>
159       </dependency>
160       <dependency>
161         <groupId>c3p0</groupId>
162         <artifactId>c3p0</artifactId>
163         <version>0.9.1.2</version>
164         <type>jar</type>
165         <scope>compile</scope>
166       </dependency>
167       <dependency>
168         <groupId>com.github.pagehelper</groupId>
169         <artifactId>pagehelper</artifactId>
170         <version>5.1.2</version>
171       </dependency>
172       <dependency>
173         <groupId>org.springframework.security</groupId>
174         <artifactId>spring-security-web</artifactId>
175         <version>${spring.security.version}</version>
176       </dependency>
177       <dependency>
178         <groupId>org.springframework.security</groupId>
179         <artifactId>spring-security-config</artifactId>
180         <version>${spring.security.version}</version>
181       </dependency>
182       <dependency>
183         <groupId>org.springframework.security</groupId>
184         <artifactId>spring-security-core</artifactId>
185         <version>${spring.security.version}</version>
186       </dependency>
187       <dependency>
188         <groupId>org.springframework.security</groupId>
189         <artifactId>spring-security-taglibs</artifactId>
190         <version>${spring.security.version}</version>
191       </dependency>
192       <dependency>
193         <groupId>com.alibaba</groupId>
194         <artifactId>druid</artifactId>
195         <version>1.0.9</version>
196       </dependency>
197       <dependency>
198         <groupId>junit</groupId>
199         <artifactId>junit</artifactId>
200         <version>4.12</version>
201       </dependency>
202     </dependencies>
203     <!-- 添加tomcat7插件 -->
204     <build>
205       <plugins>
206         <plugin>
207           <groupId>org.apache.tomcat.maven</groupId>
208           <artifactId>tomcat7-maven-plugin</artifactId>
209           <version>2.2</version>
210         </plugin>
211       </plugins>
212     </build>
213 </project>

2.配置文件 applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--dao层配置文件开始-->
    <!--配置连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql:///maven"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

    <!--配置生产SqlSession对象的工厂-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--扫描pojo包,给包下所有pojo对象起别名-->
        <property name="typeAliasesPackage" value="com.itheima.domain"/>
    </bean>

    <!--扫描接口包路径,生成包下所有接口的代理对象,并且放入spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.dao"/>
    </bean>
    <!--dao层配置文件结束-->


    <!--service层配置文件开始-->

    <!--组件扫描配置-->
    <context:component-scan base-package="com.itheima.service"/>

    <!--aop面向切面编程,切面就是切入点和通知的组合-->
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务的通知-->
    <tx:advice id="advice">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--配置切面-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.itheima.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
    </aop:config>
    <!--service层配置文件结束-->
    
</beans>

3.配置springmvc.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx.xsd
                http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--组件扫描-->
    <context:component-scan base-package="com.itheima.controller"/>

    <!--处理器映射器,处理器适配器-->
    <mvc:annotation-driven/>

    <!--视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!--释放静态资源-->
    <mvc:default-servlet-handler/>


</beans>

4.配置web.xml的配置文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <!--编码过滤器-->
    <filter>
        <filter-name>encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!--配置spring核心监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--重新指定spring配置文件的路径-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!--springmvc的核心servlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!