Simple Construction of SSM Integration Project

1. New Web Project

2. Add jar packages

3. Through reverse engineering, the corresponding entity and mapping files and interfaces are generated as follows

4. Create configuration files

4.1 spring configuration file

(1) 4.1.1 Application Context-dao.xml (dao layer configuration file)

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

    <!--Loading external property profile-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <!--data source-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${druid.url}"></property>
        <property name="username" value="${druid.username}"></property>
        <property name="password" value="${druid.password}"></property>
        <property name="driverClassName" value="${druid.driverClassName}"></property>
        <property name="initialSize" value="${druid.initialSize}"></property>
        <property name="minIdle" value="${druid.minIdle}"></property>
        <property name="maxActive" value="${druid.maxActive}"></property>
    </bean>

    <!--To configure sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--To configure mybatis configuration file-->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
        <!--Configuring Data Source Objects-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--To configure mapper Agent object
        this.sqlSessionFactory.openSession().getMapper(UserMapper.class)
    -->
    <!--Configure each one individually mapper Interface(Can also be disposable batch configuration)-->
    <!--<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        &lt;!&ndash;To configure sqlSessionFactroy object&ndash;&gt;
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        &lt;!&ndash;Configuration interface&ndash;&gt;
        <property name="mapperInterface" value="com.igeek.ssm.mapper.UserMapper"></property>
    </bean>-->

    <!--<bean id="itemMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        &lt;!&ndash;To configure sqlSessionFactroy object&ndash;&gt;
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        &lt;!&ndash;Configuration interface&ndash;&gt;
        <property name="mapperInterface" value="com.igeek.ssm.mapper.ItemMapper"></property>
    </bean>-->

    <!--One-time batch configuration mapper Interface:
    //Scanning all mapper interfaces under a package through a mapper scanner, batch loading configuration
    -->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--Setting packages to scan-->
        <property name="basePackage" value="com.igeek.ssm.mapper"></property>
        <!--To configure sqlSessionFactory object-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

</beans>

(2) applicationContext-service.xml (service layer configuration file)

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

    <!--scanning service Bag, will service Add below package@Service Annotated classes added to containers-->
    <context:component-scan base-package="com.igeek.ssm.service"></context:component-scan>

</beans>

(3) applicationContext-tx.xml (Transaction Support Profile)

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        https://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--Configuring Transaction Manager-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--Configuration notice-->
    <!--Below name The first approach requires transaction support. To configure read-only="true"It does not support transactions.-->
    <tx:advice id="tx_advice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="add*"></tx:method>
            <tx:method name="update*"></tx:method>
            <tx:method name="modify*"></tx:method>
            <tx:method name="delete*"></tx:method>
            <tx:method name="remove*"></tx:method>
            <tx:method name="get*" read-only="true"></tx:method>
            <tx:method name="select*" read-only="true"></tx:method>
            <tx:method name="query*" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>

    <!--Configuration section-->
    <aop:config>
        <aop:advisor advice-ref="tx_advice" pointcut="execution(* com.igeek.ssm.service.*.*(..))"></aop:advisor>
    </aop:config>

</beans>

4.2 Spring MVC configuration file

Spring mvc. XML (spring MVC configuration file)

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

    <!--Configuration scan controller All under the package@Controller Annotation Modified Classes-->
    <context:component-scan base-package="com.igeek.ssm.controller"></context:component-scan>

    <!--Configuration Annotation Driver-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--Configuration attempt parser-->
    <bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--Configuration file upload parser-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000"></property>
    </bean>

</beans>

4.3 mybatis configuration file

sqlMapConfig.xml (normally configure data sources and transactions in mybatis, but do not configure them if they already exist in the configuration file above)

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

</configuration>

4.4 web.xml configuration file

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">

    <!--To configure post Filter for Chinese garbled code-->
    <filter>
        <filter-name>characterEncoding</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>characterEncoding</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping>

    <!--Specify initialization parameters to listeners,Used for loading spring configuration file-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!--classpath:spring/applicationContext-dao.xml,classpath:spring/applicationContext-tx.xml,classpath:spring/applicationContext-servcie.xml-->
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>

    <!--Configuration listener for loading spring configuration file (This listener should be placed on the front-end controller)-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--Configuration of Front-end Controller-->
    <servlet>
        <servlet-name>dispatcherServlet</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>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
</web-app>

4.5 jdbc.properties configuration file

druid.url=jdbc:mysql://localhost:3306/springmvc?characterEncoding=utf-8
druid.password=root
druid.username=root
druid.driverClassName=com.mysql.jdbc.Driver
druid.initialSize=5
druid.minIdle=3
druid.maxActive=20

5. Create static files and other files such as controller layer and service layer

6. effect picture

Tags: Spring xml Druid Mybatis

Posted on Tue, 08 Oct 2019 02:10:39 -0700 by trackz