搭建SSM框架

2018-05-16  本文已影响0人  Cherrie_Lin

1. 创建maven项目,如下所示

1.第一步


image.png

2.第二步


image.png
3.第三步
image.png

2. 修改工程添加web.xml文件

3. 为项目配置maven工程

1.解压下载的安装包并放在一个特定的目录,例如我windows的存放路径为:“E:\apache-maven-3.3.9”

  1. 打开eclipse,选择windows--->preferences,找到Maven,修改默认的Maven插件
    image.png

4. 配置pom.xml文件,引入项目依赖的jar


以下为pom.xml中的文件详情,每一个依赖包有何作用已有备注。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.atguigu</groupId>
  <artifactId>ssm-crud</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  
  <dependencies>
        <!-- 引入项目依赖的jar包 --> 
        <!-- springMVC、Spring -->
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    
    
    <!-- Spring-JDBC -->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    
            
    <!-- Spring面向切面 编程-->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    
    <!-- spring 测试 -->
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.7.RELEASE</version>
    </dependency>
    
    <!-- 分页插件 -->
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>5.0.1</version>
    </dependency>



    <!-- Mybaties相关的Jar包 -->
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.2</version>
    </dependency>
    
    <!-- Mybaties逆向工程的JAR包 -->
    <!-- https://mvnrepository.com/artifact/org.mybatis.generator/mybatis-generator-core -->
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.5</version>
    </dependency>
    
    
    <!-- 返回json字符串的 支持 -->
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.8.8</version>
    </dependency>
    
    <!-- Mybatis整合Spring的适配包 -->
    <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.1</version>
    </dependency>
    
    
    
    
    <!-- 数据库连接池、驱动 -->
    <!-- https://mvnrepository.com/artifact/c3p0/c3p0 -->
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1</version>
    </dependency>
    
    
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <!--  <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.41</version>
    </dependency>
    -->
        
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.30</version>
    </dependency>
        
    
    <!--jstl,server-api,junit  -->
    <!-- https://mvnrepository.com/artifact/jstl/jstl -->
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <!-- tomcat中也有servlet-api包,会发生了冲突 解决方法:添加<scope>provided</scope>,因为provided表明该包只在编译和测试的时候用,所以,当启动tomcat的时候,就不会冲突了 -->
        <scope>provided</scope><!-- 告诉服务器已经提供 -->
    </dependency>

    
    <!-- junit -->
    <!-- https://mvnrepository.com/artifact/junit/junit -->   
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    
    </dependencies>
</project>

5. 配置文件

1 web项目------->web.xmlweb项目在启动时需要加载很多东西,包括监听器、过滤器、Servlet等

2 Spring ------>applicationContext.xml 主要描述了Spring相关配置信息,包括自动扫描Spring的注解类,引入数据源,创建创建SqlSessionFactory,装配DAO,配置Spring事物,AOP等

3 SpringMVC ---->springmvc-servlet.xml 主要描述了Spring MVC相关配置信息,包括配置控制器,即扫描controller所在的包(故在spring扫描的时候一般不扫描controller包),还有视图解析器、拦截器、映射器和适配器等等

4 Mybatis ---->mybatis-config.xml 主要描述了Mybaties的配置信息

5 数据库 ---->dbconfig.properties 主要描述了数据库的连接池、数据库名、用户名、密码等等,最主要的目的是为了开发过程的方便



配置文件内容如下所示:

