1. 程式人生 > >SSM綜合配置檔案【註解】

SSM綜合配置檔案【註解】

一:配置applicationContext.xml配置檔案重點內容
1.寫dbcp.properties檔案(連線資料庫資訊)
這裡寫圖片描述

2.新增spring和springMvc的各種約束

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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 ">

3.開啟註解掃描

<!-- 開啟註解掃描 -->
    <context:component-scan base-package="com.ssm" />

4.在配置檔案中,載入jdbc.properties檔案,並配置資料來源(dbcp , c3p0 , jdbc …)
//這裡我用的是dbcp資料來源

<!-- 載入jdbc.properties檔案 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!-- 配置資料來源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

5.配置sqlSessionfactory(Mybatis)

<!-- 配置sqlSessionfactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- 給bean下面的類,起別名 -->
        <property name="typeAliasesPackage" value="com.ssm.bean"></property>
        <!-- 載入mapper的對映檔案 -->
        <property name="mapperLocations" value="classpath:com/ssm/mapper/*.xml"></property>
    </bean>

6.配置Mybatis的mapper介面

<!-- 讀取mapper包下的mapper介面 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ssm.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

7.配置事務管理和事務註解


    <!-- 配置事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 事務註解 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

8.配置SpringMVC的試圖解析器和註解(可分開配置)

<!-- 配置試圖解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- MVC註解 -->
    <mvc:annotation-driven/>

8.配置使用(可選)

<!-- 配置js,css和其它使用 -->
    <mvc:default-servlet-handler/>

9.Date型別(可選)
定義一個類,實現Converter

public class CustomDateConverter implements Converter<String,Date>{

    @Override
    public Date convert(String source) {

        //實現 將日期串轉成日期型別(格式是yyyy-MM-dd HH:mm:ss)
        if(!"".equals(source)){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

            try {
                //轉成直接返回
                return simpleDateFormat.parse(source);
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        //如果引數繫結失敗返回null
        return null;
    }

}
<!-- 在MVC註解中配置Date轉換器 -->
    <mvc:annotation-driven conversion-service="conversionService" />
    <!-- 自定義引數繫結 -->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <!-- 轉換器 -->
        <property name="converters">
            <list>
                <!-- 日期型別轉換 -->
                <bean class="com.ssm.action.CustomDateConverter"/>
            </list>
        </property>
    </bean>

二:web.xml
1.載入applicationContext配置檔案和監聽器

<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的前端控制器

<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>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

3.配置編碼格式過濾器(亂碼)

<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>