SSM的基本集成配置文件

SSM集成

ssm【Spring+SpringMVC+MyBatis】是市面上使用最多的框架

集成配置文件

1.准备properties文件【略】
2.配置Spring.xml

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

    <!--
        集成SSM
        1.准备properties属性文件
        2.dataSource数据源配置
        3.spring与mybatis的集成配置【绑定SqlSessionFactory】
        4.service的扫描配置
    -->
    <!--扫描service层-->
    <context:component-scan base-package="com.cn.service"/>
    <!--引入properties属性文件-->
    <context:property-placeholder location="classpath:dp.properties"/>
    <!--dataSource数据源-->
    <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>
    <!--集成MyBatis的核心对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--配置数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--配置别名-->
        <property name="typeAliasesPackage" value="com.cn.domain"/>
        <!--配置Mapper映射的扫描-->
        <property name="mapperLocations" value="classpath:com/cn/mapper/*.xml"/>
    </bean>

    <!--一劳永逸的配置Mapper的方案-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--扫描的包的配置-->
        <property name="basePackage" value="com.cn.mapper" />
    </bean>

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

    <!--配置事务支持-->
    <tx:annotation-driven/>
</beans>

3.配置Spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
    <!--配置扫描controller层-->
    <context:component-scan base-package="com.cn.controller"/>

    <!--静态资源放行--支持restful风格-->
    <mvc:default-servlet-handler/>

    <!--Spring-Mvc注解支持-->
    <mvc:annotation-driven/>

    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/system/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <!--上传解析器【略】-->
</beans>

3.配置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_4_0.xsd"
           version="4.0">
    <!--监听Spring-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!--启动SpringMVC的配置-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <!--支持restful风格-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- 解决post请求乱码 -->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>

4.配置后启动服务器常见问题
java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
说找不到你的监听器
解决:
在这里插入图片描述启动服务器就行了,遇到类似的问题也能解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值