• spring项目篇2----编写配置文件


    我们接下来开始编写我们的权限系统,以便进一步熟悉SSM,接下来按照第一篇起一个maven项目,项目简介如下,接下来看一下配置文件 

     web.xml

    <!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 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"
             metadata-complete="false">
      <display-name>Archetype Created Web Application</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    
      <!--配置前端控制器-->
      <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:applicationContext.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>
      <!--配置编码过滤器-->
      <filter>
        <filter-name>CharacterEncoding</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>CharacterEncoding</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>

    接下来看一下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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
        <!--注解扫描,这是扫描的包路径-->
        <context:component-scan base-package="com.yang" />
    
        <!--导入mybatis的配置-->
        <import resource="classpath:application-mybatis" />
    
        <!--导入springMvc配置-->
        <import resource="classpath:application-mvc.xml" />
    
    </beans>

    数据库属性文件

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/promission?characterEncoding=utf-8
    jdbc.username=root
    jdbc.password=mysql

    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"
           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
    ">
        <!--加载数据库属性文件-->
        <context:property-placeholder location="classpath:db.properties" />
        <!--配置德鲁伊连接吃-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
            <property name="initialSize" value="5" />
            <!--属性文件中的名称不能跟property的name一样,因此一般这个属性文件名称前面会加上jdbc-->
            <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>
        <!--配置事务管理-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <!--设置数据源-->
            <property name="dataSource" ref="dataSource" />
        </bean>
        <!--开启注解事务-->
        <tx:annotation-driven transaction-manager="transactionManager" />
        <!--配置Mybatis工厂-->
        <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!--核心配置文件位置-->
            <property name="configLocation" value="classpath:sqlMapConfig.xml" />
            <!--配置mapper映射文件的路径-->
            <property name="mapperLocations" value="classpath:com.yang.mapper" />
        </bean>
        <!--配置Mapper接口扫描-->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!--设置mapper接口包的位置-->
            <property name="basePackage" value="com.yang.mapper" />
        </bean>
    </beans>

    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.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        ">
        <!--主要就是为了Spring MVC来用的,提供Controller请求转发,json自动转换等功能-->
        <mvc:annotation-driven />
        <!--静态资源处理-->
        <mvc:default-servlet-handler />
        <!--视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views" />
            <property name="suffix" value=".jsp" />
        </bean>
        <!--配置拦截器,先注释,到时候写的时候在进行配置-->
    <!--    <mvc:interceptors>-->
    <!--        <mvc:interceptor>-->
    <!--            <mvc:mapping path="/*/" />-->
    <!--        </mvc:interceptor>-->
    <!--    </mvc:interceptors>-->
        <!--配置文件上传解析器-->
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <!--设置上传最大文件大小-->
            <property name="maxUploadSize">
                <value>10240000</value>
            </property>
        </bean>
    
    </beans>

    sqlMapConfig

    <?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>
            <!--设置sql语句输出到终端-->
            <setting name="logImpl" value="STDOUT_LOGGING"/>
        </settings>
        <typeAliases>
            <!-- 批量别名定义,扫描整个包下的类,别名为类名(大小写不敏感) -->
            <package name="com.yang.domain" />
        </typeAliases>
        <plugins>
            <!--配置分页插件-->
            <plugin interceptor="com.github.pagehelper.PageHelper" />
        </plugins>
    </configuration>

    最后看一下可以根据数据库帮助我们生成mapper接口以及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>
    
        <!--targetRuntime有两个参数一个是MyBatis3Simple只生成简单的增删改查,另外一个会生成很多操作,
        但那些基本都是我们自己编写,一次一般只使用MyBatis3Simple-->
        <context id="mysql" defaultModelType="hierarchical" targetRuntime="MyBatis3Simple">
            <!-- 自动识别数据库关键字,
            默认false,如果设置为true,
            根据SqlReservedWords中定义的关键字列表;
            一般保留默认值,
            遇到数据库关键字(Java关键字),
            使用columnOverride覆盖 -->
            <property name="autoDelimitKeywords" value="false" />
            <!-- 生成的Java文件的编码 -->
            <property name="javaFileEncoding" value="UTF-8" />
            <!-- 格式化java代码 -->
            <property name="javaFormatter" value="org.mybatis.generator.api.dom.DefaultJavaFormatter" />
            <!-- 格式化XML代码 -->
            <property name="xmlFormatter" value="org.mybatis.generator.api.dom.DefaultXmlFormatter" />
    
            <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,
            比如ORACLE就是双引号,MYSQL默认是`反引号; -->
            <property name="beginningDelimiter" value="`" />
            <property name="endingDelimiter" value="`" />
    
            <!--阻止生成日期和注释-->
            <commentGenerator>
                <property name="suppressDate" value="true" />
                <property name="suppressAllComments" value="true" />
            </commentGenerator>
    
            <!-- 数据库连接信息 -->
            <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql:///promission" userId="root" password="mysql" />
    
            <!-- java类型处理器 用于处理数据库中的类型到Java中的类型,
                 默认使用JavaTypeResolverDefaultImpl;
                 注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和NUMERIC数据类型; -->
            <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl">
                <!-- true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型 false:默认,
                    scale>0;length>18:使用BigDecimal;
                    scale=0;length[10,18]:使用Long;
                    scale=0;length[5,9]:使用Integer;
                    scale=0;length<5:使用Short; -->
                <property name="forceBigDecimals" value="false" />
            </javaTypeResolver>
    
    
            <!--
            配置domain生成策略
            targetProject:把自动生成的domain放在哪个工程里面
            targetPackage:哪个包下
            -->
            <javaModelGenerator targetPackage="com.yang.domain" targetProject="src/main/java">
                <!-- for MyBatis3/MyBatis3Simple
                自动为每一个生成的类创建一个构造方法,
                构造方法包含了所有的field;而不是使用setter; -->
                <property name="constructorBased" value="false" />
    
                <!-- for MyBatis3 / MyBatis3Simple
                是否创建一个不可变的类,如果为true,
                那么MBG会创建一个没有setter方法的类,
                取而代之的是类似constructorBased的类 -->
                <property name="immutable" value="false" />
    
                <!-- 设置是否在getter方法中,
                对String类型字段调用trim()方法 -->
                <property name="trimStrings" value="true" />
            </javaModelGenerator>
    
            <!--
                配置mapper.xml的生成策略
                targetPackage:把自动生成的mapper放在哪个工程里面
                targetProject:哪个包下
            -->
            <sqlMapGenerator targetPackage="com.yang.mapper"
                             targetProject="src/main/java">
                <!-- 在targetPackage的基础上,
                根据数据库的schema再生成一层package,
                最终生成的类放在这个package下,默认为false -->
                <property name="enableSubPackages" value="true" />
            </sqlMapGenerator>
    
            <!--mapper接口生成策略-->
            <javaClientGenerator targetPackage="com.yang.mapper" type="XMLMAPPER" targetProject="src/main/java">
                <property name="enableSubPackages" value="true" />
            </javaClientGenerator>
            <!--指定生成那个表-->
            <table tableName="user"></table>
    
    
        </context>
    </generatorConfiguration>

    文件结尾,我们看一下maven的项目依赖

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>com.yang</groupId>
      <artifactId>promissionPro</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <name>promissionPro Maven Webapp</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    
        <!--定义版本号   ${org.springframework.version}-->
        <org.springframework.version>5.2.3.RELEASE</org.springframework.version>
        <org.mybatis.version>3.5.3</org.mybatis.version>
    
      </properties>
    
      <dependencies>
        <!--单元测试-->
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
        <!--lombok-->
        <!--Lombok能通过注解的方式,在编译时自动为属性生成构造器、getter/setter、equals、hashcode、toString方法。-->
        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.10</version>
        </dependency>
        <!-- servlet-api -->
        <!--用到HttpServletRequest和HttpServletResponse等对象,这些对象都是要靠这个jar包才能使用的-->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>servlet-api</artifactId>
          <version>2.5</version>
          <scope>provided</scope>
        </dependency>
        <!--mysql驱动-->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>8.0.19</version>
        </dependency>
        <!-- druid -->
        <!--阿里云德鲁伊连接池-->
        <dependency>
          <groupId>com.alibaba</groupId>
          <artifactId>druid</artifactId>
          <version>1.1.21</version>
        </dependency>
        <!-- common-lang -->
        <!--commons-lang主要目的是提供更多的工具类,其实也就是方便我们的开发-->
        <dependency>
          <groupId>commons-lang</groupId>
          <artifactId>commons-lang</artifactId>
          <version>2.6</version>
        </dependency>
        <!--spring 框架-->
        <!--spring-test-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-test</artifactId>
          <version>${org.springframework.version}</version>
          <scope>test</scope>
        </dependency>
        <!--spring-core-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${org.springframework.version}</version>
        </dependency>
        <!--spring-context-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${org.springframework.version}</version>
        </dependency>
        <!--spring-context-support-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>${org.springframework.version}</version>
        </dependency>
        <!--spring-expression-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-expression</artifactId>
          <version>${org.springframework.version}</version>
        </dependency>
        <!--spring-jdbc-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>${org.springframework.version}</version>
        </dependency>
        <!--spring-tx-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>${org.springframework.version}</version>
        </dependency>
        <!--spring-web-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${org.springframework.version}</version>
        </dependency>
        <!--spring-aop-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${org.springframework.version}</version>
        </dependency>
        <!--spring-webmvc-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${org.springframework.version}</version>
        </dependency>
    
        <!-- aspectj -->
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjrt</artifactId>
          <version>1.9.5</version>
        </dependency>
        <!--aspectj weaver-->
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.9.5</version>
        </dependency>
    
        <!-- cglib -->
        <!--动态代理包-->
        <dependency>
          <groupId>cglib</groupId>
          <artifactId>cglib</artifactId>
          <version>3.3.0</version>
        </dependency>
    
        <!--mybatis包-->
        <!-- mybatis -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>${org.mybatis.version}</version>
        </dependency>
        <!--mybatis-spring-->
        <!--整合mybatis以及spring框架-->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>2.0.3</version>
        </dependency>
    
        <!--实现基本的序列化和反序列化-->
        <!-- jackson-core -->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-core</artifactId>
          <version>2.10.2</version>
        </dependency>
        <!--jackson-databind-->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-databind</artifactId>
          <version>2.10.2</version>
        </dependency>
        <!--jackson-annotations-->
        <dependency>
          <groupId>com.fasterxml.jackson.core</groupId>
          <artifactId>jackson-annotations</artifactId>
          <version>2.10.2</version>
        </dependency>
    
        <!--提供日志整合-->
        <!--slf4j-api-->
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-api</artifactId>
          <version>1.7.6</version>
        </dependency>
        <!--slf4j-log4j12-->
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.7.6</version>
        </dependency>
        <!--log4j-->
        <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.17</version>
        </dependency>
        <!--上传文件-->
        <!--commons-fileupload-->
        <dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.4</version>
        </dependency>
        <!--就是在jsp文件中提供特殊标签,for循环,if判断等-->
        <!-- jstl -->
        <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>1.2</version>
        </dependency>
        <!--JSP 标准标签库-->
        <!-- standard -->
        <dependency>
          <groupId>taglibs</groupId>
          <artifactId>standard</artifactId>
          <version>1.1.2</version>
        </dependency>
        <!--mybatis分页组件-->
        <!--pagehelper-->
        <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper</artifactId>
          <version>5.1.11</version>
        </dependency>
    
      </dependencies>
    
      <build>
        <finalName>promissionPro</finalName>
        <!--这个是mybatis的一个插件,这个插件可以帮助我们根据数据库自动创建pojo类,以及我们的映射接口以及xml文件-->
        <!--注意这个插件不要添加到pluginManagement标签中,否则没有作用-->
        <plugins>
          <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.4.0</version>
            <configuration>
              <verbose>true</verbose>
              <overwrite>false</overwrite>
            </configuration>
            <!--因为需要连接数据库,所以需要提供mysql的支持-->
            <dependencies>
              <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.19</version>
              </dependency>
            </dependencies>
          </plugin>
        </plugins>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
          <plugins>
            <plugin>
              <artifactId>maven-clean-plugin</artifactId>
              <version>3.1.0</version>
            </plugin>
            <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
            <plugin>
              <artifactId>maven-resources-plugin</artifactId>
              <version>3.0.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.8.0</version>
            </plugin>
            <plugin>
              <artifactId>maven-surefire-plugin</artifactId>
              <version>2.22.1</version>
            </plugin>
            <plugin>
              <artifactId>maven-war-plugin</artifactId>
              <version>3.2.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-install-plugin</artifactId>
              <version>2.5.2</version>
            </plugin>
            <plugin>
              <artifactId>maven-deploy-plugin</artifactId>
              <version>2.8.2</version>
            </plugin>
          </plugins>
        </pluginManagement>
      </build>
    </project>
  • 相关阅读:
    MArkdown使用转
    吴恩达deep learning笔记。
    TCP状态转换图
    TCP关闭的四次握手
    SYN的中发送若干的TCP选项
    Ubuntu系统中切换root用户的命令,总忘了sudo
    logistic regression recap
    二分分类中的logistic回归(regression)
    UNIX下的网络族分类,IPv4和IPv6协议族
    11.8模拟赛
  • 原文地址:https://www.cnblogs.com/yangshixiong/p/12266920.html
Copyright © 2020-2023  润新知