(三) ssm -- spring 整合mybatis

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- 分页插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 该参数默认为false 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用和startPage中的pageNum效果一样 -->
            <property name="offsetAsPageNum" value="true"/>
            <!-- 该参数默认为false设置为true是,使用RowBounds分页会进行count查询 -->
            <property name="rowBoundsWithCount" value="true"/>
            <!-- 设置为true时,如果pageSize=0或者ROwRounds.limit=0就会查询出全部的结果(相当于每一偶执行分页查询,但是返回结果仍然是page类型) -->
            <property name="pageSizeZero" value="true"/>
        </plugin>
    </plugins>

</configuration>
mybatis-config.xml

创建好 mybatis-config.xml文件后 需要创建spring-context.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"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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.0.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd  
            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd  
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx.xsd 
            http://www.springframework.org/schema/aop 
            http://www.springframework.org/schema/aop/spring-aop.xsd"> 
            
     <!-- 扫描service层 -->    
    <context:component-scan base-package="com.taotao.service"></context:component-scan>
       <!-- 扫描jdbc.properties配置文件  -->
      <context:property-placeholder location="classpath:jdbc.properties"/>
     <!-- 配置c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${driver}" />
        <property name="jdbcUrl" value="${url}" />
        <property name="user" value="${username}" />
        <property name="password" value="${password}" />
    </bean>
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
        <property name="typeAliasesPackage" value="com.taotao.pojo"></property>
        <property name="mapperLocations" value="classpath:com/taotao/dao/*Mapper.xml"></property>
    </bean>
    <!-- 扫描生成所有dao层 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.taotao.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 使用注解来控制事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    
    
</beans>  
spring-context.xml

直接把mybatis配置写在spring 配置文件中  引用了jdbc.properties文件

需要注意扫描 mapper.xml文件位置

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/taotao?serverTimezone=UTC
username=root
password=root
jdbc.properties

在web.xml配置spring

<!-- 配置适配器spring -->
  <listener>
    <description>启动spring容器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-context.xml</param-value>
  </context-param> 
web.cml

如以上配置完成 启动项目看是否报错  成功 / 创建测试类 测试数据访问层是否成功

整体成功 框架搭好了

 

注意项目使用 mybatis的反向工程 下载地址

链接: https://pan.baidu.com/s/1db4FlPFAWZpNIyAl2BBx4A 密码: jqa9

posted @ 2018-05-26 17:56  ssSir  阅读(25)  评论(0)    收藏  举报