Spring、Spring-MVC、Mybatis、Mybatis-generator整合核心配置文件记录

Spring、Spring-MVC、Mybatis、Mybatis-generator整合核心配置文件记录

Spring、Spring-MVC、Mybatis、Mybatis-generator整合核心配置xml文件记录

1. 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"
       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">
    <!--引入系统配置的资源文件,多个文件使用 , 隔开-->
    <context:property-placeholder location="classpath:dbConfig.properties"/>

    <!--Spring自动扫描的dao与service包,自动注入这两个包下的所有-->
    <context:component-scan base-package="com.ssm.dao,com.ssm.service"/>
</beans>

2. spring-mvc.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:p="http://www.springframework.org/schema/p"
       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-4.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 自动扫描controllers下的所有类,使其成为Spring MVC的控制器 -->
    <context:component-scan base-package="com.ssm.controller"/>
    <!--下面两句不加上,有时候会出现handler无法访问的问题-->
    <!--将SpringMVC不能处理的请求交给Tomcat 这样可以访问静态资源-->
    <mvc:default-servlet-handler/>
    <!--能支持springMVC更高级的一些功能,JSR303校验,快捷Ajax,映射动态请求-->
    <mvc:annotation-driven/>

    <!--  避免IE执行Ajax时,返回的数据类型为JSON数据时出现下载文件的下载框 -->
    <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/html;charset=UTF-8</value>
            </list>
        </property>
    </bean>

    <!--spring 3.1 开始AnnotationMethodHandlerAdapter与DefaultAnnotationHandlerMapping已经过时。
         用 RequestMappingHandlerMapping 来替换 DefaultAnnotationHandlerMapping,
          用 RequestMappingHandlerAdapter 来替换 AnnotationMethodHandlerAdapter
    -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="mappingJackson2HttpMessageConverter"></ref>
            </list>
        </property>
    </bean>
    <!--发布webservice同步接口  注意端口号不要和已有的冲突  项目启动即发布-->
    <bean class="org.springframework.remoting.jaxws.SimpleJaxWsServiceExporter">
        <property name="baseAddress" value="http://127.0.0.1:1231/"/>
    </bean>
    <bean id="accountSynchronousService" class="com.ssm.controller.sysManagement.AccountSynchronousService">
    </bean>
    <!--视图解析器:对模型视图名称的解析,即在模型视图名称添加前缀与后缀-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/views/" p:suffix=".jsp"/>

    <!--配置自定义的拦截器-->
    <!-- 当设置多个拦截器时,先按顺序调用preHandle方法,然后逆序调用每个拦截器的postHandle和afterCompletion方法 -->
    <mvc:interceptors>
        <!--配置拦截器(不)作用于那些请求路径-->
        <!--<bean class="com.ssm.DefineInterceptor.LoginInterceptor"></bean>-->
        <mvc:interceptor>
            <!--作用于所有请求-->
            <mvc:mapping path="/*.action"/>
            <mvc:mapping path="/*/*.action"/>
            <mvc:mapping path="/views/*.jsp"/>
            <!--不作用于 URL为:“/userController/userLogin.action”的请求-->
            <mvc:exclude-mapping path="/emp/empLogin.action"/>
            <mvc:exclude-mapping path="/emp/ssoLogin.action"/>
            <mvc:exclude-mapping path="/login.jsp"/>
            <mvc:exclude-mapping path="/ssoLogin.jsp"/>
            <bean class="com.ssm.UserDefinedInterceptor.LoginInterceptor"></bean>
        </mvc:interceptor>
    </mvc:interceptors>


    <!--文件上传-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding">
            <value>UTF-8</value>
        </property>
        <!--<property name="maxUploadSize">
            <value>1048576000</value>&lt;!&ndash;上传文件大小限制为1000M,1000*1024*1024&ndash;&gt;
        </property>-->
        <property name="maxInMemorySize">
            <value>4096</value>
        </property>
    </bean>

</beans>

3. mybatis-config.xml

<?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="mapUnderscoreToCamelCase" value="true"/>
        <!--控制台打印SQL,带参数及结果-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
    <typeAliases>
        <package name="com.ssm.model"/>
    </typeAliases>
    <!--配置分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>
