RBAC项目:集成SSM框架

添加依赖

在atcrowdfunding-parent的pom.xml中添加如下依赖

<dependencies>                                                              
    <dependency>                                                            
        <groupId>javax.servlet</groupId>                        
        <artifactId>servlet-api</artifactId>                          
        <version>2.5</version>                              
    </dependency>                                                           
    <dependency>                                                            
        <groupId>javax.servlet.jsp</groupId>                        
        <artifactId>jsp-api</artifactId>                       
        <version>2.1.3-b06</version>                              
    </dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframework</groupId>                        
        <artifactId>spring-core</artifactId>                          
        <version>4.0.0.RELEASE</version>                              
    </dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframework</groupId>                        
        <artifactId>spring-context</artifactId>                       
        <version>4.0.0.RELEASE</version>                              
    </dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframework</groupId>                        
        <artifactId>spring-jdbc</artifactId>                          
        <version>4.0.0.RELEASE</version>                              
    </dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframework</groupId>                        
        <artifactId>spring-orm</artifactId>                           
        <version>4.0.0.RELEASE</version>                              
    </dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframework</groupId>                        
        <artifactId>spring-web</artifactId>                           
        <version>4.0.0.RELEASE</version>                              
    </dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframework</groupId>                        
        <artifactId>spring-webmvc</artifactId>                        
        <version>4.0.0.RELEASE</version>                              
    </dependency>                                                           
    <dependency>                                                            
        <groupId>com.mchange</groupId>                                
        <artifactId>c3p0</artifactId>                                 
        <version>0.9.2</version>                                      
    </dependency>                                                           
    <dependency>                                                            
        <groupId>cglib</groupId>                                      
        <artifactId>cglib</artifactId>                                
        <version>2.2</version>                                        
    </dependency>                                                           
    <dependency>                                                            
        <groupId>org.aspectj</groupId>                                
        <artifactId>aspectjweaver</artifactId>                        
        <version>1.6.8</version>                                      
    </dependency>                                                           
                                                                                  
    <!-- Spring整合MyBatis -->                                              
    <!-- MyBatis中延迟加载需要使用Cglib -->                                 
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->         
    <dependency>                                                            
        <groupId>org.mybatis</groupId>                                
        <artifactId>mybatis</artifactId>                              
        <version>3.2.8</version>                                      
    </dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.mybatis</groupId>                                
        <artifactId>mybatis-spring</artifactId>                       
        <version>1.2.2</version>                                      
    </dependency>                                                           
                                                                                  
    <!-- 控制日志输出:结合log4j -->                                        
    <dependency>                                                            
        <groupId>log4j</groupId>                                      
        <artifactId>log4j</artifactId>                                
        <version>1.2.17</version>                                     
    </dependency>                                                           
    <dependency>                                                            
        <groupId>org.slf4j</groupId>                                  
        <artifactId>slf4j-api</artifactId>                            
        <version>1.7.7</version>                                      
    </dependency>                                                           
    <dependency>                                                            
        <groupId>org.slf4j</groupId>                                  
        <artifactId>slf4j-log4j12</artifactId>                        
        <version>1.7.7</version>                                      
    </dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>mysql</groupId>                                      
        <artifactId>mysql-connector-java</artifactId>                 
        <version>5.1.37</version>                                     
    </dependency>                                                           
    <dependency>                                                            
        <groupId>jstl</groupId>                                       
        <artifactId>jstl</artifactId>                                 
        <version>1.2</version>                                        
    </dependency>                                                           
                                                                                  
    <!-- ********其他****************************** -->                     
                                                                                  
    <!-- Ehcache二级缓存 -->                                                
    <dependency>                                                            
        <groupId>net.sf.ehcache</groupId>                             
        <artifactId>ehcache</artifactId>                              
        <version>1.6.2</version>                                      
    </dependency>                                                           
                                                                                  
                                                                                  
    <!-- 石英调度 - 开始 -->                                                
    <dependency>                                                            
        <groupId>org.quartz-scheduler</groupId>                       
        <artifactId>quartz</artifactId>                               
        <version>1.8.5</version>                                      
    </dependency>                                                           
    <dependency>                                                            
        <groupId>org.springframework</groupId>                        
        <artifactId>spring-context-support</artifactId>               
        <version>4.0.0.RELEASE</version>                              
    </dependency>                                                           
    <dependency>                                                            
        <groupId>commons-collections</groupId>                        
        <artifactId>commons-collections</artifactId>                  
        <version>3.1</version>                                        
    </dependency>                                                           
    <!-- 石英调度 - 结束 -->                                                
                                                                                  
    <dependency>                                                            
        <groupId>org.codehaus.jackson</groupId>                       
        <artifactId>jackson-mapper-asl</artifactId>                   
        <version>1.9.2</version>                                      
    </dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.apache.poi</groupId>                             
        <artifactId>poi</artifactId>                                  
        <version>3.9</version>                                        
    </dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.jfree</groupId>                                  
        <artifactId>jfreechart</artifactId>                           
        <version>1.0.19</version>                                     
    </dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>commons-fileupload</groupId>                         
        <artifactId>commons-fileupload</artifactId>                   
        <version>1.3.1</version>                                      
    </dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.freemarker</groupId>                             
        <artifactId>freemarker</artifactId>                           
        <version>2.3.19</version>                                     
    </dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.activiti</groupId>                               
        <artifactId>activiti-engine</artifactId>                      
        <version>5.15.1</version>                                     
    </dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.activiti</groupId>                               
        <artifactId>activiti-spring</artifactId>                      
        <version>5.15.1</version>                                     
    </dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.apache.commons</groupId>                         
        <artifactId>commons-email</artifactId>                        
        <version>1.3.1</version>                                      
    </dependency>                                                           
                                                                                  
    <dependency>                                                            
        <groupId>org.activiti</groupId>                               
        <artifactId>activiti-explorer</artifactId>                    
        <version>5.15.1</version>                                     
        <exclusions>                                                        
            <exclusion>                                                     
                <artifactId>groovy-all</artifactId>                   
                <groupId>org.codehaus.groovy</groupId>                
            </exclusion>                                                    
        </exclusions>                                                       
    </dependency>                                                           
