步骤2:mybati配置

文档配置的目录结构图

          

 

 
  1. 在web.xml文件中配置contextConfiglocation的application-context.xml文件的classpath,配置自动context装配监听 ContextLoaderListener
    • ContextLoaderListener :监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息
<?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/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    id="WebApp_ID" version="2.4">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:application-context.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
2.引用spring通用的bean容器命名空间
<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
   
 
</beans>
3.配置application-context.xml 文件,主要根据resource文件的路径目录,【需要 第二步中的命名空间】
     
    <import resource="config/*.xml" />
 
3.在classpath:config/目录下配置spring的bean的扫描标签annotation.xml,开启自动扫面配置和扫描包配置,【需要 第二步中的命名空间】
 
<!-- spring扫包,在基础包除controller外的所有bean -->
       <context:annotation-config />
       <context:component-scan base-package="com.mybatis">
              <context:exclude-filter type="annotation"
                     expression="org.springframework.stereotype.Controller" />
       </context:component-scan>
 
       <!-- 开启自动扫面配置 -->
       <context:annotation-config />
4.在classpath:config/目录下配置读取property的标签,property.xml,【需要 第二步中的命名空间】
 
       <!-- 读取jdbc.properties文件,该文件主要配置数据库的连接串 -->
       <bean
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="locations">
                     <list>
                           <value>
                                  classpath:properties/jdbc.properties
                           </value>
                     </list>
              </property>
       </bean>

 

5.在classpath:config/目录下配置c3p0标签,jdbc.xml,,【需要 第二步中的命名空间】
 
     <!-- c3p0-->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
              <property name="driverClass" value="${driverClass}"/>
              <property name="jdbcUrl" value="${jdbcUrl}"></property>
              <property name="user" value="${user}" />
              <property name="password" value="${password}"/>
       </bean>
View Code

 

6.在classpath:config/目录下配置mybatis标签,mybatis.xml
 
<!-- 创建Mybatis SessionFactory 会话工厂, -->
       <bean class="org.mybatis.spring.SqlSessionFactoryBean">
              <!-- 联结jdbc。xml中的c3p0的dataSource 数据源 -->
              <property name="dataSource" ref="dataSource"></property>
              <!-- 配置数据表bean对象和查询对象的包路径 -->
              <property name="typeAliasesPackage" value="com.mybatis.core.bean,com.mybatis.core.query"></property>
       </bean>
       <!-- 配置映射MapperScannerConfigurer对象的扫描路径 -->
       <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <property name="basePackage" value="com.mybatis.core.dao"></property>
       </bean>
7.在classpath:properties/目录下配置数据库信息,jdbc.properties,,【需要 第二步中的命名空间】
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://192.168.85.128:3306/bbs_ecom?characterEncoding=UTF-8
user=root
password=root
8.在classpath:config/mybatis.xml 里配置mybatis的基本设置(开启缓存、是否启用延迟加载)
 
 
 
posted @ 2017-08-15 15:09  担禾滩  阅读(222)  评论(0编辑  收藏  举报