spring與mybatis的整合

整合須要的jar包

要實現spring與mybatis的整合,就要首先導入相關的依賴jar包,以下:java

    •   Spring的jar包
    •   Mybatis的jar包
    •   Spring與mybatis整合的jar包
    •   Mysql數據庫驅動的jar包
    •   Druid數據庫鏈接池的jar包
    •   Spring與Junit集成測試的jar包(spring-test、Junit)
    •   日誌的jar包

        具體依賴mysql

      複製代碼
      <properties>
            <spring-version>4.2.4.RELEASE</spring-version>
        </properties>
        
        <dependencies>
            <!-- spring 核心 -->
            <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>${spring-version}</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-core</artifactId>
              <version>${spring-version}</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-beans</artifactId>
              <version>${spring-version}</version>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-expression</artifactId>
              <version>${spring-version}</version>
          </dependency>
          
          <!-- spring web集成 -->
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-web</artifactId>
              <version>${spring-version}</version>
          </dependency>
          <dependency>
              <groupId>javax.servlet</groupId>
              <artifactId>javax.servlet-api</artifactId>
              <version>3.1.0</version>
              <scope>provided</scope>
          </dependency>
          
          <!-- spring aop的jar包 -->
          <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-aop</artifactId>
              <version>${spring-version}</version>
          </dependency>
          
          <dependency>
              <groupId>aopalliance</groupId>
              <artifactId>aopalliance</artifactId>
              <version>1.0</version>
          </dependency>
            
            <dependency>
              <groupId>org.aspectj</groupId>
              <artifactId>aspectjweaver</artifactId>
              <version>1.6.8</version>
          </dependency>
            
           <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-aspects</artifactId>
              <version>${spring-version}</version>
          </dependency>
            
            <!-- spring jdbc jar包 -->
                <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-jdbc</artifactId>
              <version>${spring-version}</version>
          </dependency>
            
            <!-- mysql驅動 -->
          <dependency>
              <groupId>mysql</groupId>
              <artifactId>mysql-connector-java</artifactId>
              <version>5.1.38</version>
          </dependency>
          <!-- 事務 -->
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-tx</artifactId>
              <version>${spring-version}</version>
          </dependency>
          
          <!-- druid數據庫鏈接池 -->
          <dependency>
              <groupId>com.alibaba</groupId>
              <artifactId>druid</artifactId>
              <version>1.0.18</version>
          </dependency>
            
            <!-- mybaties的依賴包 -->
            <dependency>
              <groupId>org.mybatis</groupId>
              <artifactId>mybatis</artifactId>
              <version>3.2.7</version>
          </dependency>
          <dependency>
              <groupId>org.mybatis</groupId>
              <artifactId>mybatis-spring</artifactId>
              <version>1.2.2</version>
          </dependency>
          <!-- 日誌相關的jar包 -->
          <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-api</artifactId>
              <version>1.7.13</version>
          </dependency>
          <dependency>
              <groupId>org.slf4j</groupId>
              <artifactId>slf4j-log4j12</artifactId>
              <version>1.7.5</version>
          </dependency>
        
          <dependency>
              <groupId>log4j</groupId>
              <artifactId>log4j</artifactId>
              <version>1.2.16</version>
          </dependency>
          
          <!-- spring與Junit測試的jar包 -->
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-test</artifactId>
              <version>${spring-version}</version>
              <scope>test</scope>
          </dependency>
          <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.9</version>
              <scope>test</scope>
          </dependency>
                
        </dependencies>
        
        <build>  
          <plugins>  
              <plugin>  
                  <groupId>org.apache.maven.plugins</groupId>  
                  <artifactId>maven-compiler-plugin</artifactId>  
                  <configuration>  
                      <source>1.7</source>
                      <target>1.7</target>  
                  </configuration>  
              </plugin>  
              <plugin>
                      <groupId>org.apache.tomcat.maven</groupId>
                      <artifactId>tomcat7-maven-plugin</artifactId>
                      <version>2.2</version>
                      <configuration>
                          <!-- 指定端口 -->
                          <port>8080</port>
                          <!-- 請求路徑 -->
                          <path>/</path>
                      </configuration>
                  </plugin>
              
          </plugins>  
      </build>

      整合的步驟

       

        第一步:建立maven的web工程web

        第二步:導入整合須要的jar包spring

       

        第三步:mybatis的核心配置文件:sqlMapConfig.xmlsql

      複製代碼
      <?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>
           <!-- 定義別名 -->
           <typeAliases>
               <package name="cn.itcast.spring.domain"/>
           </typeAliases>
           <!-- 加載映射文件 -->    
           <mappers>
               <mapper resource="mapper.xml"/>
           </mappers>
       </configuration>    
      複製代碼

       

        第四步:spring的核心配置文件:applicationContext.xml的配置數據庫

        •     加載db.properties屬性配置文件
        •     配置Druid數據庫鏈接池
        •     將SqlSessionFactory的單例對象,配置到spring容器中
        •     將mapper代理對象或者dao接口的實現類對象,配置到spring容器中
      複製代碼
      <?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-4.2.xsd
              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
              http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">            
          <context:component-scan base-package="cn.spring"></context:component-scan>
          
          <!-- 加載db.properties屬性文件 -->
          <context:property-placeholder location="classpath:db.properties"/>
          
          <!-- 數據庫鏈接池 -->
          <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
              <property name="driverClassName" value="${jdbc.driverClassName}"></property>
              <property name="url" value="${jdbc.url}"></property>
              <property name="username" value="${jdbc.username}"></property>
              <property name="password" value="${jdbc.password}"></property>
          </bean>
          
          <!-- SQLSessionFactory的配置 -->
          <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
              <property name="dataSource" ref="dataSource"></property>        
              <!-- mybatis的核心配置文件的路徑 -->
              <property name="configLocation" value="classpath:sqlMapConfig.xml"></property>
          </bean>       
      </beans>
      複製代碼

       

            至於將mapper代理對象或者dao接口的實現類對象,配置到spring容器中,將在下面進行配置express

       

        第五步:編寫dao接口、dao實現類或者mapper接口、mapper映射文件apache

       傳統Dao開發方式

        開發方式:dao接口+dao實現類。api

        Dao的實現類須要從spring容器中獲取sqlSession對象來操做數據庫,spring爲咱們提供了SqlsessionDaoSupport類,咱們只須要繼承這個類,就能夠順利的得到sqlSession對象。tomcat

      總結一句話:dao實現類須要繼承SqlsessionDaoSupport類,而後再spring容器中配置這個bean。

      1 public interface AccountDao {
      2     public List<Account> findAll();
      3 }
      複製代碼
       1 public class AccountDaoImpl extends SqlSessionDaoSupport implements AccountDao {//須要繼承SqlSessionDaoSupport 
       2     @Override
       3     public List<Account> findAll() {
       4         SqlSession sqlSession = getSqlSession();    //繼承SqlSessionDaoSupport 後直接調用getSqlSession方法
       5         List<Account> selectList = sqlSession.selectList("test.abc");
       6         //整合後sqlSession不須要關閉
       7         return selectList;
       8     }
       9 
      10 }
      複製代碼

            mapper.xml

      複製代碼
      <?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="test">
          <select id="abc" resultType="account">
              select * from account
          </select>
      </mapper>
      複製代碼

            applicationContext.xml中配置dao

         <!-- dao的實現類放到容器中(spring與mybatis整合傳統dao的開發) -->
          <bean id="accountDao" class="cn.itcast.spring.dao.AccountDaoImpl">
              <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
          </bean>

       

            基於mapper代理形式開發Dao

                開發mapper接口及mapper映射文件

      1 public interface AccountMapper {
      2     public List<Account> selectAll();
      3 }
      複製代碼
      <?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代理的開發規則:
          1.namespace必須爲接口的全路徑
          2.id必須和接口的方法名稱一致
          3.parameterType必須與接口方法的參數類型一致
          4.resultType必須與接口方法的返回值類型一致
      -->
      <mapper namespace="cn.itcast.spring.mapper.AccountMapper"> <select id="selectAll" resultType="account"> select * from account </select> </mapper>
      複製代碼

       

                applicationContext.xml中配置mapper代理

         <!-- 包掃描器 -->
          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <!-- 掃描包,mapper接口和映射對象的名稱必須保持一致 -->
              <property name="basePackage" value="cn.spring.mapper"></property>
          </bean>

       

       

       

        第六步:編寫Junit測試代碼測試

            傳統dao方式開發測試

      複製代碼
       1 @RunWith(SpringJUnit4ClassRunner.class)
       2 @ContextConfiguration("classpath:applicationContext.xml")
       3 public class SpringMybatisTest {
       4     @Autowired
       5     private AccountDao accountDao;
       6     
       7     @Test
       8     public void testName() throws Exception {
       9         List<Account> findAll = accountDao.findAll();        
      10         for (Account account : findAll) {
      11             System.out.println(account);
      12         }
      13     }
      14 }
      複製代碼

       

       

            基於mapper代理方式測試

      複製代碼
       1 @RunWith(SpringJUnit4ClassRunner.class)
       2 @ContextConfiguration("classpath:applicationContext.xml")
       3 public class SpringMybatisTest {
       4     @Autowired
       5     private AccountMapper accountMapper;
       6     
       7     @Test
       8     public void testName() throws Exception {
       9         List<Account> list = accountMapper.selectAll();        
      10         for (Account account : list) {
      11             System.out.println(account);
      12         }
      13     }
      14 }
      複製代碼
相關文章
相關標籤/搜索