java整合SSM框架


    • 使用Myeclipse搭建maven项目 

    1. 准备工作

      安装maven

  官网下载安装(http://maven.apache.org/

        配置环境变量

             

      配置完后,使用命令行输入mvn -version查看是否配置成功,出现以下界面表示配置成功。

          

    2. 在MyEclipse中配置maven

        打开MyEclipse2015————点击菜单栏Window———选项栏Preference ,搜索Maven,进入Installations,点击Add引入安装的maven。

        

        进入User Setting,设置Global Setting,选中安装的maven的settings.xml配置文件(以上步骤已经完成maven配置)

        


      

    3. 创建项目(检测是否配置成功,这里创建的是web项目)

        

        点击项目右键->Run as->Maven install将依赖install至本地maven库,有可能会报:

        -Dmaven.multiModuleProjectDirectorysystem propery is not set. Check $M2_HOME environment variable and mvn scriptmatch.,

        表示JDK版本和maven有冲突,更改一下jdk版本,并且加上-Dmaven.multiModuleProjectDirectory=$M2_HOME即可;

        如运行过程中控制台如下图所示,表示成功。

        

        

    • 在maven项目中搭建spring+springMVC+MyBatis

      修改pom.xml添加对应的包依赖


        
            javax.servlet
            javax.servlet-api
            3.0.1
            compile
        
        
            jstl
            jstl
            1.2
        

        
        
            c3p0
            c3p0
            0.9.1
        
        
        
        
            com.alibaba
            druid
            1.0.25
            
                
                  com.alibaba
                  jconsole
                
                
                  com.alibaba
                  tools
                
             
        

        
        
    org.springframework
    spring-core
    4.1.7.RELEASE


    org.springframework
    spring-beans
    4.1.7.RELEASE


     org.springframework
     spring-tx
     4.1.7.RELEASE
    

    org.springframework
    spring-context
    4.1.7.RELEASE


    org.springframework
    spring-context-support
    4.1.7.RELEASE

 

    org.springframework
    spring-web
    4.1.7.RELEASE

 

    org.springframework
    spring-webmvc
    4.1.7.RELEASE

 

    org.springframework
    spring-aop
    4.1.7.RELEASE

 
 

    org.springframework
    spring-aspects
    4.1.7.RELEASE

 

    org.springframework
    spring-jdbc
    4.1.7.RELEASE


        
        
            org.aspectj
            aspectjweaver
            1.8.5
        
        
        
        
            junit
            junit
            4.12
        

        
        
            org.mybatis
            mybatis
            3.4.1
        
        
        
        
            log4j
            log4j
            1.2.17
        

        
        
            org.mybatis
            mybatis-spring
            1.3.0
        

        
        
            mysql
            mysql-connector-java
            5.1.38
        
        
            com.mchange
            mchange-commons-java
            0.2.10
        

        
        
            commons-codec
            commons-codec
            1.10
        
        
        
        
            dom4j
            dom4j
            1.6.1
        
        
            com.thoughtworks.xstream
            xstream
            1.3.1
        

        
        
            net.sf.json-lib
            json-lib
            2.4
            jdk15
        
        
            xom
            xom
            1.2.5
        

        
        
            org.apache.httpcomponents
            httpclient
            4.2.5
        

        
        
            com.github.pagehelper
            pagehelper
            4.0.0
        
        
        
        
            tk.mybatis
            mapper
            3.3.8
        
        
            commons-httpclient
            commons-httpclient
            3.1
            
 
         
        
        org.jdom
        jdom
        1.1.3
        
修改pom.xml,添加ssm相关jar包依赖

      修改web.xml文件

  
    index.html  
    index.htm  
    index.jsp  
    default.html  
    default.htm  
    default.jsp  
    
      
    class>org.springframework.web.context.ContextLoaderListenerclass>  
    
        
     
     contextConfigLocation  
     classpath:applicationContext*.xml  
    
   
    
    
        springDispatcherServlet
        class>org.springframework.web.servlet.DispatcherServletclass>
        
        
            contextConfigLocation
            classpath:springmvc.xml
        
        1
    
    
        springDispatcherServlet
        /
    
修改web.xml,配置驱动及过滤器和加载Spring配置文件

      创建并配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

    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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">


    
    package="cn.ft.mapper,cn.ft.service.impl,cn.ft.service.rest.impl">
    
    

    
    class="com.mchange.v2.c3p0.ComboPooledDataSource">
        
        
        
        
         
        
    

    class="org.mybatis.spring.SqlSessionFactoryBean">
        
        
        
        
        
    
    
    
    
    
    class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
    
    
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
    
    
    
        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    

    
    
        
            expression="execution(* cn.ft.service.impl.*Impl.*(..))" />
        
            advice-ref="transactionAdvice" />
    

applicationContext.xml

      创建并配置springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">


    
    package="cn.ft.controller">


    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        
        
    
    
    
    
    
    
    

    <bean
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
        
            
                
                    /refuse
                
            
        
    
        
                
                    class="org.springframework.http.converter.StringHttpMessageConverter">
                        
                            
                                application/json;charset=UTF-8
                            
                        
                    
                
            
    

springmvc.xml

      配置数据源db.properties

driverClassName=com.mysql.jdbc.Driver
validationQuery=SELECT 1

jdbc_url=jdbc\:mysql\://localhost\:3306/ft?useUnicode\=true&characterEncoding\=UTF-8&zeroDateTimeBehavior\=convertToNull
jdbc_username=root
jdbc_password=8888
db.properties

      配置mybatis-config.xm

<?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">

  
  
     
  
  
  
     
     
    
    <package name="cn.ft.model"/>
  
  
  
  
  
mybatis-config.xml

      配置log4j.properties

log4j.rootCategory=DEBUG, stdout , R
 
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
 
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File=D\:\\Tomcat 5.5\\logs\\qc.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n