</dependencies> 

因为依赖传递,所以就可以在其他项目(比如atcrowdfunding-web)就看到这些依赖

集成spring框架

spring是整个系统框架的核心,将前端请求数据的处理和数据库的数据操作整合在一起。

  • 1、打开atcrowdfunding-web\src\main\webapp\WEB-INF中的web.xml,在标签中增加如下配置信息
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <!--
		Spring环境构建时需要读取web应用的初始化参数contextConfigLocation,
		从classpath中读取配置文件spring/spring-*.xml
	-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/spring-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>
  • 2、打开atcrowdfunding-web\src\main\resources, 创建文件夹spring,并在sping文件夹下创建
    spring-context.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:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">


	<!--
	    1、spring框架的核心是构建对象,整合对象之间的关系[IOC]以及扩展对象功能[AOP], 所以
		需要告诉spring框架去哪里找到相应对象
		2、 扫描配置红为什么要排除Controller注解
			因为Controller注解的作用是生命控制器类,这个类应该由springMVC框架进行管理和组织,
			而不是spring管理,所以不需要spring扫描
	-->
    <context:component-scan base-package="com.atguigu.*" >
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

集成springMVC框架

SpringMVC框架用于处理系统中数据的流转和控制

  • 1、打开atcrowdfunding-web\src\main\webapp\WEB-INF中的web.xml,在标签中增加如下配置信息
	<!--
		集成springMVC框架
	-->
    <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:spring/springmvc-context.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>
  • 2、打开atcrowdfunding-web\src\main\resources\spring,在spring文件夹下创建
    springmvc-context.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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
		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-4.0.xsd">
	
	<!--
	   SpringMVC用于处理系统中数据的流转和控制, 所以需要增加控制器Controller注解扫描的配置
	 -->
    <context:component-scan base-package="com.atguigu.*" use-default-filters="false" >
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!--
       配置:SpringMVC静态资源不拦截
       备注:实际项目中静态资源和动态资源不会放在一起,也就是不会放置在服务器中,所以可以不配置这个选项
    -->
    <mvc:default-servlet-handler/>
    <mvc:annotation-driven />
    
    
    <!--
		如果springMVC需要页面跳转,那么需要配置相应的视图解析器
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    
    <!--
		1、如果SpringMVC框架数据处理为JSON字符串而不是视图,那么为了方便浏览器对响应的字符串进行处理,
		需要明确字符串的类型以及编码方式
		2、备注:如果增加了mvc:default-servlet-handler标签,那么可以不配置
    -->
        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
        <property name="messageConverters" >
            <list>
                <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" >
                    <property name="supportedMediaTypes" >
                        <list>
                            <value>application/json;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>
    
    
    <!--
		文件上传解析器:上传文件
    -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" p:defaultEncoding="UTF-8" >
        <property name="maxUploadSize" value="2097152"/>
        <property name="resolveLazily" value="true"/>
    </bean>
