SSM整合

文章目录

    • 思路梳理
    • dao-mybatis
    • service
    • spring-mvc

思路梳理

当我们使用SSM项目开发一个项目时

  1. 需求分析
  2. 数据库设计
  3. 后端业务逻辑实现

第三步,才需要使用SSM框架

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.0modelVersion>

    <groupId>com.yygroupId>
    <artifactId>SSM_learningartifactId>
    <version>1.0-SNAPSHOTversion>

    
    <dependencies>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.47version>
        dependency>
        
        <dependency>
            <groupId>com.mchangegroupId>
            <artifactId>c3p0artifactId>
            <version>0.9.5.2version>
        dependency>

        
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>servlet-apiartifactId>
            <version>2.5version>
        dependency>
        <dependency>
            <groupId>javax.servlet.jspgroupId>
            <artifactId>jsp-apiartifactId>
            <version>2.2version>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>jstlartifactId>
            <version>1.2version>
        dependency>

        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.5.2version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>2.0.2version>
        dependency>

        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.1.9.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>5.1.9.RELEASEversion>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.16.18version>
        dependency>
        
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.2.70version>
        dependency>

    dependencies>






    
    <build>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.propertiesinclude>
                    <include>**/*.xmlinclude>
                includes>
                <filtering>falsefiltering>
            resource>

        resources>
    build>



project>

dao-mybatis

  1. 实现dao层 ----- 数据库交互
    首先要实现dao层,dao层是操作数据库的层,我们使用Mybatis框架帮助操作数据库!
    (Mybatis可以帮我们完成从一个dao层接口和sql语句,得到一个实现类实现接口执行sql语句的过程!)
    因为SSM整合,最终我们所有的类都需要从Spring中得到。
    所以我们要将用Mybatis动态生成的实现了dao层接口的类注册到Spring中。

    完成了dao层接口实现类的SpringBean注册,bean名字为接口名 + SpringBean命名规则:
    1、如果类第一个字母大写第二个小写,那么首字母小写获取bean
    2、如果第一个和第二个字母都是大写的,那个获取bean首字母要大写

    mybatis-config.xml

    
    
    <configuration>
            
            
        <typeAliases>
            <package name="com.yy.pojo"/>
        typeAliases>
            
            
        <mappers>
            <package name="com.yy.dao"/>
        mappers>
    configuration>
    

    spring-dao.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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/context
           https://www.springframework.org/schema/context/spring-context.xsd">
    
    
        
        <context:property-placeholder location="classpath:database.properties">context:property-placeholder>
    
        
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driver}">property>
            <property name="jdbcUrl" value="${jdbc.url}">property>
            <property name="user" value="${jdbc.username}">property>
            <property name="password" value="${jdbc.password}">property>
        bean>
    
    
    
        
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            
            <property name="dataSource" ref="dataSource">property>
            
            <property name="configLocation" value="classpath:mybatis-config.xml">property>
        bean>
        
    	此处重点!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        
        
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
            
            <property name="basePackage" value="com.yy.dao"/>
        bean>
    
    beans>
    
    

service

  1. service层
    完成了第一步,我们已经可以在Spring中通过context.getBean("IBookMapper");来获取dao层的每个接口的实现类对象了。

    接下来配置Service层:

    spring-service.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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/context https://www.springframework.org/schema/context/spring-context.xsd">
    
        
        <context:component-scan base-package="com.yy.service">context:component-scan>
    
        
        
    
    
    
    
        
        
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            
            <property name="dataSource" ref="dataSource" />
        bean>
    
    beans>
    

spring-mvc

  1. SpringMVC整合
    完成了第二步,我们已经可以在Spring中通过context.getBean("bookService");来获取service层的业务实现类对象了。
    首先为项目添加web依赖,生成web相关文件。
     
    在web.xml中配置DispatcherServlet :使前端的请求可以流入DispatcherServlet。
    并配置乱码处理过滤器,session等web相关配置

    web.xml

    
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        
        <servlet>
            <servlet-name>springmvcservlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
            <init-param>
                <param-name>contextConfigLocationparam-name>
                <param-value>classpath:spring-mvc.xmlparam-value>
            init-param>
            <load-on-startup>1load-on-startup>
        servlet>
        <servlet-mapping>
            <servlet-name>springmvcservlet-name>
            <url-pattern>/url-pattern>
        servlet-mapping>
    
        
        <filter>
            <filter-name>encodingFilterfilter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
            <init-param>
                <param-name>encodingparam-name>
                <param-value>utf-8param-value>
            init-param>
        filter>
        <filter-mapping>
            <filter-name>encodingFilterfilter-name>
            <url-pattern>/*url-pattern>
        filter-mapping>
    
        
        <session-config>
            <session-timeout>15session-timeout>
        session-config>
    
    web-app>
    

    web.xml中需要引入DispatcherServlet(springmvc)的配置

    spring-mvc.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        
        
        <mvc:annotation-driven>
            <mvc:message-converters register-defaults="true">
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <constructor-arg value="UTF-8"/>
                bean>
            mvc:message-converters>
        mvc:annotation-driven>
    
        
        <mvc:default-servlet-handler/>
    
        
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        bean>
    
        
        <context:component-scan base-package="com.yy.controller" />
    
        <import resource="spring-service.xml">import>
        <import resource="spring-dao.xml">import>
    beans>
    
  2. 此时前端的请求流入DispatcherServlet,还需要再spring-mvc配置文件中引入dao和service层的配置文件,才可以通过MVC调用Service层的Bean。
    再spring-mvc配置文件中加上两句:

        <import resource="spring-service.xml">import>
        <import resource="spring-dao.xml">import>
    

你可能感兴趣的:(#,SpringMVC,ssm,spring,mybatis,springmvc)