Spring mybatis - ssm framework environment construction (scheme 1)

Posted by drepster on Fri, 09 Aug 2019 13:03:55 +0200

The SSM framework - S-Spring S-Spring mvc M-mybatis requires the following configuration files to be placed under the resources folder:

db.properties is the configuration file of the database connection pool, which has the connection information of the database jdbc:

# JDBC
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://47.95.228.179:3306/mykidsshop?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=123

# JDBC Pool
jdbc.pool.init=1
jdbc.pool.minIdle=3
jdbc.pool.maxActive=20

# JDBC Test
jdbc.testSql=SELECT 'x' FROM DUAL

#============================#
#==== Framework settings ====#
#============================#

# \u89c6\u56fe\u6587\u4ef6\u5b58\u653e\u8def\u5f84
web.view.prefix=/WEB-INF/views/
web.view.suffix=.jsp

mybatis-config.xml places mybatis-related configuration files:

<?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>
    <! - Global parameters - >
    <settings>
        <! - Print SQL statements - >.
        <setting name="logImpl" value="STDOUT_LOGGING" />

        <! -- Enables or disables caching for global mappers. >
        <setting name="cacheEnabled" value="false"/>

        <! - Enables or disables lazy loading globally. When disabled, all associated objects are loaded immediately. >
        <setting name="lazyLoadingEnabled" value="true"/>

        <! -- When enabled, objects with delayed loading properties will load arbitrary properties completely when called. Otherwise, each attribute will be loaded as needed. >
        <setting name="aggressiveLazyLoading" value="true"/>

        <! -- Whether a single SQL is allowed to return multiple data sets (depending on driver compatibility) default: true - >
        <setting name="multipleResultSetsEnabled" value="true"/>

        <! -- Whether column aliases can be used (depending on driver compatibility) defau lt: true - >
        <setting name="useColumnLabel" value="true"/>

        <! -- Allows JDBC to generate primary keys. Driver support is required. If set to true, this setting forces the generated primary key to be used, and some drives are incompatible but still executable. Defau lt: false - >
        <setting name="useGeneratedKeys" value="false"/>

        <! - Specifies how MyBatis automatically maps the column NONE of the data base table: not PARTIAL: partial FULL: all - >.
        <setting name="autoMappingBehavior" value="PARTIAL"/>

        <! -- This is the defau lt execution type (SIMPLE: Simple; REUSE: Executors may reuse prepared statements; BATCH: Executors can repeat execution statements and batch updates)-->
        <setting name="defaultExecutorType" value="SIMPLE"/>

        <! -- Convert fields using hump nomenclature. >
        <setting name="mapUnderscoreToCamelCase" value="true"/>

        <! -- Setting the local cache range session: there will be a shared statement for data: statement range (so there will be no data sharing). defalut: session - >
        <setting name="localCacheScope" value="SESSION"/>

        <! -- When setting JDBC type to null, some drivers need to specify a value, defau lt: OTHER, and no type is required when inserting null value-->
        <setting name="jdbcTypeForNull" value="NULL"/>

    </settings>

    <plugins>
        <! -- com. github. PageHelper is the package name of the PageHelper class - >
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <! - Configure the parameters in the following way, and all the parameters will be introduced later.
            <property name="helperDialect" value="mysql"/>
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>
</configuration>

spring-context.xml is the configuration file for Spring

<?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"
       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">
    <!--Open Annotation Scan-->
    <context:annotation-config />
    <!--No Scanning Controller annotation-->
    <context:component-scan base-package="com.qfedu">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

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

    <!-- Open Transaction Annotation Driver -->
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

spring-context-druid.xml places the configuration file of the database connection pool:

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

    <!-- Load the configuration properties file -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:db.properties"/>

    <!-- Data source configuration, Use Druid Database connection pool -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- The data source driver class is not written. Druid The default will automatically be based on URL Distinguish DriverClass -->
        <property name="driverClassName" value="${jdbc.driverClass}"/>

        <!-- Basic attributes url,user,password -->
        <property name="url" value="${jdbc.connectionURL}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>

        <!-- Configuration initialization size, minimum, maximum -->
        <property name="initialSize" value="${jdbc.pool.init}"/>
        <property name="minIdle" value="${jdbc.pool.minIdle}"/>
        <property name="maxActive" value="${jdbc.pool.maxActive}"/>

        <!-- Configuration to get the connection waiting timeout time -->
        <property name="maxWait" value="60000"/>

        <!-- How often is the configuration interval detected to detect idle connections that need to be closed in milliseconds? -->
        <property name="timeBetweenEvictionRunsMillis" value="60000"/>

        <!-- Configure the minimum lifetime of a connection in the pool in milliseconds -->
        <property name="minEvictableIdleTimeMillis" value="300000"/>

        <property name="validationQuery" value="${jdbc.testSql}"/>
        <property name="testWhileIdle" value="true"/>
        <property name="testOnBorrow" value="false"/>
        <property name="testOnReturn" value="false"/>

        <!-- Configuration Monitoring Statistical Interception filters -->
        <property name="filters" value="stat"/>
    </bean>
</beans>

spring-context-mybatis.xml is a configuration file integrated with spring and mybatis:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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">

    <!-- To configure SqlSession -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- Used to configure the package where the corresponding entity class is located, multiple package Between can be used ',' Number Division -->
        <property name="typeAliasesPackage" value="com.qfedu.entity"/>
        <!-- The directory used to configure the object-relational mapping configuration file -->
        <property name="mapperLocations" value="classpath*:/mapper/**/*.xml"/>
        <property name="configLocation" value="classpath:/mybatis-config.xml"></property>

    </bean>

    <!-- scanning Mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.qfedu.mapper" />
    </bean>
</beans>

spring-mvc.xml Configuration of Spring mvc

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

    <description>Spring MVC Configuration</description>

    <!-- Load the configuration properties file -->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:db.properties"/>

    <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>

    <!-- Use Annotation automatic logon Bean,Scanning only @Controller -->
    <context:component-scan base-package="com.qfedu" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!-- Default annotation mapping support -->
    <mvc:annotation-driven />

    <!-- Define View File Resolution -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="${web.view.prefix}"/>
        <property name="suffix" value="${web.view.suffix}"/>
    </bean>

    <!-- Static resource mapping -->
    <mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>

    <!--Separate front-end and back-end development, cross-domain issues when front-end accesses back-end-->
    <mvc:cors>
        <mvc:mapping path="/**"
                     allowed-origins="*"
                     allowed-methods="POST, GET, OPTIONS, DELETE, PUT,PATCH"
                     allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"
                     allow-credentials="true" />
    </mvc:cors>

</beans>

log4j.properties log4j log file configuration information for printing logs

log4j.rootLogger=INFO, console, file

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logs/log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.MaxFileSize=1024KB
log4j.appender.A3.MaxBackupIndex=10
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n

Topics: Windows JDBC Spring log4j Mybatis