zoukankan      html  css  js  c++  java
  • spring整合mybatis

    spring整合mybatis

    1.spring整合mybatis,需要用到一些依赖包,比如

        <!-- mybatis核心包 -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>${mybatis.version}</version>
        </dependency>
        <!-- mybatis/spring包 -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.2.2</version>
        </dependency>

    还有其他的一些,比如下面的,当然没有全部列出来。

        <!-- 导入java ee jar 包 -->
        <dependency>
          <groupId>javax</groupId>
          <artifactId>javaee-api</artifactId>
          <version>7.0</version>
        </dependency>
        <!-- 导入Mysql数据库链接jar包 -->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.30</version>
        </dependency>
        <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
        <dependency>
          <groupId>commons-dbcp</groupId>
          <artifactId>commons-dbcp</artifactId>
          <version>1.2.2</version>
        </dependency>
        <dependency>
          <groupId>com.github.pagehelper</groupId>
          <artifactId>pagehelper</artifactId>
          <version>4.1.4</version>
        </dependency>

    2.mybatis-config.xml配置文件中,只有一个分页插件配置。以前我们采用一般的方法时,数据库配置都是放在这个xml文件下的,现在我们要整合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>
            <!-- com.github.pagehelper为PageHelper类所在包名 -->
            <plugin interceptor="com.github.pagehelper.PageHelper">
                <!-- 4.0.0以后版本可以不设置该参数 -->
                <property name="helperDialect" value="mysql"/>
                <!-- 该参数默认为false -->
                <!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
                <!-- 和startPage中的pageNum效果一样-->
                <property name="offsetAsPageNum" value="true"/>
                <!-- 该参数默认为false -->
                <!-- 设置为true时,使用RowBounds分页会进行count查询 -->
                <property name="rowBoundsWithCount" value="true"/>
                <!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
                <!-- (相当于没有执行分页查询,但是返回结果仍然是Page类型)-->
                <property name="pageSizeZero" value="true"/>
                <!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
                <!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
                <!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
                <property name="reasonable" value="true"/>
                <!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
                <!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
                <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置映射的用默认值 -->
                <!-- 不理解该含义的前提下,不要随便复制该配置 -->
                <property name="params" value="pageNum=start;pageSize=limit;"/>
                <!-- 支持通过Mapper接口参数来传递分页参数 -->
                <property name="supportMethodsArguments" value="true"/>
                <!-- always总是返回PageInfo类型,check检查返回类型是否为PageInfo,none返回Page -->
                <property name="returnPageInfo" value="check"/>
            </plugin>
        </plugins>
    </configuration>

    3.jdbc.properties配置文件。事实上我整合的时候没有用到这个文件,我直接在spring-mybatis.xml文件里面写了数据库连接信息,但是因为里面演示了引用这个文件,所以还是把这个文件写一下。

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/test
    username=root
    password=root
    #定义初始连接数
    initialSize=0
    #定义最大连接数
    maxActive=20
    #定义最大空闲
    maxIdle=20
    #定义最小空闲
    minIdle=1
    #定义最长等待时间
    maxWait=60000

    4.我还有个mapper文件,写数据库语句的。让它也亮一下相吧,要不连没用到的jdbc.properties配置文件都亮相了,它说我没让它亮相,它该多伤心是不......

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.cn.hnust.dao.IUserDao">
        <select id="selectByPrimaryKey" resultType="com.cn.hnust.pojo.User">
            select * from user_t where id = #{userId}
        </select>
    
        <select id="selectAll" resultType="com.cn.hnust.pojo.User">
            select * from user_t
        </select>
    </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:p="http://www.springframework.org/schema/p"
           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-3.1.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
    
    
    </beans>

    第二步:

      开始添加内容了。第一个出场的是它:

        <!-- 自动扫描 -->
        <context:component-scan base-package="com.cn.hnust" />

      使用了这个标签后,spring可以自动去扫描base-pack下面或者子包下面的java文件,如果扫描到有@Component @Controller@Service等这些注解的类,则把这些类注册为bean。

    更详细的可以参考这里

      继续往下,接下来谁出场呢,是它

        <!-- 引入配置文件 -->
        <bean id="propertyConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:jdbc.properties" />
        </bean>

      这是引入配置文件,如果下面的数据库用了jdbc.properties文件,就可以用它引入了。

      也可以这种方式引入,效果一样,不过更加简洁。哈哈哈!更多的可以参考这里......

         <!-- 引入jdbc配置文件 -->
         <context:property-placeholder location="jdbc.properties"/>

      终于spring常挂在嘴边的bean要出来了,千呼万唤始出来啊!那个激动啊!啊!啊! 啊! 

            <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  destroy-method="close">
            <property name="driverClassName" value="${driver}" />
            <property name="url" value="${url}" />
            <property name="username" value="${username}" />
            <property name="password" value="${password}" />
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="${initialSize}"></property>
            <!-- 连接池最大数量 -->
            <property name="maxActive" value="${maxActive}"></property>
            <!-- 连接池最大空闲 -->
            <property name="maxIdle" value="${maxIdle}"></property>
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="${minIdle}"></property>
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="${maxWait}"></property>
    
        </bean>

      这个是数据源的配置,不过没用到上面引用的jdbc.properties的配置文件。如果用它,可以这样

              <property name="url" value="${url}" />
              <property name="username" value="${username}" />
              <property name="password" value="${password}" />

      这样就可以了,是不是很简单?哈哈哈!

      重点来了!这次说一遍就好了!!!spring和MyBatis要开始完美整合了。期待ing......

        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- 自动扫描mapping.xml文件 -->
            <property name="mapperLocations" value="classpath:UserMapper.xml"></property>
            <property name="configLocation" value="classpath:mybatis-config.xml" />
        </bean>
    
        <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.cn.hnust.dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        </bean>

      MapperScannerConfigurer , 它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean。

      basePackage 属性是让你为映射器接口文件设置基本的包路径。 你可以使用分号或逗号 作为分隔符设置多于一个的包路径。每个映射器将会在指定的包路径中递归地被搜索到。

      没有必要去指定SqlSessionFactory 或 SqlSessionTemplate , 因为 MapperScannerConfigurer 将会创建MapperFactoryBean,之后自动装配。但是,如果你使 用了一个 以上的 DataSource,那 么自动装配可能会失效 。这种情况下 ,你可以使用 sqlSessionFactoryBeanName 或 sqlSessionTemplateBeanName 属性来设置正确的 bean 名称来使用。

      如果你在mybatis-config.xml中引用mapper.xml

    <configuration>
      <typeAliases>
         <typeAlias type="com.cn.hnust.pojo.User" alias="User" />
      </typeAliases>
      <mappers>
         <mapper resource="userMapper.xml" />
      </mappers>
    </configuration>

      那 <property name="mapperLocations" value="classpath:UserMapper.xml"></property>  那这一行就可以去掉了。

      其他方式参考这里这里

      还有个事务管理,也放上来吧!

      

        <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>

      最后放上结构图和整个spring-mybatis.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: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-3.1.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.1.xsd
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    
        <!-- 自动扫描 -->
        <context:component-scan base-package="com.cn.hnust" />
        <!-- 引入配置文件 -->
        <bean id="propertyConfigurer"
              class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="location" value="classpath:jdbc.properties" />
        </bean>
    
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close">
            <property name="driverClassName" value="${driver}" />
            <property name="url" value="${url}" />
            <property name="username" value="${username}" />
            <property name="password" value="${password}" />
            <!-- 初始化连接大小 -->
            <property name="initialSize" value="${initialSize}"></property>
            <!-- 连接池最大数量 -->
            <property name="maxActive" value="${maxActive}"></property>
            <!-- 连接池最大空闲 -->
            <property name="maxIdle" value="${maxIdle}"></property>
            <!-- 连接池最小空闲 -->
            <property name="minIdle" value="${minIdle}"></property>
            <!-- 获取连接最大等待时间 -->
            <property name="maxWait" value="${maxWait}"></property>
    
        </bean>
    
        <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <!-- 自动扫描mapping.xml文件 -->
            <property name="mapperLocations" value="classpath:UserMapper.xml"></property>
            <property name="configLocation" value="classpath:mybatis-config.xml" />
        </bean>
    
        <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.cn.hnust.dao" />
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        </bean>
    
        <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
        <bean id="transactionManager"
              class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />
        </bean>
    
    </beans>

      好了,完事了,撤了!

      哈哈哈!

          哈哈哈!

              哈哈哈!

  • 相关阅读:
    android: LayoutInflater使用
    android:ListView bbs Demo
    android:制作 Nine-Patch 图片
    android:单位和尺寸
    android:提升 ListView 的运行效率
    android:定制 ListView 的界面
    android:ListView 的简单用法
    android:创建自定义控件
    android:四种基本布局
    android:ProgressDialog控件
  • 原文地址:https://www.cnblogs.com/heqiyoujing/p/9492288.html
Copyright © 2011-2022 走看看