一、加入依赖

  1、jar 包

  (1)Spring

【AOP核心】
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar

【IOC核心】
commons-logging-1.1.3.jar
spring-aop-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar

【jdbc核心】
spring-jdbc-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar


【测试】
spring-test-4.0.0.RELEASE.jar

  

  (2)SpringMVC

【SpringMVC核心】
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar

【上传下载】
commons-fileupload-1.2.1.jar
commons-io-2.0.jar

【jstl-jsp标准标签库】
jstl.jar
standard.jar

【数据校验】
classmate-0.8.0.jar
hibernate-validator-5.0.0.CR2.jar
hibernate-validator-annotation-processor-5.0.0.CR2.jar
jboss-logging-3.1.1.GA.jar
validation-api-1.1.0.CR1.jar

【AJAX】
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar
jackson-annotations-2.1.5.jar

  

  (3)MyBatis

【mybatis核心】
mybatis-3.4.1.jar
mybatis-spring-1.3.0.jar(与spring的整合包)

【ehcache整合】
ehcache-core-2.6.8.jar
mybatis-ehcache-1.0.3.jar
log4j-1.2.17.jar
slf4j-api-1.7.21.jar
slf4j-log4j12-1.7.21.jar

 

  (4)其他包

【数据库、驱动】
mysql-connector-java-5.1.37-bin.jar
druid-1.1.9.jar

  

  2、Maven 方式

<dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>

            <!-- Spring IOC 核心包-->
            <dependency>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
                <version>1.1.3</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring-version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring-version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring-version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
                <version>${spring-version}</version>
            </dependency>

            <!-- Spring AOP 核心包 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring-version}</version>
            </dependency>

            <!-- Spring 数据库相关 -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>${spring-version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring-version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring-version}</version>
            </dependency>

            <!--Spring test 测试-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>${spring-version}</version>
            </dependency>

            <!-- Spring  Web MVC 核心包  -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring-version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring-version}</version>
            </dependency>

            <!-- 上传下载-->
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>1.3.1</version>
            </dependency>

            <!-- jstl-jsp标准标签库-->
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>org.apache.taglibs</groupId>
                <artifactId>taglibs-standard-impl</artifactId>
                <version>1.2.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.taglibs</groupId>
                <artifactId>taglibs-standard-spec</artifactId>
                <version>1.2.1</version>
            </dependency>

            <!-- Hibernate 数据校验-->
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>5.2.4.Final</version>
            </dependency>

            <!-- Ajax—— jackson 相关-->
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.9.8</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.9.8</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.8</version>
            </dependency>

            <!-- MyBatis 核心包-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.1</version>
            </dependency>
            <!-- mybatis-spring 整合包-->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.1</version>
            </dependency>


            <!-- ehcache 相关包-->
            <dependency>
                <groupId>net.sf.ehcache</groupId>
                <artifactId>ehcache-core</artifactId>
                <version>2.6.8</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis.caches</groupId>
                <artifactId>mybatis-ehcache</artifactId>
                <version>1.0.3</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.6.2</version>
            </dependency>

            <!--数据库连接池、数据库驱动包-->
            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.2.1</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.10</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.6</version>
            </dependency>

        </dependencies>

 

 

