SSM集成

SSM整合(掌握)

整合步骤

  1. 创建项目

  2. 导包

  3. 配置文件

    applicationContext.xml 配置spring+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: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
    ">
        <!--扫描service 自动注入到spring  service注解 -->
        <context:component-scan base-package="cn.itsource.ssm.service"></context:component-scan>
        <!-- db.proproperties  dataSource  sqlSessionFactory transaction mapper-->
    
        <!--引入jdbc.properties-->
        <context:property-placeholder location="classpath:db.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>
    
        <!--sqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*Mapper.xml"></property>
            <property name="typeAliasesPackage">
                <value>
                    cn.itsource.ssm.domain
                </value>
            </property>
        </bean>
    
        <!-- 把产生mapper 交给 spring-->
        <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="cn.itsource.ssm.mapper"></property>
        </bean>
    
        <!--事务配置-->
        <!--配置事务管理器-->
        <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
        <!--开启事务注解的支持,默认会去找一个名称叫做transactionManager的事务管理器 -->
        <tx:annotation-driven transaction-manager="transactionManager" />
    
    </beans>
    

    applicationCotnext-mvc.xml 配置springmvc

    <?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/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="cn.itsource.ssm.web.controller" />
        <!--静态资源处理-->
        <mvc:default-servlet-handler />
        <!--识别@RequestMapping等注解支持-->
        <mvc:annotation-driven />
        <!--配置视图解析器-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
    
    </beans>
    

    db.properties --配置数据库

    	jdbc.driverClassName=com.mysql.jdbc.Driver
    	jdbc.url=jdbc:mysql:///mybatis?createDatabaseIfNotExist=true
    	jdbc.username=root
    	jdbc.password=root
    

    web.xml --配置web的内容

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
        <!--
            监听器
    
        -->
        <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>
    
        <!--核心控制器-->
        <servlet>
            <servlet-name>dispatchServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:applicationContext-mvc.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatchServlet</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>
    

    log4j.properties 日志文件

    #log4j.rootLogger=ERROR, stdout
    #(了解)日志等级: OFF level > FATAL(致命) > ERROR(错误) > WARN (警告)>
    #                INFO (提示)> DEBUG (调试)> trace > ALL level(所有配置)
    
    #输出效果 如果你设置日志级别是trace,则大于等于这个级别的日志都会输出
    
    
    # 关闭日志输出
    #log4j.rootLogger=NONE
    #log4j.rootLogger=NONE  ERROR 为严重错误 主要是程序的错误、
    # WARN为一般警告,比如session丢失、
    # INFO为一般要显示的信息,比如登录登出、
    # DEBUG为程序的调试信息
    # TRACE 堆栈信息
    # 扫描包 配置自己包
    
    log4j.rootLogger=ERROR, stdout
    #log4j.rootLogger=NONE
    log4j.logger.cn.itsource=TRACE
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值