ssm配置文件随记

这里简单记录一下SSM里的各种配置文件,简单记录一下,作为基础。注意,xml中某些配置是有顺序要求的。

web.xml

  1. 引入约束:约束是作为其子标签的编写规范,可以作为提示和规范。

    1
    2
    3
    4
    5
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    </web-app>
  2. 配置spring配置文件的位置

    1
    2
    3
    4
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
  3. 配置乱码过滤器:此处的配置是为了配置接受数据的编码信息。(注意是接收参数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <!--乱码过滤器-->
    <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>
    </filter>
    <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
  4. spring监听器:此配置是确保在webapp启动的时候spring容器也被初始化。(所以这里也配置了spring配置文件的位置)

    1
    2
    3
    4
    <!--spring 监听器-->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
  5. spring-mvc前端控制器:此配置即springMVC的servlet,让所有的请求都走springMVC的控制器,即只走springMVC的servlet。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    <!--springmvc的前端控制器-->
    <servlet>
    <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>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    applicationContext.xml(spring配置文件)

    1. 引入约束:注意spring不同的部分有不同的约束,如基本 的beans,aop,contex等(这里也不需要记每个地址,只需要将xmlns:xxx=…xxx…中的xxx改为对应的单词,如aop,tx)。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      <?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:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      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/tx
      http://www.springframework.org/schema/tx/spring-tx.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
      <beans>
    2. 配置组件扫描(扫描service和mapper):注意context:component-scan内部可以包含context:exclude-filter或者context:include-filter

      1
      2
      3
      4
      5
      <!--组件扫描 扫描service和mapper-->
      <context:component-scan base-package="com.itheima">
      <!--排除controller的扫描-->
      <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
      </context:component-scan>
    3. 加载properties文件:可以引入外部的文件,使用其中的变量

      1
      2
      <!--加载propeties文件-->
      <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    4. 配置数据源:这里采用的c3p0连接池。连接池的本质是为我们维护一系列的数据库连接,防止每此都要打开和关闭连接,浪费资源

      1
      2
      3
      4
      5
      6
      7
      <!--配置数据源信息-->
      <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="driverClass" value="${jdbc.driver}"></property>
      <property name="jdbcUrl" value="${jdbc.url}"></property>
      <property name="user" value="${jdbc.username}"></property>
      <property name="password" value="${jdbc.password}"></property>
      </bean>
    5. 配置sessionFactory:配置mybatis的sessionFactory,用来获取mybatis的session

      1
      2
      3
      4
      5
      6
      <!--配置sessionFactory-->
      <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"></property>
      <!--加载mybatis核心文件-->
      <property name="configLocation" value="classpath:sqlMapConfig-spring.xml"></property>
      </bean>
    6. 扫描mapper包,为mapper接口创建实现类

      1
      2
      3
      4
      <!--扫描mapper所在的包 为mapper创建实现类-->
      <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.itheima.mapper"></property>
      </bean>
    7. 声明事务控制

      1. 平台事务管理器

        1
        2
        3
        4
        <!--平台事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
        </bean>
      2. 配置事务增强:注意 transaction-manager="transactionManager"可以省略,因为其默认即为 transaction-manager="transactionManager"

        1
        2
        3
        4
        5
        6
        <!--配置事务增强-->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
        <tx:method name="*"/>
        </tx:attributes>
        </tx:advice>
      3. 配置事务的aop织入

        1
        2
        3
        4
        <!--事务的aop织入-->
        <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.itheima.service.impl.*.*(..))"></aop:advisor>
        </aop:config>

      spring-mvc.xml

      1. 引入约束

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        <?xml version="1.0" encoding="UTF-8" ?>
        <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        </beans>
      2. 组件扫描:只扫描controller

        1
        2
        <!--组件扫描  主要扫描controller-->
        <context:component-scan base-package="com.itheima.controller"></context:component-scan>
      3. 配置mvc注解驱动:这个标签注册了一系列的beans和adapts,比如JSON(当我们引入了Jackson,则不需要单独配置,在直接在@ResponseBody的controller中返回以额对象就会直接返回一个json),valid等等。

        1
        2
        3
        4
        5
        6
        7
        8
        <!--配置mvc注解驱动-->
        <mvc:annotation-driven>
        <mvc:message-converters>
        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="defaultCharset" value="UTF-8"></property>
        </bean>
        </mvc:message-converters>
        </mvc:annotation-driven>
      4. 注册文件上传(可选):注意id必须为multipartResolver,因为spring是按id获取这个bean的。(需要引入commons-fileupload坐标)

        1
        2
        3
        4
        5
        <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
        <property name="maxUploadSize" value="5242800"/>
        <property name="maxUploadSizePerFile" value="5242800"/>
        </bean>
      5. 配置默认servlet:当spring-mvc无法匹配到请求的请求时,使用默认的servlet来处理,可以用来暴露静态资源。

        1
        <mvc:default-servlet-handler/>

      sqlMapConfig.xml(不整合spring时的配置)

      1. 设置打印sql语句和结果

        1
        2
        3
        <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
        </settings>
      2. 引入约束

        1
        2
        3
        4
        <?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">
      3. 加载properties文件

        1
        <properties resource="jdbc.properties"></properties>
      4. 定义别名:这里定义实体类的别名,我们在mapper中的可以使用别名来代替很长的全路径名称。注意这里可以单独配置,也可以扫包。

        1
        2
        3
        4
        <typeAliases>
        <!--<typeAlias type="com.itheima.domain.Account" alias="account"></typeAlias>-->
        <package name="com.itheima.domain"></package>
        </typeAliases>
      5. 配置环境:这里配置环境,即可以配置多套设置,比如developmentproduction

        1
        2
        3
        4
        5
        6
        7
        8
        9
        10
        11
        12
        <!-- 前面在spring配置文件中配置bean之后就不用在这里配了 -->
        <environments default="developement">
        <environment id="developement">
        <transactionManager type="JDBC"></transactionManager>
        <dataSource type="POOLED">
        <property name="driver" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
        </dataSource>
        </environment>
        </environments>
      6. 加载映射文件:加载XXXmapper.xml。用在配置文件的情况下。同样可以扫包和单独配置。

        1
        2
        3
        4
        <mappers>
        <!--<mapper resource="com/itheima/mapper/AccountMapper.xml"></mapper>-->
        <package name="com.itheima.mapper"></package>
        </mappers>

      sqlMapConfig-spring.xml(整合spring的mybatis)

      注意mybatis和spring整合后,大部分的配置都在spring里写好了。所以这里不用配置太多。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      <?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="logImpl" value="STDOUT_LOGGING"/>
      </settings>

      <!--定义别名-->
      <typeAliases>
      <!--<typeAlias type="com.itheima.domain.Account" alias="account"></typeAlias>-->
      <package name="com.itheima.domain"></package>
      </typeAliases>

      </configuration>

      xxxmapper.xml(mapper注解方式)

      注意:xxxmapper.xml必须与xxxmapper.class位于同一个包下(可以是放在src中,也可以是在resource下新建路劲完全相同的包,否则无法匹配)。

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      <?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="priv.mw.mapper.UserMapper">

      <sql id="selectAll">
      select * from user
      </sql>

      <select id="findAll" resultType="user">
      <include refid="selectAll"></include>
      </select>

      <insert id="insert" parameterType="user">
      insert user values (#{id}, #{username}, #{password}, #{birthday})
      </insert>

      <update id="update" parameterType="user">
      update user set
      <if test="username != null">
      username = #{username}
      </if>

      <if test="password != null">
      password = #{password}
      </if>

      <if test="birthday != null">
      birthday = #{birthday}
      </if>
      <where>
      id = #{id}
      </where>
      </update>

      <delete id="delete" parameterType="user">
      delete user from user where id = #{id}
      </delete>
      </mapper>

    xxx.class(mapper接口方式)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package priv.mw.mapper;

    import org.apache.ibatis.annotations.*;
    import priv.mw.domain.User;

    import java.util.List;

    @Mapper
    public interface UserMapper {

    @Select("select * from user")
    public List<User> findAll();

    @Insert("insert into user values (#{id}, #{username}, #{password}, #{birthday})")
    public Integer insert(User user);

    @Update("update user set username = #{username}, password=#{password}, birthday=#{birthday} where id = #{id}")
    public Integer update(User user);

    @Delete("delete from user where id = #{id}")
    public Integer delete(User user);

    }

    log4j.properties

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    #
    # Hibernate, Relational Persistence for Idiomatic Java
    #
    # License: GNU Lesser General Public License (LGPL), version 2.1 or later.
    # See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
    #

    ### direct log messages to stdout ###
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.err
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

    ### direct messages to file hibernate.log ###
    #log4j.appender.file=org.apache.log4j.FileAppender
    #log4j.appender.file.File=hibernate.log
    #log4j.appender.file.layout=org.apache.log4j.PatternLayout
    #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

    ### set log levels - for more verbose logging change 'info' to 'debug' ###

    log4j.rootLogger=all, stdout

Powered by Hexo and Hexo-theme-hiker

Copyright © 2019 - 2024 My Wonderland All Rights Reserved.

UV : | PV :