ssm框架整合

Posted 英俊聪明

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ssm框架整合相关的知识,希望对你有一定的参考价值。

          ssm框架整合步骤                     2017-09-23  22:24:13

一、导包(大概30个左右)

二、创建po类

三、创建mapper接口 和映射文件(可用逆向工程生成)

四、创建SqlMapperContext.xml全局配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://www.mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <typeAliases>
       <package name="com.fjm.ssm.po"/>
   </typeAliases> 

</configuration>

 

五、配置applicationContext-dao.xml

   配置内容包括数据源和sqlSessionFactory

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
       ">
<!--  <context:component-scan base-package="com.fjm.mapper"></context:component-scan> -->
<!-- 配置spring的公共模块 -->
<!-- 1 加载properties文件 -->
    <context:property-placeholder location="classpath:jdbcInfo.properties"/>
    <!-- 2 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
     
   <!--配置SqlSessionFactory  -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!--加载全局配置文件  -->
       <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
       <!--引入数据源  -->
       <property name="dataSource" ref="dataSource"></property>
   </bean>
   
  
   <!--批量  代理mapper  会加载mapper目录下的映射文件和接口-->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.fjm.ssm.mapper"></property>
   </bean>
</beans>

六、创建service接口和实现类,调用mapper接口实现数据调用

package com.fjm.ssm.service;

import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fjm.ssm.mapper.ItemsExtMapper;
import com.fjm.ssm.po.Items;
import com.fjm.ssm.po.ItemsQuery;

@Service
public class ItemsServiceImpl implements ItemsService {
    
    @Autowired
    private ItemsExtMapper mapper;
    @Override
    public List<Items> queryItemsByItemsQuery(ItemsQuery vo) {
        return mapper.ItemsQuery(vo);
    }

}

七、配置applicationContext-service.xml,实现service层的事务管理

 

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
       ">

  <!--扫描service里的方法进行事件控制  -->
     <context:component-scan base-package="com.fjm.ssm.service"></context:component-scan>
    
    <!--配置事务管理器  -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"></property>
    </bean>
        
     <!--配置通知  -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 传播特性 -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="del*" propagation="REQUIRED" />
            <tx:method name="remove*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="query*" read-only="true" />
            <tx:method name="select*" read-only="true" />
            <tx:method name="get*" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!--配置切面  -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.fjm.ssm.service.*.*(..))"/>
    </aop:config>
 </beans>

 

八、配置springmvc.xml配置文件来配置controller的 业务访问处理

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 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.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    
    <!--扫描,使该包下的注解生效  -->
    <context:component-scan base-package="com.fjm.ssm.controller"></context:component-scan>

   <!--处理器映射器和适配器  -->
   <mvc:annotation-driven/>
   
   <!-- 配置视图解析器 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/"></property>
      <property name="suffix" value=".jsp"></property>
   </bean>
</beans>

 

九、配置web.xml
    

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
    <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.xml</param-value>
      </init-param>
    </servlet>
    <servlet-mapping>
       <servlet-name>springmvc</servlet-name>
       <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    
    <!--  加载spring容器-->
    <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  
  
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

十、创建controller 类处理业务访问操作

@Controller
public class QueryController {

    @Autowired
    private ItemsService service;

    @RequestMapping("/generatorMethod")
    public ModelAndView generatorMethod() {


。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
}

 

 十一、分析流程

①tomcat启动时,加载spring容器,contorller、service、mapper下的文件被加载

②调用过程 :   jsp页面 调用controller -->   被处理器拦截  -->  进入springmvc.xml  -->   进入controller   -->  调用 service层   -->  调用mapper接口  -->

自动生成实现类,调用映射文件,进行sql语句发送  -->  进入applicationContext-dao.xml 与数据库进行数据访问

 

以上是关于ssm框架整合的主要内容,如果未能解决你的问题,请参考以下文章

SSM 项目整合详细解读

520前,我放弃陪女朋友时间,被迫写代码:“SSM框架整合+excel文件上传到数据库+数据更新“

SSM框架整合

Java EE互联网轻量级框架整合开发— SSM框架(中文版带书签)原书代码

手把手整合SSM框架

黑马程序员最全SSM框架用户角色案例(SSM整合版)