ssm項目之web.xml,spring,springmvc,mybatis配置


本文地址:http://www.cnblogs.com/maplefighting/p/7482549.html 

可以安裝spring ide,有些自動補全簡單多了,配置地址:

http://www.cnblogs.com/maplefighting/p/7481762.html

一、web.xml

配置spring容器,前端控制器,字符過濾器

比如web.xml配置可以alt+/ ,然后點擊就自動生成了

創建applicationContext.xml文件時可以右鍵new --> other找到spriing......然后直接創建

web.xml代碼如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

    <!-- 1、啟動spring的容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 2、springmvc前端控制器,攔截所有請求 -->
    <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 3、字符編碼過濾器 -->
    <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>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
    <!-- 使用Rese風格的URI,將頁面普通的post請求轉為指定的delete或者put請求 -->
    <filter>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    
</web-app>
View Code

前端控制器這樣配置需創建一個dispatcherServlet.xml在同級目錄下,名字  dispatcherServlet-servlet.xml

 

二、springmvc配置

 也就是配置dispatcherServlet-servlet.xml,包含網站跳轉邏輯的控制配置,配置視圖解析器

 有一些補齊沒顯示的話可以按下圖勾下

 

context:component-scan可以把context勾上

代碼如下:

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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.3.xsd">

    <!-- SpringMVC的配置文件,包含網站跳轉邏輯的控制配置 -->
    <context:component-scan base-package="com.sgd" use-default-filters="false">
        <!-- 只掃描控制器 -->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- 配置試圖解析器,方便頁面返回 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!-- 在web-inf下創建views文件,視圖就放這里 -->
        <property name="prefix" value="/WEB-INF/views/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 兩個標准配置 -->
    <!-- 將springmvc不能處理的請求交給tomcat -->
    <mvc:default-servlet-handler/>
    <!-- 能支持springmvc更高級的一些功能 ,JSR303校驗,快捷的ajax-->
    <mvc:annotation-driven/>
</beans>
View Code

一些類可以crtl+shift+t,然后搜索,之后復制名字,這樣比較不容易敲錯

三、spring的配置文件applicationContext.xml

配置數據源,與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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <context:component-scan base-package="com.sgd">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- spring的配置文件,這里主要配置和業務邏輯有關的 -->
    <!-- ==============數據源,事務控制....=============== -->
    <context:property-placeholder location="classpath:dbconfig.properties"/>
    <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    
    <!--=================配置和MyBatis的整合============== -->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 配置MyBatis全局配置文件的位置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="dataSource" ref="pooledDataSource"></property>
        <!-- 指定mybatis,mapper文件的位置 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>
    
    <!-- 配置掃描器,將mybatis接口的實現加入到ioc容器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 掃描所有dao接口的實現,加入到ioc容器中 -->
        <property name="basePackage" value="com.sgd.crud.dao"></property>
    </bean>
    <!-- ============================================ -->
    
    <!-- ==============事務控制的配置===================== -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 控制住數據源 -->
        <property name="dataSource" ref="pooledDataSource"></property>
    </bean>
    <!-- 開啟基於注解的事務,使用xml配置形式的事務(必要主要的都是使用配置式) -->
    <aop:config>
    <!-- 切入點表達式 -->
        <aop:pointcut expression="execution(* com.sgd.crud.service..*(..))" id="txPoint"/>
        <!-- 配置事務增強 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>
    <!-- 配置事務增強,事務如何切入 可加可不加transaction-manager="transactionManager"-->
    <tx:advice id="txAdvice" >
        <tx:attributes>
            <!-- 所有方法都是事務方法 -->
            <tx:method name="*"/>
            <!-- 以get開始的所有方法 -->
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
    <!-- spring配置文件的核心點(數據源,與mybatis的整合,事務控制) -->
</beans>
View Code

四、mybaits配置文件

mybatis-config.xml 相關配置可以到http://www.mybatis.org/mybatis-3/ 看看,根據需要配置,這個文件其實可以不要,直接配置到spring配置文件中

<?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="mapUnderscoreToCamelCase" value="true"/>
     </settings>
     <typeAliases>
         <package name="com.sgd.crud.bean"/>
     </typeAliases>
 </configuration>
View Code

接下來就是創建表了,上面的.xml文件后面差什么東西再加


免責聲明!

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



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