美文网首页
Spring-SpringMvc-mybatis整合_笔记

Spring-SpringMvc-mybatis整合_笔记

作者: bboymonk | 来源:发表于2017-04-28 11:10 被阅读0次

    applicationContext-mybatis.xml

    Mybatis操作数据库的核心类是sqlSession,与Spring整合之后用的是
    sqlSessionTemplate这个类。
    不过还是以下面的mapper接口为主。
    <?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-3.2.xsd
    
    
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    
    <!-- 读取jdbc.properties文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- dbcp连接池    JNDI获取数据源,非常消耗性能,所以设置成单例,destroy时要关闭数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
        <!-- 这里的name要写成BasicDataSource里的名字 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/springmvcdb"/>
        <property name="username" value="root"/>
        <property name="password" value="wjb"/>
    </bean>
    
    <tx:annotation-driven/>
    <!-- 事务管理 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 配置mybatis的SqlSessionFactory,通过它才能构造SqlSession -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 引入mybatis配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!-- 引入所有的mapper.xml文件 -->
        <!-- <property name="mapperLocations" value="classpath:dao/*.xml"/> -->
    </bean>
    
    <!--
        这个配置方式只能让单个的mapper接口生成实例
     <bean id="employeeMapper"  class="org.mybatis.spring.mapper.MapperFactoryBean">
    //这里其实就相当于mybatis里的sqlSession.getMapper()一样。
        <property name="mapperInterface" value="dao.EmployeeMapper"/>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
    </bean> -->
    
    <!-- 这个配置方式能让dao包下的所有mapper接口生成实例 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="dao"/>
    </bean>
    </beans>
    

    spring-servlet.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:p="http://www.springframework.org/schema/p"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    
    
            <!-- 默认是beanNameUrlHandlerMapping -->
            <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"></bean> -->
            <!-- 第一种方法
                根据javaBean的名字来处理映射
            <bean name="/wjb" class="controller.WelcomeController"></bean> -->
    
    
           <!-- 第二种方法,基于注解的,扫描controller包下的所有类,让注解生效 -->
           <context:component-scan base-package="controller"/>
           <mvc:annotation-driven/>
    
            <!-- 内部视图解析器 -->
            <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/jsp/"></property>
                <property name="suffix" value=".jsp"></property>
            </bean>
    
    
            <!-- 全局异常配置  SimpleMappingExceptionResolver -->
            <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
                <property name="exceptionMappings">
                    <props>
                        <prop key="pojo.UserException">error</prop>   <!-- 这里可以配置多个异常信息,error是逻辑视图名 -->
                    </props>
                </property>
            </bean>
    
            <!-- SpringMvc访问静态资源文件,必须配置URL,否则无法映射成功。
                        两个*是包括statics文件夹下的子文件夹。 -->
            <mvc:resources location="/statics/" mapping="/statics/**"/>
    
            <!-- SpringMvc实现单文件上传,先要配置。如果是多文件上传的话,多个文件加起来不能超过100000 -->
            <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
                <property name="maxUploadSize" value="100000"/>
            </bean>   
    </beans>
    

    jdbc.properties

    driverClassName=com.mysql.jdbc.Driver
    url=jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=UTF-8
    username=root
    password=wjb
    

    log4j.properties

    log4j.rootLogger=DEBUG,CONSOLE,file
    #log4j.rootLogger=ERROR,ROLLING_FILE
    log4j.logger.cn.jbit.dao=debug
    log4j.logger.com.ibatis=debug 
    log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug 
    log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug 
    log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug 
    log4j.logger.java.sql.Connection=debug 
    log4j.logger.java.sql.Statement=debug 
    log4j.logger.java.sql.PreparedStatement=debug 
    log4j.logger.java.sql.ResultSet=debug 
    log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug
    ######################################################################################
    # Console Appender  \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
    ######################################################################################
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.Threshold=error
    log4j.appender.CONSOLE.Target=System.out
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n
    ######################################################################################
    # DailyRolling File  \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
    ######################################################################################
    log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
    log4j.appender.file.DatePattern=yyyy-MM-dd
    log4j.appender.file.File=log.log
    log4j.appender.file.Append=true
    log4j.appender.file.Threshold=error
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
    log4j.logger.com.opensymphony.xwork2=error  
    

    web.xml

    <?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">
      <display-name>SMVCMybatis</display-name>
      
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-*.xml</param-value>
      </context-param>
      
      <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      
      <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet.xml</param-value>
        </init-param>
      </servlet>
      <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
        <servlet>
            <servlet-name>spring</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:spring-servlet.xml</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <servlet-name>spring</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
      
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      
      <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
      </context-param>
      
      <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
      </listener>
      
      
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>
    

    spring的注解:

    <context:property-placeholder location="classpath:/com/jdbc.properties"/>的目的是加载资源文件,通过注解@importResource("classpath:/com/jdbc.properties")也能实现。

    相关文章

      网友评论

          本文标题:Spring-SpringMvc-mybatis整合_笔记

          本文链接:https://www.haomeiwen.com/subject/mzsozttx.html