</configuration>
  1. spring-mybatis.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
       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-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!-- 配置数据源 druid -->
    <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="${jdbc_driverClassName}"/>
        <property name="url" value="${jdbc_url}" />
        <property name="username" value="${jdbc_username}" />
        <property name="password" value="${jdbc_password}" />
        <!-- 初始化连接大小 -->
        <property name="initialSize" value="0" />
        <!-- 连接池最大使用连接数量 -->
        <property name="maxActive" value="20" />
        <!-- 连接池最大空闲 -->
        <!--<property name="maxIdle" value="20" />-->
        <!-- 连接池最小空闲 -->
        <property name="minIdle" value="0" />
        <!-- 获取连接最大等待时间 -->
        <property name="maxWait" value="60000" />
        <property name="validationQuery" value="${validationQuery}" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        <property name="testWhileIdle" value="true" />
        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="25200000" />
        <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandoned" value="true" />
        <!-- 1800秒,也就是30分钟 -->
        <property name="removeAbandonedTimeout" value="1800" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="true" />
        <!-- 监控数据库 -->
        <!-- <property name="filters" value="stat" /> -->
        <property name="filters" value="mergeStat" />
    </bean>


    <!--myBatis配置文件-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--指定Mybatis全局配置文件位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="dataSource" ref="dataSource"/>
        <!-- 自动扫描entity文件,省掉Configuration.xml里面的手工配置,自动扫描mapping文件下的所有xml-->
        <property name="mapperLocations" value="classpath:com/ssm/mappers/*/*.xml"></property>
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ssm.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

    <!-- 配置一个可以执行批量的sqlSession -->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
        <constructor-arg name="executorType" value="BATCH"></constructor-arg>
    </bean>


    <!--配置事物管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>


    <!--1.开启基于注解的事物;2.使用xml配置形式的事物(必须注意的是:推荐使用基于xml配置形式的事物)-->
    <aop:config>
        <!--切入点表达式,对 任意类型 service 包下及其子包中的任务方法中多个参数的方法进行事物控制-->
        <aop:pointcut id="txPoint" expression="execution(* com.ssm.service..*(..))"/>
        <!--配置事物增强-->
        <aop:advisor advice-ref="transactionAdvice" pointcut-ref="txPoint"/>
    </aop:config>

    <!--注解方式配置事物-->
    <!-- <tx:annotation-driven transaction-manager="transactionManager"/>-->
    <!--配置事物增强,事物如何切入.注意【transaction-manager="transactionManager" 属性默认取值为transactionManager,可以省略,如果事物配置器id值不是transactionManager,则必须加上此属性】-->
    <tx:advice id="transactionAdvice">
        <tx:attributes>
            <!--切入点切入的所有方法都是事物方法-->
            <tx:method name="insert*"></tx:method>
            <tx:method name="delete*"></tx:method>
            <tx:method name="update*"></tx:method>
            <tx:method name="modify*"></tx:method>
            <tx:method name="import*"></tx:method>
            <tx:method name="export*"></tx:method>
            <!--切入点切入以get开头的的所有方法都是只读事物方法-->
            <tx:method name="get*" read-only="true"></tx:method>
            <tx:method name="select*" read-only="true"></tx:method>
            <tx:method name="find*" read-only="true"></tx:method>
            <tx:method name="query*" read-only="true"></tx:method>
            <tx:method name="count*" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>
</beans>

4. mybaits-generator.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <!--【注意:修改此文件者,请慎重执行,以免覆盖之前的代码】-->
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <!--阻止生成注释的配置-->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--配置数据库连接信息-->
        <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
                        connectionURL="jdbc:oracle:thin:@192.168.1.254:1521:orcl"
                        userId="familyAccount"
                        password="familyaccount">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!--指定JavaBen生成的位置  .\src指的是当前项目下的src目录下,注意:linux下使用的是./src,而windows下使用的是.\src-->
        <javaModelGenerator targetPackage="testMBG.entity" targetProject="./src/main/java/">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--指定映射文件生成的位置-->
        <sqlMapGenerator targetPackage="testMBG.mappers" targetProject="./src/main/java/">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!--指定 dao 接口生成的位置,mapper接口-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="testMBG.dao" targetProject="./src/main/java/">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!--<table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >
            <property name="useActualColumnNames" value="true"/>
            <generatedKey column="ID" sqlStatement="DB2" identity="true" />
            <columnOverride column="DATE_FIELD" property="startDate" />
            <ignoreColumn column="FRED" />
            <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />
        </table>-->
        <!--<table tableName="tb_department" domainObjectName="Department"></table>
        <table tableName="tb_employee" domainObjectName="Employee"></table>
        <table tableName="tb_menu" domainObjectName="Menu"></table>
        <table tableName="TB_ORGANIZATION" domainObjectName="Organization"></table>-->
        <!--<table tableName="tb_com_log" domainObjectName="ComLog" enableSelectByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableCountByExample="false"></table>-->

        <!--文献资源库-->
       <!-- <table tableName="TB_LITERATURE_RESOURCE" domainObjectName="LiteratureResource" enableSelectByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableCountByExample="false"></table>

        &lt;!&ndash;逆向导入外网数据的表&ndash;&gt;
        <table tableName="V_IMPORT_LITERATURE" domainObjectName="ImportLiterature" enableSelectByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableCountByExample="false"
        enableUpdateByPrimaryKey="false" enableDeleteByPrimaryKey="false"></table>

        <table tableName="V_IMPORT_ATTACHMENT" domainObjectName="ImportAttachment" enableSelectByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableCountByExample="false"
               enableUpdateByPrimaryKey="false" enableDeleteByPrimaryKey="false"></table>-->
        <!--文献订阅-->
        <!--<table tableName="TB_SUBSCRIBE" domainObjectName="Subscribe" enableSelectByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableCountByExample="false"></table>-->
        <table tableName="tb_user" domainObjectName="TbUser" enableSelectByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableCountByExample="false"></table>

    </context>
