ssm整合

步骤

  1. 创建工程

  2.引入相应的Spring,Spring-mvc, mybatis,数据库连接池和驱动包,其他的(jstl,servlet-api,junit)包

  3.编写ssm整合的配置文件,

    3.1编写web.xml  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>tea</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>/jsp/index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 1: 启动Spring容器 -->
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mybatis.xml</param-value>
  </context-param>
   <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 2: 配置Springmvc的前端控制器,拦截所有请求 -->
  <servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <!-- 3: 字符编码过滤器 ,一定要放在所有过滤器前面-->
  <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>
  </filter>
  <filter-mapping>
      <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 4: 使用Rest风格URL ,将页面普通的post请求转换为指定的delete或者put请求 -->
   <!-- <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
     -->
</web-app>

    3.2编写spring配置文件

<?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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" >
    
    
    <!-- 默认的注解映射的支持 -->  
    <mvc:annotation-driven /> 
    <!-- 开启事务注解驱动 -->  
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" /> 
    <!-- 引入配置文件 -->
    <!-- 官方解释    A property resource configurer that resolves placeholders in bean property values of context definitions. It pulls values from a properties file into bean definitions.
    The default placeholder syntax follows the Ant / Log4J / JSP EL style:
    ${...} -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties" />
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        
    </bean>

    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自动扫描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath:org/clgc/tomorrow/entity/*.xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring会自动查找其下的类 
    Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="org.clgc.tomorrow.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

<!-- 自动扫描 -->
    <context:component-scan base-package="org.clgc.tomorrow">
        <context:exclude-filter type="regex" expression="org.clgc.tomorrow.controller.*"/>
        <!-- <context:exclude-filter type="annotation"  expression="org.springframework.stereotype.Controller" /> -->
    </context:component-scan>
</beans>

    数据源的properties文件

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/clgc?characterEncoding=UTF-8
username=root
password=123456

 

    3.3编写spring-mvc配置文件

<?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:context="http://www.springframework.org/schema/context"
    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.1.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">
        
    <!-- 扫描指定包下面的注解 @Controller -->
    <context:component-scan base-package="org.clgc.tomorrow.controller"></context:component-scan>
    <!-- 添加mvc的注解驱动 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    <!-- 配置文件上传 -->
    <bean id="multipartResolver" 
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"></property>
        <property name="maxUploadSize" value="1024000000"></property>
    </bean>
    <!-- 配置解析器对象 -->
    <bean id="resourceViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!-- 过滤静态资源 -->
    <mvc:default-servlet-handler/>
</beans>

    3.4编写mybatis配置文件

<?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="org.clgc.tomorrow.bean" />
    </typeAliases>
</configuration>

    3.5逆向工程配置文件

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
  

  <context id="DB2Tables" targetRuntime="MyBatis3">
      <commentGenerator>
          <property name="suppressAllComments" value="true"/>
      </commentGenerator>
    <jdbcConnection
        driverClass="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost:3306/cdlg"
        userId="root"
        password="">
    </jdbcConnection>

    <javaTypeResolver >
      <property name="forceBigDecimals" value="false" />
    </javaTypeResolver>
    <!-- 指定JavaBean生成的位置 -->
    <javaModelGenerator 
        targetPackage="org.clgc.tomorrow.bean" targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
      <property name="trimStrings" value="true" />
    </javaModelGenerator>
    <!-- 指定sql映射文件生成的位置 -->
    <sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources">
      <property name="enableSubPackages" value="true" />
    </sqlMapGenerator>
    <!-- 指定mapper接口的生成的位置 -->
    <javaClientGenerator type="XMLMAPPER" targetPackage="org.clgc.tomorrow.dao"  targetProject=".\src\main\java">
      <property name="enableSubPackages" value="true" />
    </javaClientGenerator>
    <!-- 指定每个表的生成策略 -->
    <table tableName="student" domainObjectName="Student">
        
    </table>
    <table tableName="user" domainObjectName="User">
        
    </table>
    <table tableName="friend" domainObjectName="Friend">
        
    </table>
    <table tableName="relation" domainObjectName="Relation">
        
    </table>
    <table tableName="record" domainObjectName="Record">
        
    </table>
    
  </context>
</generatorConfiguration>

    4.测试mapper

posted @ 2020-03-22 14:08  swallower  阅读(116)  评论(0)    收藏  举报