zoukankan      html  css  js  c++  java
  • Maven构建SSM框架的配置文件整理

    Spring, SpringMVC, MyBatis三个框架整合的项目的相关配置文件整理。

    项目文件目录如下:

    • src/main/java 存放各种项目代码
    • src/main/resources
      • mappers 存放MyBatis的各种mapper.xml
      • application.properties
      • applicationContext.xml
      • servlet-mvc.xml
      • mybatis-config.xml
      • mybatis-generator-config.xml(用于从数据库生成实体和mapper)
    • src/test/java 存放各种测试代码
    • WebContent
      • WEB-INF
        • content 存放各种jsp
        • web.xml
    • pom.xml

    pom.xml

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.22.RELEASE</spring.version>
    </properties>
    
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.2</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
    
        <!-- mybatis依赖 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>
    
        <!-- MySQL数据库依赖 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.3</version>
        </dependency>
    
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jstl-impl</artifactId>
            <version>1.2</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
    
        <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.8</version>
        </dependency>
    
    </dependencies>
    

    Spring配置文件

    • 首先准备一个数据库相关的配置文件application.properties,最简单的配置如下
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/demo?useSSL=false&characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=123456
    
    • 文件开头的命名空间
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
        xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
        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-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/s ... ing-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
    • 然后在配置文件中加载上述文件
    <context:property-placeholder location="classpath:application.properties" />
    
    • 配置数据库连接池,自行根据选取的类进行相应配置
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="acquireIncrement" value="5"></property>
        <property name="initialPoolSize" value="10"></property>
        <property name="minPoolSize" value="5"></property>
        <property name="maxPoolSize" value="20"></property>
    </bean>
    
    • 根据所选的ORM框架,配置对应的sessionFactory
    <!-- hibernate的 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
    </bean>
    
    <!-- mybatis的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <property name="mapperLocations" value="classpath:mappers/*.xml" />
    </bean>
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="pers.corvey.dao"/>
    </bean>
    
    • 配置事务管理
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    • 扫描包
    <context:component-scan base-package="pers.corvey.service"/>
    

    SpringMVC配置文件

    • 文件头的命名空间
    
    <?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-4.1.xsd
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
    
    </beans>
    
    • 需要扫描的类<context:component-scan base-package="pers.corvey.controller"/>

    • 注解<mvc:annotation-driven />

    • 视图名称解析器

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/content/" />
        <property name="suffix" value=".jsp" />
    </bean>
    
    • 静态资源处理<mvc:resources mapping="/resources/**" location="/resources/"/>

    web.xml配置

    • web.xml的加载顺序是:<context-param> - <listener> - <filter> - <servlet>

    • <context-param>包含一个参数名和值,一般需要配置contextConfigLocation来指明applicationContext.xml的位置

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    • <session-config>用于设置HttpSession的失效时间,默认为30min,以分钟为单位,必须为整数,如果为0或负数则永不超时。
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    
    • <filter>过滤器
    <filter>
        <filter-name>encodingFilter</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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    • <listen>监听器
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
    
    • <servlet>
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:servlet-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    • <welcome-file-list>指定首页文件名称
    <welcome-file-list>
        <welcome-file>/WEB-INF/content/index.jsp</welcome-file>
    </welcome-file-list>
    

    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="lazyLoadingEnabled" value="true" />
            <!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
            <setting name="aggressiveLazyLoading" value="false" />
            <!-- 指定哪个对象的方法触发一次延迟加载。 -->
            <setting name="lazyLoadTriggerMethods" value=""/>
            <!-- 是否允许单条sql 返回多个数据集 (取决于驱动的兼容性) default:true -->
            <setting name="multipleResultSetsEnabled" value="true" />
            <!-- 是否可以使用列的别名 (取决于驱动的兼容性) default:true -->
            <setting name="useColumnLabel" value="true" />
            <!-- 允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false -->
            <setting name="useGeneratedKeys" value="true" />
            <!-- 指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射 PARTIAL:部分 FULL:全部 -->
            <setting name="autoMappingBehavior" value="PARTIAL" />
            <!-- 这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新) -->
            <setting name="defaultExecutorType" value="SIMPLE" />
            <!-- 使用驼峰命名法转换字段。 -->
            <setting name="mapUnderscoreToCamelCase" value="true" />
            <!-- 设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session -->
            <setting name="localCacheScope" value="SESSION" />
            <!-- 设置但JDBC类型为空时,某些驱动程序 要指定值,default:OTHER,插入空值时不需要指定类型 -->
            <setting name="jdbcTypeForNull" value="NULL" />
            <!-- 输出SQL语句 -->
            <setting name="logImpl" value="STDOUT_LOGGING" />
        </settings>
    
        <plugins>
            <plugin interceptor="com.github.pagehelper.PageInterceptor">
                <!-- 该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为true时,使用 RowBounds 分页会进行 count 查询 -->
                <property name="rowBoundsWithCount" value="false" />
                <!-- 当该参数设置为 true 时,如果 pageSize=0 或者 RowBounds.limit = 0 就会查询出全部的结果 -->
                <property name="pageSizeZero" value="false" />
                <!-- 当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页 -->
                <property name="reasonable" value="true" />
                <!-- 支持通过 Mapper 接口参数来传递分页参数 -->
                <property name="supportMethodsArguments" value="false" />
            </plugin>
        </plugins>
    </configuration>
    

    日志相关配置有待补充

    参考文献

  • 相关阅读:
    [OS] 信号量(Semaphore)
    [OS] 进程互斥
    [剑指Offer] 52.正则表达式匹配
    [剑指Offer] 51.构建乘积数组
    [剑指Offer] 50.数组中重复的数字
    [剑指Offer] 49.把字符串转换成整数
    [剑指Offer] 48.不用加减乘除做加法
    [剑指Offer] 47.求1+2+3+...+n
    PHP知识库图谱汇总(完善中)
    修改thinkphp路由模式,去掉Home
  • 原文地址:https://www.cnblogs.com/corvey/p/10474185.html
Copyright © 2011-2022 走看看