SSM Framework Integration Configuration File Writing

Posted by jackel15 on Tue, 27 Aug 2019 13:43:05 +0200

Configuration step instructions

Red is the cue message

Green classes introduced for bean Tags

Purple label header for non-bean labels

Blue is the name attribute name of the table

 

I. Dao Layer

1. Import the Data Source Profile (context:property-placeholder)

2. Create Data Source Data Source Object (Druid as Data Source in this case) (Druid Data Source)

3. Create SqlSessionFactoryBean object (SqlSessionFactoryBean)

3.1 Introducing Data Source

3.2 Aliases Package for Mapping Beans

3.3 Introduce mybatis-config configuration file (there are some mybatis configurations, such as paging plug-ins due to log files, etc.) (configLocation)

4. Scanning dao packages and automatically creating dynamic proxy objects (this step only requires injecting dao packages, and spring automatically stores the created objects in ioc containers) (Mapper Scanner Configurer)

II. Service Layer

1. Open the Annotation Scan of service Layer (context:component-scan)

2. Declarative transaction management

2.1 Create Transaction Manager Objects (Data Source Transaction Manager)

2.2 Transaction Enhancement (tx:advice)

3. Configuration section (aop:config)

III. Controller Layer

1. Open the controller Layer's Annotation Scan (context:component-scan)

2. Configure Annotation Driver (Writing this configuration can solve many unnecessary problems, and also inject some data format objects, such as Json, for field conversion of data) (mvc:annotation-driven)

2.1 Configuration of Message Converters (mvc:message-converters)

2.1.1 Message Converter Object (which automatically converts data sent from the front end into json format) (FastJson HttpMessageConverter)

IV. web.xml

 

 

Sample spring-ssm 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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop" 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/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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--dao layer-->
    <!--Introducing configuration files-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--Establish DataSource object-->
    <bean id="DruidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--Establish SqlSessionFactoryBean object-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--data source-->
        <property name="dataSource" ref="DruidDataSource"/>
        <!--Mapping an alias-->
        <property name="typeAliasesPackage" value="com.heronsbill.bean"/>
        <!--Introduce mybatis_config configuration file-->
        <property name="configLocation" value="classpath:MyBatis_config.xml"/>
    </bean>

    <!--Scanning packages automatically create dynamic proxy objects, which automatically store the created objects to ioc In containers-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.heronsbill.dao"/>
        <!--injection sqlSessionFactory Object, this step background has been automatically injected, you can not write-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>

    </bean>
    <!--service layer-->
    <!--Open Annotation Scan-->
    <context:component-scan base-package="com.heronsbill.service.impl"/>
    <!--Declarative transaction management-->
    <!--Creating Transaction Manager Objects-->
    <bean id="DataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="DruidDataSource"/>
    </bean>
    <!--Transaction Enhancement-->
    <tx:advice id="txAdvice" transaction-manager="DataSourceTransactionManager">
        <tx:attributes>
            <tx:method name="insert*"/>
            <tx:method name="update*"/>
            <tx:method name="delete*"/>
            <tx:method name="add*"/>
            <tx:method name="edit*"/>
            <tx:method name="set*"/>
            <tx:method name="*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
    </tx:advice>

    <!--Configuration section-->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com.heronsbill.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
    </aop:config>

    <!--controller layer-->

    <!--open controller Layer Annotation Scanning-->
    <context:component-scan base-package="com.heronsbill.web"/>
    <!--mvc Annotation driven-->
    <mvc:annotation-driven>
        <!--Automatically json Format conversion-->
        <mvc:message-converters>
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes" value="application/json;charset=utf-8"/>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

</beans>

MyBatis_config.xml configuration sample

<?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>
    <settings>
        <!--Configuration logs-->
        <setting name="logImpl" value="log4j"/>
    </settings>
</configuration>

Sample web.xml configuration

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--Coding filter-->
  <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>


  <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/*</param-value>
  </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>DispatcherServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
</web-app>

Summary:

Every time I write a project, the environment is always a big task. I need to sort out the configuration process so that I can read it later.

The ability is still shallow and needs to be improved. If there are deficiencies, don't hesitate to give advice!

Topics: Spring Mybatis xml JDBC