SSM详细配置文件与每一行的解释 ---初学推荐

spring配置文件

1.加载properties配置文件

2.配置数据源对象DataSource

3.集成mybatis(实际上创建一个sqlSessionFactory对象而已)

​ sqlSessionFactory : 需要 DataSource数据源对象

需要 mybatis的核心配置文件
4.进行mapper包扫描,创建mapper接口实现类对象放入spring容器中

5.spring的普通注解扫描

6.如果要用到事务 需要配置平台事务管理器(实际上就是创建一个transactionManager对象而已)

​ 并且开开启事务的注册驱动

注意:配置平台事务管理器一定要在有数据源的前提下:DataSource

<!--1.为了配置数据源需要加载配置文件-->
<!--2.配置数据源datasource-->
<!--3.spring集成mybadis 实际上就是创建sqlsessionfactory对象 ,把datasource对象和mybatis的核心配置文件给他-->
<!--4.扫描mapper包,创建mapper包下的接口实现类对象并且放入spring容器中-->
<!--5.spring自身的扫描service和mapper层-->
<!--6.如果要用到事务,需要配置平台事务管理器把datasource给他-->
<!--7.我用注解写事务,需要开启事务的注解驱动-->
<?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
">
        <!--1.为了配置数据源需要加载配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--2.配置数据源对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <!--3.spring容器集成mybatis-->
    <!--sqlSessionFactory 他需要一个数据源和核心配置文件-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--相当于配置mybatis的环境-->
        <property name="dataSource" ref="dataSource"/>
        <!--相当于
         InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
    </bean>
    <!--4.配置mapper包扫描 ,创建mapper的实现类对象,放入spring容器中-->
    <!--mapper包底下每一个实现类的对象放到spring容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--相当于-->
        <!--OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);-->
        <property name="basePackage" value="com.itcast.mapper"/>
    </bean>
        <!--5.spring的包扫描-->
    <context:component-scan base-package="com.itcast.service,com.itcast.mapper"/>
</beans>
<!--如果用到事务 需要配置平台事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--并且开始事务的注解驱动-->
    <tx:annotation-driven/>

springmvc配置文件

1.通常用注解写并且需要把对象转换为json类型的字符串

​ 配置mvc的注解驱动

2.扫描controller包下的注解

3.前端控制器会拦截静态资源,所以开启mvc的静态资源的访问权限

另加:

  • 如果页面为jsp的可以配置视图解析器,(bean写IRVRPS 是视图解析器)
  • 文件上传配置多媒体解析器(bean写的CMR是多媒体解析器)
  • 拦截请求,配置拦截器
  • 异常处理类,无需配置,仅需springmvc扫描的时候带上它.
<!--1.开启mvc的注解驱动,当我返回对象时,底层自动将它变为对应的json字符串-->
<!--2.controller的包扫描,识别controller注解-->
<!--3.开启mvc的静态资源访问权限,以防静态资源请求被前端控制器拦截-->
<?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
">

     <!--1.开启mvc的注解驱动 , 底层有解决json的问题,将你返回的对象,转换为json格式的字符串-->
    <!--默认底层就会集成jackson进行对象或集合的json格式字符串的转换-->
    <mvc:annotation-driven/>
    
    <!--2.先扫描controller包再说 识别出注解-->
    <context:component-scan base-package="com.itcast.controller"/>
   
    <!--当有静态资源需要加载时,比如jquery文件,通过谷歌开发者工具抓包发现,
    没有加载到jquery文件,原因是SpringMVC的前端控制器DispatcherServlet的url-pattern配置的是/,
    代表对所有的资源都进行过滤操作,需要放行静态资源-->
    <!--3.开启静态资源访问权限-->
    <mvc:default-servlet-handler/>
<!-------------------------------------------->
    <!--4.如果页面是jsp的那么需要配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--5.文件上传的多媒体解析器-->
    <!--配置文件上传解析器-->
    <!--此id不可改-->
    <bean id="multipartResolver"  class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="defaultEncoding" value="UTF-8"/>
        <!--所有文件的总大小-->
        <property name="maxUploadSize" value="500000"/>
        <!--每个文件的大小-->
        <property name="maxUploadSizePerFile" value="50000"/>
    </bean>
    <!--6.配置拦截器-->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--对哪些资源执行拦截操作-->
            <mvc:mapping path="/**"/>
            <!--你得告诉springmvc你写的拦截类是哪一个-->
            <bean class="拦截类的全路径"/>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

web.xml的配置文件

1.用监听器加载spring容器
2.只要用springmvc就得配置前端控制器
3.解决全局乱码问题

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

<welcome-file-list>
    <welcome-file>customer.html</welcome-file>
</welcome-file-list>

    <!--利用监听器创建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>

    <!--用到springmvc就要配置前端控制器-->
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--解决乱码的问题-->
    <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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值