二、写配置

  1、web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
    </welcome-file-list>

    <!-- 配置 spring 容器,指定 spring 的配置文件 spring.xml-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--  前端控制器(核心控制器)  -->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置DispatcherServlet的初始化參數:设置文件的路径和文件名称 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
        <!--
           servlet 启动加载,servlet 原本是第一次访问创建对象
           load-on-startup:服务器启动的时候就创建对象,值越小优先级越高,越先创建对象
         -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--
      /* 和 / 都是拦截所有请求,/:会拦截所有请求,但是不会拦截 *.jsp,能保证 jsp访问正常;
      /* 的范围更大,还会拦截 *.jsp 这些请求,一旦拦截 jsp 页面就不能显示了
    -->
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>     <!-- / 这样写,只有请求才处理,页面会过滤掉 -->
    </servlet-mapping>


    <!--  字符编码过滤器  -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- 设置字符编码 -->
        <init-param>
            <!--encoding;解决请求乱码问题;request.setCharacterEncoding(this.encoding);-->
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <!-- 强制设置响应进行编码 -->
        <init-param>
            <!--forceEncoding;解决响应乱码问题;response.setCharacterEncoding(this.encoding);-->
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>  <!-- 对所有的请求都拦截,处理所有响应-->
    </filter-mapping>

    <!-- REST 风格过滤器-->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

 

 

  2、Spring 配置

    spring.xml 配置

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

    <!--扫描组件,排除 controller-->
    <!--包扫描; Spring除过控制器以外不要,其他业务逻辑组件都要,包括dao,service-->
    <context:component-scan base-package="com.njf" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <!--1、引入外部配置文件-->
    <context:property-placeholder location="../dbconfig.properties" />
    <!--
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="dbconfig.properties"/>
    </bean>
    -->

    <!--2、配置数据源  -->
    <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
        <property name="minPoolSize" value="${jdbc.minPoolSize}" />
    </bean>

    <!-- 3、使用JdbcTemplate操作数据库 pass   -->
    <!-- 3、使用 MyBatis 操作数据库   -->



    <!-- 4、配置事务控制   -->
    <!--  (1)配置事务管理器,让他控制住数据源里面的连接的关闭和提交  事务切面  -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--控制数据源-->
        <property name="dataSource" ref="c3p0DataSource"/>
    </bean>

    <!-- (2)开启基于注解的事务控制模式:依赖tx名称空间-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- (3)基于xml配置,配置事务,哪些方法切入事务还要写切入点表达式,给事务方法加注解即可@Transactional-->
    <aop:config>
        <!--配置切入点表达式-->
        <aop:pointcut id="txPoint" expression="execution(* com.njf.service.*.*(..))" />
        <!--事务增强,事务建议,事务属性-->
        <aop:advisor advice-ref="myTx" pointcut-ref="txPoint"/>
    </aop:config>

    <!-- (4)事务建议,事务属性 -->
    <tx:advice id="myTx" transaction-manager="transactionManager">
        <!--配置事务属性-->
        <tx:attributes>
            <tx:method name="*" rollback-for="java.lang.Exception"/>
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>


</beans>

 

 

  3、SpringMVC 配置

    springmvc.xml

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


    <!-- 包扫描,SpringMVC只扫描控制器;禁用默认的规则-->
    <!-- 扫描组件,将加上@Controller注解的类作为springMVC的控制层和异常控制器 -->
    <context:component-scan base-package="com.njf" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <!--  配置一个视图解析器 :能帮我们拼接页面地址-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--扫静态资源-->
    <mvc:default-servlet-handler/>
    <!--扫动态资源-->
    <mvc:annotation-driven/>

    <!--配置文件上传解析器-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="maxUploadSize" value="#{1024*1024*20}"/>
    </bean>


</beans>

 

  4、MyBatis 配置

    整合的关键配置写在 spring 的配置中:spring.xml

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

    <!--扫描组件,排除 controller-->
    <!--包扫描; Spring除过控制器以外不要,其他业务逻辑组件都要,包括dao,service-->
    <context:component-scan base-package="com.njf" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <!--1、引入外部配置文件-->
    <context:property-placeholder location="../dbconfig.properties" />
    <!--
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="dbconfig.properties"/>
    </bean>
    -->

    <!--2、配置数据源  -->
    <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}" />
        <property name="minPoolSize" value="${jdbc.minPoolSize}" />
    </bean>

    <!-- 3、使用JdbcTemplate操作数据库 pass   -->
    <!-- 3、使用 MyBatis 操作数据库   -->



    <!-- 4、配置事务控制   -->
    <!--  (1)配置事务管理器,让他控制住数据源里面的连接的关闭和提交  事务切面  -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--控制数据源-->
        <property name="dataSource" ref="c3p0DataSource"/>
    </bean>

    <!-- (2)注解配置:开启基于注解的事务控制模式:依赖tx名称空间-->
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <!-- (3)XML配置:基于xml配置,配置事务,哪些方法切入事务还要写切入点表达式,给事务方法加注解即可@Transactional-->
    <aop:config>
        <!--配置切入点表达式-->
        <aop:pointcut id="txPoint" expression="execution(* com.njf.service.*.*(..))" />
        <!--事务增强,事务建议,事务属性-->
        <aop:advisor advice-ref="myTx" pointcut-ref="txPoint"/>
    </aop:config>

    <!-- (4)事务建议,事务属性 -->
    <tx:advice id="myTx" transaction-manager="transactionManager">
        <!--配置事务属性-->
        <tx:attributes>
            <tx:method name="*" rollback-for="java.lang.Exception"/>
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>


    <!--  Spring 整合 MyBatis -->
    <!--配置使用mybatis操作数据库-->
    <!--可以根据配置文件得到SqlSessionFactory-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--指定mybatis配置文件位置与spring进行整合-->
        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
        <!--指定引用使用哪个数据源-->
        <property name="dataSource" ref="c3p0DataSource"/>
        <!--指定sql映射文件,将mapper下的所有xml文件都读取-->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
    </bean>

    <!--把每一个dao接口的实现加入到ioc容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定dao接口所在的包-->
        <property name="basePackage" value="com.njf.dao"/>
    </bean>

</beans>

 

    mybatis-config.xml 配置 MyBatis 的信息:

<?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>
        <setting name="cacheEnabled" value="true"/>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="jdbcTypeForNull" value="NULL"/>
        <setting name="lazyLoadingEnabled" value="true"/>
        <!--
            aggressiveLazyLoading:侵入延迟加载,当使用任一属性,所有的属性都会加载;禁用的话,只有用到该属性才会加载,即按需加载
        -->
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>

    <databaseIdProvider type="DB_VENDOR">
        <!--  为不同的数据库厂商起别名      -->
        <property name="MySQL" value="mysql"/>
        <property name="SQL Server" value="sqlserver"/>
        <property name="DB2" value="db2"/>
        <property name="Oracle" value="oracle" />
    </databaseIdProvider>

</configuration>

 

  5、其他配置

  (1)dbconfig.properties

jdbc_username=root
jdbc_password=00000
jdbc_url=jdbc:mysql://localhost:3306/mybatis_achang
jdbc_driverClassName=com.mysql.jdbc.Driver

 

  (2)ehcache.xml 

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="../config/ehcache.xsd">
    <!-- 磁盘保存路径 -->
    <diskStore path="D:\tmp\ehcache" />

    <defaultCache
            maxElementsInMemory="1"
            maxElementsOnDisk="10000000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
    </defaultCache>
</ehcache>

        <!--
        属性说明:
        l diskStore:指定数据在磁盘中的存储位置。
        l defaultCache:当借助CacheManager.add("demoCache")创建Cache时,EhCache便会采用<defalutCache/>指定的的管理策略

        以下属性是必须的:
        l maxElementsInMemory - 在内存中缓存的element的最大数目
        l maxElementsOnDisk - 在磁盘上缓存的element的最大数目,若是0表示无穷大
        l eternal - 设定缓存的elements是否永远不过期。如果为true,则缓存的数据始终有效,如果为false那么还要根据timeToIdleSeconds,timeToLiveSeconds判断
        l overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上

        以下属性是可选的:
        l timeToIdleSeconds - 当缓存在EhCache中的数据前后两次访问的时间超过timeToIdleSeconds的属性取值时,这些数据便会删除,默认值是0,也就是可闲置时间无穷大
        l timeToLiveSeconds - 缓存element的有效生命期,默认是0.,也就是element存活时间无穷大
         diskSpoolBufferSizeMB 这个参数设置DiskStore(磁盘缓存)的缓存区大小.默认是30MB.每个Cache都应该有自己的一个缓冲区.
        l diskPersistent - 在VM重启的时候是否启用磁盘保存EhCache中的数据,默认是false。
        l diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒。每个120s,相应的线程会进行一次EhCache中数据的清理工作
        l memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候, 移除缓存中element的策略。默认是LRU(最近最少使用),可选的有LFU(最不常使用)和FIFO(先进先出)
         -->

 

  (3)log4j.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
        <param name="Encoding" value="UTF-8" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m  (%F:%L) \n" />
        </layout>
    </appender>
    <logger name="java.sql">
        <level value="debug" />
    </logger>
    <logger name="org.apache.ibatis">
        <level value="info" />
    </logger>
    <root>
        <level value="debug" />
        <appender-ref ref="STDOUT" />
    </root>
</log4j:configuration>

 

三、代码

  1、Teacher 类

public class Teacher implements Serializable {

    private Integer id;
    private String name;
    private String course;
    private String address;
    private Date birth;
}

 

  2、dao 层

     接口:

public interface TeacherDao {
    public Teacher getTeacherById(Integer id);
}

 

    mapper:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.njf.dao.TeacherDao">


    <resultMap id="teacherMap" type="com.njf.bean.Teacher">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="course" property="course" />
        <result column="address" property="address"/>
        <result column="address" property="address"/>
    </resultMap>

    <!--
        public Teacher getTeacherById(Integer id);
    -->
    <select id="getTeacherById" resultMap="teacherMap">
        select * from t_teacher where id = #{id}
    </select>


</mapper>

 

  3、service 层

@Service
public class TeacherServiceImpl implements TeacherService{
    @Autowired
    private TeacherDao teacherDao;

    @Override
    public Teacher getTeacher(Integer id) {
        return teacherDao.getTeacherById(id);
    }
}

 

  4、控制层

@Controller
public class TeacherController {

    @Autowired
    TeacherService teacherService;

    @RequestMapping("/getTeacher")
    public String getTeacher(@RequestParam(value = "id", defaultValue = "1")Integer id, Model model){
        Teacher teacher = teacherService.getTeacher(id);
        model.addAttribute("teacherMsg", teacher);
        return "success";
    }
}

 

四、文件夹结构

  

 

五、总结

  1、搭建环境

  (1)创建一个动态的 WEB 工程
  (2)导入 SSM 需要使用的 jar 包(Spring,SpringMVC,MyBatis)
  (3)导入整合适配包
  (4)导入其他技术的一些支持包,如连接池,数据库驱动,日志等……
  (5)导入第三方支持的包:log4j,pageHelper,AspectJ,jackson,jstl

  2、配置 web.xml 

  (1)核心控制器(前端控制器):DispatcherServlet

  (2)字符编码过滤器:CharacterEncodingFilter

  (3)REST 风格的过滤器:HiddenHttpMethodFilter

  3、配置 springmvc.xml

  (1)扫描控制层组件和全局异常处理器;

  (2)配置视图解析器;

  (3)配置静态资源处理:default-servlet

  (4)配置动态资源,MVC 驱动:<mvc:annotation-driven />

  (5)可选:文件上传解析器(MultipartResolver)、拦截器等

  4、整合 spring 和 springmvc

  (1)web.xml

    ① 实例化 Spring 容器的监听器:ContextLoaderListener

    ② 配置 context-param,指定spring的配置文件

  (2)spring.xml

    ① 扫描组件(排除控制层)

    ② 配置数据源(properties 文件的引入)

    ③ 配置事务控制(事务管理器、事务属性、AOP 切面)

  5、MyBatis 配置

  (1)核心(全局)配置文件 mybatis-config.xml

  (2)mapper 接口和 mapper 映射文件

  6、spring 整合 MyBatis

  (1)MyBatis 的 SqlSession 的创建:SqlSessionFactoryBean:管理 Sqlsession

  (2)MyBatis 的 Mapper 接口的代理实现类:MapperScannerConfigurer

    查看不同 MyBatis 版本整合 Spring 时使用的适配包

    

 

 

   下载整合适配包:https://github.com/mybatis/spring/releases

 

posted on 2021-12-11 11:36  格物致知_Tony  阅读(102)  评论(0编辑  收藏  举报