SSM項目配置文件及其各項使用


$說明:

   ·Spring 5  + Mybatis 3.4.5 +SpringMVC 

   ·使用druid數據庫

   ·使用log4j輸出日志

$Spring 及其配置文件(部分)

Spring官方網站:http://spring.io/

Spring重點: 

  ·IOC(控制反轉)

  ·DI(依賴注入)

  ·AOP (面向切面編程)

<?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-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
           ">

    <!-- 加載properties文件 -->
    <context:property-placeholder location="classpath:druid.properties" system-properties-mode="NEVER" />
    <!-- Spring 注解解析器 -->
    <context:annotation-config />
    <!-- 掃描 -->
    <context:component-scan  base-package="com.xxx.*" />

    <!-- druid配置 -->
    <bean id="dataSource"  class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="${druid.url}"></property>
        <property name="driverClassName"  value="${druid.driverClassName}"></property>
        <property name="username" value="${druid.username}"></property>
        <property name="password" value="${druid.password}"></property>

    </bean>

    <!-- mybatis文件配置,掃描所有mapper文件 -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 注入數據源 -->
        <property name="dataSource" ref="dataSource"></property>
         <!-- 配置mybatis全局文件 -->
        <property name="configLocation"   value="classpath:mybatis.xml"></property>  <!-- 加載mybatis文件 -->
        <property name="mapperLocations" value="classpath:mapper/*Mapping.xml"></property>  <!-- 掃描所有mapper路徑 -->
        
    </bean>


    <!-- DAO接口所在包名,Spring會自動查找其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.xxx.dao" />
    </bean>
 

</beans>

$ Mybatis 及其配置(部分)

Mybatis官方網站:http://blog.mybatis.org/

Mybatis下載地址:https://github.com/mybatis/mybatis-3/releases

Mybatis重點:

    ·全局配置文件

    ·Mybatis映射文件

    ·動態SQL

    ·一對多,多對一等關系

<?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>
<!-- mybatis的全局配置文件 -->
    <typeAliases>
    
        <typeAlias type="com.xxx.pojo.User" alias="User" />
    </typeAliases>

</configuration>

<部分代碼可寫入Spring中>

$SpringMVC 及其配置(部分)

SpringMVC無縫接入Spring ,可在Spring官網中及其文檔中查看

SpringMVC重點

    ·控制器的使用Controller

    ·json

    ·放行靜態資源:*.js  *.css *.jpg等

    ·攔截器

    ·核心:前端控制器(DispatcherServlet

    ·多視圖控制

<?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:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
           ">
            
            <!-- <import resource="classpath:ApplicationContext.xml"/>        -->
              <!-- 控制器掃描器 -->
              <context:component-scan base-package="com.xxx.controller"/>
              
              
              <!-- 
              會自動注冊DefaultAnnotationHandlerMapping與AnnotationMethodHandlerAdapter 兩個bean,
              是spring MVC為@Controllers分發請求所必須的。
            並提供了:數據綁定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,
            讀寫XML的支持(JAXB),讀寫JSON的支持(Jackson)。
               -->
              <mvc:annotation-driven />
              <!-- 放行靜態資源 -->    
              <mvc:default-servlet-handler/>
              
              <!-- 配置攔截器 -->
              <mvc:interceptors>
              
              <mvc:interceptor>
              <!-- 攔截路徑 -->
              <mvc:mapping path="/**"/>
              <!-- 放行 -->
              <mvc:exclude-mapping path="/User/login"/>
              <!-- 攔截類 -->
              <bean class="com.xxx.Interceptor.CheckLoginInterceptor"/>
              </mvc:interceptor>
              
              </mvc:interceptors>
              
              
              
              <!--
              配置視圖解析器
         -->
           <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="prefix" value="/WEB-INF/views/"></property>
           <property name="suffix" value=".jsp"></property>
           </bean>
           
      
           </beans>

$其他配置(部分)

  ·log4j日志配置:

#日志的基本設置
# 設置日志的全局配置,級別越小顯示的越詳細  trace<debug<info<warn<error<fatal
log4j.rootLogger=debug, stdout
# log4j.logger.加需要輸出的包的路徑  並設置日志級別
log4j.logger.com.Mapping=TRACE
# Console output...  將文件輸出達到某個位置  ConsoleAppender 輸出到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

  ·druid數據庫配置:

#druid基本配置
druid.url=jdbc:mysql://127.0.0.1/springmvc?useUnicode=true&characterEncoding=utf8
druid.driverClassName=com.mysql.jdbc.Driver
druid.username=root
druid.password=082999

$注意:

    ·部分包名如: com.xxx.Controller      是自用包名。請讀者詳細查看后記得修改。 不然會報配置文件無效相關的錯誤。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM