SSM综合配置文件【注解】

本文详细介绍了SSM(Spring、SpringMVC、Mybatis)整合中的applicationContext.xml配置,包括数据库连接信息的dbcp.properties文件,Spring和SpringMVC的约束添加,注解扫描的开启,数据源配置,sqlSessionFactory的设置,Mapper接口的配置,事务管理和注解,SpringMVC的视图解析器与注解配置。此外,还提到了web.xml文件的配置,如加载应用上下文,SpringMVC前端控制器,编码过滤器防止乱码。
摘要由CSDN通过智能技术生成

一:配置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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值