</generatorConfiguration>

5. ehcach.xml

<?xml version="1.0" encoding="UTF-8" ?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">

    <!--
    java.io.tmpdir - Default temp file path 默认的 temp 文件目录
    maxElementsInMemory:内存中最大缓存对象数.
    maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大.
    eternal:Element是否永久有效,一但设置了,timeout将不起作用.
    overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中
    timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0, 也就是可闲置时间无穷大
    timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,
    默认是0.也就是element存活时间无穷大.
    diskPersistent:是否缓存虚拟机重启期数据。
    diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
    diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区.
    -->
    <diskStore path="D:\\ehcache" />
    <defaultCache maxElementsInMemory="1000" eternal="false"
                  timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
                  maxElementsOnDisk="10000000" diskPersistent="false"
                  diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
</ehcache>

6. 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">
    <display-name>ssm</display-name>
    <!--启动Spring  IOC 容器-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml,classpath:spring-mybatis.xml</param-value>
    </context-param>
    <!--监听配置-->
    <listener>
        <description>配置Spring监听器</description>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <description>防止spring内存溢出监听器,可配置可不配</description>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>

    <!--字符编码过滤器 一定要放在其他过滤器之前-->
    <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>
        <!--spring新版本中 将请求与响应的编码分开了,所以指定一下请求与响应各自的编码:取值为true与false,默认为false-->
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/</url-pattern>
    </filter-mapping>

    <!--使用REST风格的URI 将普通的POST请求转为DELETE或PUT请求-->
    <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>

    <!--Spring MVC servlet配置-->
    <servlet>
        <description>Spring MVC servlet</description>
        <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-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <!--这里指定应答以*.action结尾的请求,如果 是 / 则表示任意-->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

    <!-- druid监控页面,使用${pageContext.request.contextPath}/druid/index.html访问 -->
    <servlet>
        <servlet-name>druidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>druidStatView</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>

    <!-- 配置session超时时间,单位分钟 -->
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>/login.jsp</welcome-file>
        <!--<welcome-file>/index.jsp</welcome-file>-->
    </welcome-file-list>
</web-app>

相关推荐

  1. 04MyBatis核心配置文件

    2024-01-22 13:42:02       34 阅读
  2. MyBatis的核心配置文件

    2024-01-22 13:42:02       18 阅读
  3. MyBatis——MyBatis 核心配置文件

    2024-01-22 13:42:02       6 阅读
  4. Mr_HJ / form-generator项目文档学习与记录

    2024-01-22 13:42:02       35 阅读
  5. spring boot 核心配置文件是什么?

    2024-01-22 13:42:02       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-22 13:42:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-22 13:42:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-22 13:42:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-22 13:42:02       20 阅读

热门阅读

  1. brpc负载均衡load balance和服务发现name servicing

    2024-01-22 13:42:02       29 阅读
  2. 计算机通信:HTTP协议

    2024-01-22 13:42:02       34 阅读
  3. 编程笔记 html5&css&js 050 CSS表格2-1

    2024-01-22 13:42:02       28 阅读
  4. Golang爬虫技术

    2024-01-22 13:42:02       36 阅读
  5. 用go语言删除重复文件

    2024-01-22 13:42:02       36 阅读
  6. Vue.js:构建用户界面的渐进式框架

    2024-01-22 13:42:02       27 阅读
  7. 华为网络设备常用命令大全

    2024-01-22 13:42:02       44 阅读