SSM框架整合

ssm整合

  1. 创建一个maven项目 配置环境

    1. 创建数据库和数据库表

    2. 创建maven工程

    3. 导入pom.xml中坐标依赖

          <properties>
      
      		<spring.version>5.0.2.RELEASE</spring.version>
              <slf4j.version>1.6.6</slf4j.version>
              <log4j.version>1.2.12</log4j.version>
              <mysql.version>5.1.6</mysql.version>
              <mybatis.version>3.4.5</mybatis.version>
          </properties>
          </dependencies>
      		<!-- spring -->
              <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjweaver</artifactId>
                  <version>1.6.8</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-aop</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-context</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-web</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-webmvc</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-test</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-tx</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-jdbc</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.12</version>
                  <scope>compile</scope>
              </dependency>
      
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <version>${mysql.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>javax.servlet</groupId>
                  <artifactId>servlet-api</artifactId>
                  <version>2.5</version>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>javax.servlet.jsp</groupId>
                  <artifactId>jsp-api</artifactId>
                  <version>2.0</version>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>jstl</groupId>
                  <artifactId>jstl</artifactId>
                  <version>1.2</version>
              </dependency>
      
              <!-- log start -->
              <dependency>
                  <groupId>log4j</groupId>
                  <artifactId>log4j</artifactId>
                  <version>${log4j.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.slf4j</groupId>
                  <artifactId>slf4j-api</artifactId>
                  <version>${slf4j.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.slf4j</groupId>
                  <artifactId>slf4j-log4j12</artifactId>
                  <version>${slf4j.version}</version>
              </dependency>        <!-- log end -->
              <dependency>
                  <groupId>org.mybatis</groupId>
                  <artifactId>mybatis</artifactId>
                  <version>${mybatis.version}</version>
              </dependency>
      
      
              <dependency>
                  <groupId>org.mybatis</groupId>
                  <artifactId>mybatis-spring</artifactId>
                  <version>1.3.0</version>
              </dependency>
      
              <dependency>
                  <groupId>c3p0</groupId>
                  <artifactId>c3p0</artifactId>
                  <version>0.9.1.2</version>
                  <type>jar</type>
                  <scope>compile</scope>
              </dependency>
      
          </dependencies><!-- spring -->
              <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjweaver</artifactId>
                  <version>1.6.8</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-aop</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-context</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-web</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-webmvc</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-test</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-tx</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-jdbc</artifactId>
                  <version>${spring.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.12</version>
                  <scope>compile</scope>
              </dependency>
      
              <dependency>
                  <groupId>mysql</groupId>
                  <artifactId>mysql-connector-java</artifactId>
                  <version>${mysql.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>javax.servlet</groupId>
                  <artifactId>servlet-api</artifactId>
                  <version>2.5</version>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>javax.servlet.jsp</groupId>
                  <artifactId>jsp-api</artifactId>
                  <version>2.0</version>
                  <scope>provided</scope>
              </dependency>
      
              <dependency>
                  <groupId>jstl</groupId>
                  <artifactId>jstl</artifactId>
                  <version>1.2</version>
              </dependency>
      
              <!-- log start -->
              <dependency>
                  <groupId>log4j</groupId>
                  <artifactId>log4j</artifactId>
                  <version>${log4j.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.slf4j</groupId>
                  <artifactId>slf4j-api</artifactId>
                  <version>${slf4j.version}</version>
              </dependency>
      
              <dependency>
                  <groupId>org.slf4j</groupId>
                  <artifactId>slf4j-log4j12</artifactId>
                  <version>${slf4j.version}</version>
              </dependency>        <!-- log end -->
              <dependency>
                  <groupId>org.mybatis</groupId>
                  <artifactId>mybatis</artifactId>
                  <version>${mybatis.version}</version>
              </dependency>
      
      
              <dependency>
                  <groupId>org.mybatis</groupId>
                  <artifactId>mybatis-spring</artifactId>
                  <version>1.3.0</version>
              </dependency>
      
              <dependency>
                  <groupId>c3p0</groupId>
                  <artifactId>c3p0</artifactId>
                  <version>0.9.1.2</version>
                  <type>jar</type>
                  <scope>compile</scope>
              </dependency>
      
             </dependencies>
      <build>
              <finalName>ssm</finalName>
      <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
                  <plugins>
                      <plugin>
                          <groupId>org.apache.maven.plugins</groupId>
                          <artifactId>maven-compiler-plugin</artifactId>
                          <version>3.2</version>
                          <configuration>
                              <source>1.8</source>
                              <target>1.8</target>
                              <encoding>UTF-8</encoding>
                              <showWarnings>true</showWarnings>
                          </configuration>
                      </plugin>
                      <plugin>
                          <artifactId>maven-clean-plugin</artifactId>
                          <version>3.1.0</version>
                      </plugin>
                      <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                      <plugin>
                          <artifactId>maven-resources-plugin</artifactId>
                          <version>3.0.2</version>
                      </plugin>
                      <plugin>
                          <artifactId>maven-compiler-plugin</artifactId>
                          <version>3.8.0</version>
                      </plugin>
                      <plugin>
                          <artifactId>maven-surefire-plugin</artifactId>
                          <version>2.22.1</version>
                      </plugin>
                      <plugin>
                          <artifactId>maven-war-plugin</artifactId>
                          <version>3.2.2</version>
                      </plugin>
                      <plugin>
                          <artifactId>maven-install-plugin</artifactId>
                          <version>2.5.2</version>
                      </plugin>
                      <plugin>
                          <artifactId>maven-deploy-plugin</artifactId>
                          <version>2.8.2</version>
                      </plugin>
                  </plugins>
              </pluginManagement>
          </builds>
      
    4. 搭建src下的目录结构和类文件

      1. domain包下的Account实体类
      2. dao包下的AccountDao接口 定义两个操作数据库表的方法
      3. service包下的AccountService接口 定义两个操作数据库表的方法 service.impl包下的AccountServiceImpl实现类
      4. controller包下的AccountController类
  2. 配置spring开发环境

    1. 配置spring核心配置文件 applicationContext.xml文件

        <!--
            1. 开启注解的扫描
                希望spring 帮我们去处理service和dao的对象的创建
                但是controller不需要spring框架去处理 后续可以直接在spring mvc配置文件中去处理controller对象
        -->
        <context:component-scan base-package="com.huike">
            <!--配置哪些注解不被spring所扫描-->
            <context:exclude-filter type="annotation"
                                    expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
        <!--
            Spring 整合 Mybatis的步骤(无事务):
                1. 配置数据源  数据源中配置连接属性
                2. 配置sqlSessionFactory对象  为该对象配置数据源属性
                3. 配置要扫描的dao的包 用于生成dao的代理对象
        -->
    
        <!--1. 配置数据源  数据源中配置连接属性-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="jdbc:mysql:///ssm"/>
            <property name="user" value="root"/>
            <property name="password" value="123456"/>
        </bean>
    
        <!--2. 配置sqlSessionFactory对象  为该对象配置数据源属性-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!--3.配置要扫描的dao的包 用于生成dao的代理对象 -->
        <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.huike.dao"/>
        </bean>
    
        <!--
            Spring配置事务管理步骤:
                1. 配置事务管理器   还需将数据源的引用作为属性配置进去
                2. 配置事务通知
                3. 配置aop
        -->
    
        <!--1. 配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    
        <!--2. 配置事务通知-->
        <tx:advice id="txAdvice">
            <tx:attributes>
                <tx:method name="find*" read-only="true"/>
                <tx:method name="*"/>
            </tx:attributes>
        </tx:advice>
    
        <!--3. 配置aop增强   cglib jdk-->
        <aop:config>
            <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.huike.service.impl.*ServiceImpl.*(..))"/>
        </aop:config>
    ```
    
    1. 在对应的类中加上注解

    2. 测试

  3. 配置springmvc开发环境

    1. 配置前端控制器 DisPatcherServlet 让服务器启动时就加载该控制器 并且将springmvc.xml一并加载

    2. 配置过滤器 CharacterEncodingFilter 解决中文乱码

    3. 编写springmvc.xml文件

      1. 开启扫描 --> 只开启controller的扫描
      2. 配置视图解析器 InternalResourceViewResolver
      3. 设置哪些资源不走过滤器 放行 css js images 等
    4. 开启spring mvc注解的支持

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:mvc="http://www.springframework.org/schema/mvc"
             xmlns:context="http://www.springframework.org/schema/context"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation=" http://www.springframework.org/schema/beans
                                  http://www.springframework.org/schema/beans/spring-beans.xsd
                                  http://www.springframework.org/schema/mvc
                                  http://www.springframework.org/schema/mvc/spring-mvc.xsd
                                  http://www.springframework.org/schema/context
                                  http://www.springframework.org/schema/context/spring-context.xsd">
      
          <!-- 扫描controller的注解,别的不扫描 -->
          <context:component-scan base-package="com.huike.controller">
              <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
          </context:component-scan>
      
          <!-- 配置视图解析器 -->
          <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
              <!-- JSP文件所在的目录 -->
              <property name="prefix" value="/WEB-INF/pages/"/>        <!-- 文件的后缀名 -->
              <property name="suffix" value=".jsp"/>
          </bean>
          <!-- 设置静态资源不过滤 -->
          <mvc:resources location="/css/" mapping="/css/**"/>
          <mvc:resources location="/images/" mapping="/images/**"/>
          <mvc:resources location="/js/" mapping="/js/**"/>
      
          <!-- 开启对SpringMVC注解的支持 -->
          <mvc:annotation-driven/>
      
      
      </beans>
      
    5. 测试环境是否搭建成功

  4. Spring 整合 Spring MVC框架

    • 目的:在controller中能够成功的区调用service对象中的方法
    • 步骤:
      1. 在项目启动时,去加载applicationContext.xml文件 在web.xml中配置 ContextLoadListener 监听器
        • 注意:这个监听器只能去加载WEB-INF 目录下的配置文件 需要设置路径
      2. 在controller中去注入service对象 调用service方法进行测试
      3. 测试环境是否搭建成功
  5. 配置Mybatis开发环境

    1. 编写Mybatis的配置文件 SqlMapConfig.xml文件
    2. 在dao方法上添加sql注解 在注解上编写SQL语句(不需要单独为dao设置mapper.xml文件了)
    3. 测试mybatis环境是否搭建成功
  6. spring 整合 mybatis 框架

    1. 没有事务的整合 (只能实现查询操作)
      1. 将mybatis中的配置文件SqlMapConfig.xml中的内容 配置到 applicationContext.xml文件中
        • 其中sessionFactory session dao对象统一交给spring的ioc处理(在配置文件中配置bean标签 或 加注解)
      2. 为dao接口类上添加注解 @Repository
      3. 在service中调用dao对象 spring DI注入该对象
      4. 测试整合环境是否成功
    2. 含有事务的整合 (在1的基础上加上了spring对事务的管理 可以实现增删改查所有对数据库的操作)
      1. 在spring的配置文件applicationContext.xml中 增加对事务的支持
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值