SSM框架–CRM項目-創建項目配置文件

上一節我們介紹了項目的創建和Jar包的導入,接下來我們介紹項目配置的搭建。
框架搭建
這裏寫圖片描述


詳解
這裏寫圖片描述

配置文件詳解
①db.properties

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/crm
jdbc.user=root
jdbc.password=

②log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

③sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--導入MyBatis約束-->
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!--爲類起別名-->
    <typeAliases>
        <!--掃描包下的所有類和子包-->
        <package name="com.crm.domain"></package>
    </typeAliases>
    <!--environments環境在Spring配置 這裏省略-->
    <!--mapper標籤省略,在Spring中使用MyBatis動態代理-->
</configuration>

④applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--導入Spring約束-->
<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/schema/context/spring-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">

    <!-- 配置C3P0連接池 -->
        <!-- 導入db.properties文件 -->
        <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
        <!--配置數據源-->
        <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driverClass}"></property>
            <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
            <property name="user" value="${jdbc.user}"></property>
            <property name="password" value="${jdbc.password}"></property>
        </bean>

    <!--配置sqlSessionFactory-->
    <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置數據庫連接池-->
        <property name="dataSource" ref="dataSource"></property>
        <!--導入sqlMapConfig.xml-->
        <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
    </bean>

    <!--MyBatis動態代理增強(掃描)-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定基本包-->
        <property name="basePackage" value="com.crm.dao"></property>
    </bean>
</beans>

⑤springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--導入SpringMVC約束-->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       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-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!--自動掃描com.crm下的所有組件
        @Component
            @Controller  控制層
            @Service     服務層
            @Repository  dao層
    -->
    <context:component-scan base-package="com.crm"></context:component-scan>

    <!--
        默認自動註冊RequestMappingHandlerMapping和RequestMappingHandlerAdapter兩個Bean
    -->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

⑥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_1.xsd"
           version="3.1">


    <!--引入Spring容器,使容器隨項目的啓動二創建-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--指定Spring配置文件的位置-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>


    <!--處理Post中文提交亂碼-->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!--配置SpringMVC-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--指定SpringMVC配置文件的位置-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!-- 
        1./* 攔截所有JSP .js .png .css  真的攔截  不建議使用
        2.*.action,*.do 攔截以action結尾的請求   肯定能使用   使用在後臺
        3./  攔截所有  不包括JSP 但是包括.js .png .css文件 強烈建議使用  使用在前臺  放行靜態資源
        -->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

    <!--配置主頁-->
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>
</web-app>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章