</beans>    

集成mybatis

MyBatis作用是:处理业务也数据库之间的数据交互,因此创建对象和管理对象生命周期的职责可以委托spring框架完成

  • 1、打开atcrowdfunding-web\src\main\resources\spring\spring-context.xml, 增加配置项:
    <!--
		  spring创建mybatis的核心对象
	-->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
        <property name="configLocation" value="classpath:mybatis/config.xml" />
        <property name="dataSource" ref="dataSource" />
        <property name="mapperLocations" >
            <list>
                <value>classpath*:mybatis/mapper-*.xml</value>
            </list>
        </property>
    </bean>
    
     <!--
		  mybatis关联mysql数据库:使用连接池
	-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/atcrowdfunding?rewriteBatchedStatements=true&amp;useUnicode=true&amp;characterEncoding=utf8"/>
        <property name="user" value="root"/>
        <property name="password" value="123456"/>
    </bean>
    
    
    <!--
		 扫描dao包
	-->
     <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
        <property name="basePackage" value="com.atguigu.atcrowdfunding.**.dao" />
    </bean>
    
    
       <!--
		增加事务处理配置:spring框架采用声明式事务,通过AOP的方式将事务增加到业务中
    -->
     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager" >
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" rollback-for="java.lang.Exception" />
            <tx:method name="query*" read-only="true" />
        </tx:attributes>
    </tx:advice>    
    <aop:config>
        <aop:advisor advice-ref="transactionAdvice" pointcut="execution(* com.atguigu..*Service.*(..))"/>
    </aop:config>
  • 2、增加mybatis的核心配置文件:打开atcrowdfunding-web\src\main\resources, 创建mybatis文件夹,
    然后在mybatis文件夹中创建文件config.xml,其内容如下
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <typeAliases>
        
    </typeAliases>
</configuration>

测试框架是否成功集成

测试springMVC框架

测试能否解析视图:

  • 在 atcrowdfunding-web/src/main/java/com/atguigu/atcrowdfunding/controller目录中增加UserController
package com.atguigu.atcrowdfunding.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@RequestMapping("/index")
	public String index() {
		return "index";
	}
}

  • 添加视图
    在 atcrowdfunding-web/…/WEB-INF/jsp/下面写一个jsp页面。

在这里插入图片描述

测试能否解析json

package com.atguigu.atcrowdfunding.controller;

import java.util.HashMap;
import java.util.Map;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/user")
public class UserController {
	
	@RequestMapping("/index")
	public String index() {
		return "index";
	}
	
	@ResponseBody
	@RequestMapping("/json")
	public Object json() {
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", "张珊");
		
		return map;
	}
}

  • 将web项目发布到服务器中,启动服务器,浏览器中分别输入路径http://127.0.0.1:8080/应用路径名称/user/index,http://127.0.0.1:8080/应用路径名称/user/json进行测试。

如果访问成功,说明项目中SpringMVC框架集成成功。

测试spring框架

  • 1、测试前,在mysql数据库中增加atcrowdfunding库及t_user表。
CREATE DATABASE `atcrowdfunding`;

CREATE TABLE t_user (
   id int(11) NOT NULL auto_increment  ,
   PRIMARY KEY (id)
) ENGINE=InnoDB auto_increment =5 DEFAULT CHARSET=utf8;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值