1.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_3_0.xsd" id="WebApp_ID" version="3.0">
 
 <!--是描述性的文字,可以去掉,cloudGateway指本web项目的名字-->
  <display-name>cloudGateway</display-name>
  
  <!--以下配置的加载顺序:先 ServletContext >> context-param >> listener >> filter >> servlet >>  spring-->
  
  
  <!--------------------------------- 1.全局范围内环境参数初始化---------------------------->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext.xml</param-value>
  </context-param>
  
  
  
  <!---------------------------------- 2.监听器的配置 --------------------------------------->
  <listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <!-- 防止Spring内存溢出监听器  -->  
  <listener>  
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>  
  </listener> 
  
  
  
  <!----------------------------------- 3.过滤器的配置 -------------------------------------->
  <!-- 例:编码过滤器 -->
  <filter>  <!-- 用来声明filter的相关设定,过滤器可以截取和修改一个Servlet或JSP页面的请求或从一个Servlet或JSP页面发出的响应-->  
    <filter-name>CharacterEncodingFilter</filter-name>  <!--指定filter的名字-->
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  <!--定义filter的类的名称-->
    <async-supported>true</async-supported>     <!--设置是否启用异步支持-->
    <init-param><!--用来定义参数,若在Servlet可以使用下列方法来获得:Stringparam_name=getServletContext().getInitParamter("param-name里面的值");-->  
      <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所对应的URL-->
    <filter-name>CharacterEncodingFilter</filter-name>   <!--指定对应filter的名字-->
    <url-pattern>/*</url-pattern>     <!--指定filter所对应的URL,/*表示所有的url--> 
  </filter-mapping>
  
  
 <!------------------------------------------- 4.servlet配置 ------------------------------------------------------------>
    <servlet>  <!-- 用来申明一个servlet数据 -->
    <servlet-name>springmvc</servlet-name> <!-- 指定servlet的名称 -->
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!--指定servlet的类名称,这里配置了前端控制器-->
    <init-param><!--用来定义参数,可有多个init-param。在servlet类中通过getInitParamenter(String name)方法访问初始化参数 -->
      <param-name>contextConfigLocation</param-name>  <!--参数名称-->
      <param-value>classpath:spring/springmvc-servlet.xml</param-value>  <!--参数值-->
    </init-param>
    <load-on-startup>1</load-on-startup><!--当值为正数或零时:Servlet容器先加载数值小的servlet,再依次加载其他数值大的servlet.-->
  </servlet>
  
  <servlet-mapping><!--用来定义servlet所对应的URL--> 
    <servlet-name>default</servlet-name><!--指定servlet的名称-->
    <url-pattern>/static/*</url-pattern><!--指定servlet所对应的URL-->
  </servlet-mapping>
  
    <servlet-mapping><!--用来定义servlet所对应的URL--> 
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
 
  <!-----------------------------------------------其他配置------------------------------------------------->
  <!-----会话超时配置(单位为分钟)---->  
  <session-config><!--如果某个会话在一定时间未被访问,则服务器可以扔掉以节约内存-->  
      <session-timeout>120</session-timeout>  
  </session-config>  
  
  
  <!-----MIME类型配置---->  
  <mime-mapping><!--设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开-->  
      <extension>*.ppt</extension>            <!--扩展名名称-->  
      <mime-type>application/mspowerpoint</mime-type>         <!--MIME格式-->  
  </mime-mapping>  

 
  <!-- 欢迎页面,这个不多说 -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
 

<!-- 配置错误页面 -->
  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/views/errors/404.jsp</location>
  </error-page>
  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/WEB-INF/views/errors/error.jsp</location>
  </error-page>
 
</web-app>


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:p="http://www.springframework.org/schema/p"  
       xmlns:tx="http://www.springframework.org/schema/tx"  
       xmlns:aop="http://www.springframework.org/schema/aop"  
       xmlns:context="http://www.springframework.org/schema/context"  
       xsi:schemaLocation="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.xsd  
       http://www.springframework.org/schema/aop  
       http://www.springframework.org/schema/aop/spring-aop.xsd  
       http://www.springframework.org/schema/context  
       http://www.springframework.org/schema/context/spring-context.xsd"  
       default-init-method="init">
       
    <!-- 1.自动扫描 将标注Spring注解的类自动转化成bean-->   
    <context:component-scan base-package="com.macrosan.cloudGateway">
    <!-- 不扫描controller,其他都扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 2.开启基于AOP功能,AOP如果用不到,可以不写 -->
    <aop:aspectj-autoproxy proxy-target-class="true" />
    
    
    <!-- 3.数据源 ,事务控制 -->                   
    <!-- 引入jdbc配置文件 -->  
    <context:property-placeholder location="classpath:jdbc.properties" order= "1" ignore-unresolvable="true"/>
      <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="testConnectionOnCheckin" value="false"/>
        <property name="testConnectionOnCheckout" value="true"/>
    </bean>  
    
   
    <!-- 4.配置和MyBatis的整合,创建SqlSessionFactory,同时指定数据源 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">      
        <!-- 注入数据源 -->
        <property name="dataSource" ref="pooledDataSource"></property>
        <!-- 指定mybatis全局配置文件位置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 配置sql映射文件所在位置  注意:默认与mapper类位置相同   -->  
        <property name="mapperLocations" value="classpath:com/macrosan/cloudGateway/business/daoMapper/*.xml" />  
    </bean>  
    
    <!-- 5.装配dao接口,Mapper接口所在包名,Spring会自动查找其下的Mapper -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.macrosan.cloudGateway.business.dao" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> 
        <!-- 默认情况下会自动注入id=sqlSessionFactory的bean,也可以按上述方式指定sqlSessionFactory -->  
    </bean> 
     
     
    <!-- 6.配置一个可以执行批量的sqlSession -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
        <!-- 执行类型批量 -->
        <constructor-arg name="executorType" value="BATCH"></constructor-arg>
    </bean>
   
    <!-- 7.配置事务管理器,全注解声明式事务  -->  
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
       <!-- 控制住事务源 -->
        <property name="dataSource" ref="pooledDataSource"></property> 
    </bean> 
    
    <!--对@Transactional这个注解进行的驱动,这是基于注解的方式使用事务配置声明,这样在具体应用中可以指定对哪些方法使用事务-->
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

3. springmvc-servlet.xml

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

    <!-- 1.扫描controller层,使spring mvc认为该包下使用了@Controller注解的类是控制器 -->
    <context:component-scan base-package="com.macrosan.cloudGateway.business.controller" />
    <!-- 扫描service层 -->

    <!-- 2.视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".html"></property>
    </bean>
    
    <!-- 3.配置映射器和适配器,能支持springMVC更高级的一些功能,JSR303校验,快捷的ajax..映射动态请求  -->
    <mvc:annotation-driven />
    
    <!-- 4.启用默认Servlet,将springmvc不能处理的请求交给tomcat-->
    <mvc:default-servlet-handler/>
    
    <!-- 5.拦截器的配置 -->
    <mvc:interceptors>
       <bean class="com.macrosan.cloudGateway.business.core.LoginInterceptor" />
    </mvc:interceptors>
    
    <!-- 6. 静态资源处理 -->
    <mvc:resources mapping="/**" location="/"/>
    
    
</beans>

4.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>
    
    <settings>
        <!-- 开启驼峰命名规则 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings> 
    
    <!-- 类型别名 -->
    <typeAliases>
        <package name="com.macrosan.bean"/>
    </typeAliases>
  
  <!-- 分页插件 -->
  <plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 分页合理化 -->
        <property name="reasonable" value="true"/>
    </plugin>       
  </plugins>
  
  
  
</configuration>

5 dbconfig.properties

### ------链接mysql
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/cloudGateway?useUnicode=true&characterEncoding=utf-8
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456
上一篇下一篇

猜你喜欢

热点阅读