Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

preface

In our last blog post, we talked about how to frame a project. Now that the basic directory structure is in place, we are ready to integrate the SSM. Here’s how.

DAO layer integration

Create spring-dao.xml under SRC /main/ Resources /spring to integrate the DAO layer. Step by step, see how to integrate the DAO layer.

Database Configuration

First, prepare a database configuration file jdbc.properties in the SRC /main/resources directory to configure database parameters.

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=JDBC: mysql: / / localhost: 3306 / database name? useUnicode=true&characterEncoding=utf8
jdbc.username=The user name
jdbc.password=The user password
Copy the code

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

    <! -- 1. Import database configuration information -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <! -- 2. Database connection pool -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <! 2.1 Connection Pool Properties -->
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <! -- 2.2 attributes unique to c3P0 -->
        <property name="maxPoolSize" value="30"/>
        <property name="minPoolSize" value="10"/>
        <! -->
        <property name="autoCommitOnClose" value="false"/>
        <! -- Connection timeout time -->
        <property name="checkoutTimeout" value="10000"/>
        <! Retry times after connection failure -->
        <property name="acquireRetryAttempts" value="2"/>
    </bean>

</beans>
Copy the code

configurationSqlSessionFactoryObject (MyBatis)

SRC /main/resources: MyBatis -config. XML; spring-dao.xml: SqlSessionFactory;


      
<! DOCTYPEconfiguration
        PUBLIC "- / / mybatis.org//DTD Config / 3.0 / EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <! GetGeneratedKeys; getGeneratedKeys; getGeneratedKeys;
        <setting name="useGeneratedKeys" value="true"/>

        <! -- Replace column names with column aliases default :true -->
        <setting name="useColumnLabel" value="true"/>

        <! Table{create_time} -> Entity{createTime} ->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
</configuration>
Copy the code
<! SqlSessionFactory = SqlSessionFactory
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <! SQL > insert into database connection pool
    <property name="dataSource" ref="dataSource"/>
    <! 3.2 Configuring MyBatis Global Configuration File -->
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <! -- 3.3 Scanning poJO packets -->
    <property name="typeAliasesPackage" value="com.cunyu.pojo"/>
    <! -- 3.4 Scanning SQL configuration files -->
    <property name="mapperLocations" value="classpath:mapper/*.xml"/>
</bean>
Copy the code

The component scanning

<! -- 4. Scan DAO interface package to implement dynamic injection of DAO interface -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <! SqlSessionFactory -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    <! -- 4.2 Scanning dao packages -->
    <property name="basePackage" value="com.cunyu.dao"/>
</bean>
Copy the code

The service layer integration

In SRC/main/resources/spring under the new spring – service. The XML, is used to integrate the service layer, mainly do is as follows:

  1. Scan all classes under the service package with the @service annotation

  2. Configure the transaction manager

  3. Configure annotation-based declarative transactions


      
<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:tx="http://www.springframework.org/schema/tx"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <! -- 1. Scan service package -->
    <context:component-scan base-package="com.cunyu.service"/>

    <! 2. Configure transaction manager -->
    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <! SQL > insert into database connection pool
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <! 3. Configure annotation-based declarative transactions -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
Copy the code

The controller layer integration

Since you already have the DAO and Service layers integrated, you can’t do without the Controller layer. In SRC/main/resources/spring under the new spring – service. The XML, the integration of the controller layer needs to be done are mainly as follows:

  1. Turn on the annotation pattern for Spring MVC, since most development is based on annotations these days
  2. Processing of static resources such as images, styles, scripts, etc
  3. Configure the view resolver;
  4. scanningcontroller

      
<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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <! --1. Enable Spring MVC annotation mode -->
    <mvc:annotation-driven/>

    <! -- 2. Static resource Processing -->
    <mvc:default-servlet-handler/>

    <! -- 3. View resolver -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="suffix" value=".jsp"/>
        <property name="prefix" value="WEB-INF/jsp/"/>
    </bean>

    <! -- 4. Scan controller package -->
    <context:component-scan base-package="com.cunyu.controller"/>
</beans>

Copy the code

Configure web. XML

In web. XML, the DispatcherServlet is configured and the Spring MVC configuration file is loaded.


      
<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>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <! Spring MVC - configuration needs to be loaded in the Spring configuration file - dao. XML - > Spring - service. The XML - > Spring - web. XML - >
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-*.xml</param-value>
        </init-param>
        <! -- Load order, smaller number, higher priority -->
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <! -- Default matches all requests -->
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

Copy the code

conclusion

After the above configuration, our SSM framework is fully integrated and business logic can be developed. Alternatively, you can use this framework for other projects, with minor modifications. The integration process is summarized in the following order:

  1. Project framework construction;
  2. Integration of the dao layer
  3. Integrated service layer
  4. Integrated controller layer
  5. Configure web. XML