Mybatis學習--spring和Mybatis整合,mybatis--spring

來源:互聯網
上載者:User

Mybatis學習--spring和Mybatis整合,mybatis--spring

  • 簡介

  在前面寫測試代碼的時候,不管是基於原始dao還是Mapper介面開發都有許多的重複代碼,將spring和mybatis整合可以減少這個重複代碼,通過spring的模板方法模式,將這些重複的代碼進行封裝,如:擷取SqlSessionFactory、SqlSession、SqlSession的關閉等,我們只需要實現具體的業務處理。另外,spring還利用其IOC將Dao或者Mapper介面的放入到容器中進行管理,更好的實現瞭解耦。

  • Spring和MyBatis整合

  1、整合思路:

  需要spring通過單例方式管理SqlSessionFactory。spring和mybatis整合組建代理程式對象,使用SqlSessionFactory建立SqlSession。(spring和mybatis整合自動完成)持久層的mapper都需要由spring進行管理。

  2、整合環境

 

  建立一個新的java工程,將Mybatis和Spring整合的jar包匯入到工程中。

  3、sqlSessionFactory

 

  在applicationContext.xml配置sqlSessionFactory和資料來源:

 

 1 <!-- 載入設定檔 --> 2     <context:property-placeholder location="classpath:db.properties" /> 3  4     <!-- 資料來源,使用dbcp --> 5     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 6         destroy-method="close"> 7         <property name="driverClassName" value="${jdbc.driver}" /> 8         <property name="url" value="${jdbc.url}" /> 9         <property name="username" value="${jdbc.username}" />10         <property name="password" value="${jdbc.password}" />11         <property name="maxActive" value="10" />12         <property name="maxIdle" value="5" />13     </bean>14 15 16     <!-- sqlSessinFactory -->17     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">18         <!-- 載入mybatis的設定檔 -->19         <property name="configLocation" value="mybatis/SqlMapConfig.xml" />20         <!-- 資料來源 -->21         <property name="dataSource" ref="dataSource" />22 </bean>

 

  • 原始Dao開發

  1、其他都基本相同,就是Dao的實作類別有所變化:

1 public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao{2     @Override3     public User findUserById(int id) throws Exception {4         SqlSession sqlSession = this.getSqlSession();//擷取sqlSession5         User user = sqlSession.selectOne("user.findUserById", id);6         return user;7     }8 }

  實作類別繼承SqlSessionDaoSupport,這樣spring就通過模板方法將 擷取sqlSessionFactory、擷取sqlSession、交易管理、關閉sqlSession進行了封裝,我們只需要負責商務邏輯即可。

  2、配置dao

  在applicationContext.xml中配置dao。

 

1 <!-- 配置userDaoImpl -->2     <bean id="userDao" class="com.luchao.mybatis.first.daoimpl.UserDaoImpl">3         <property name="sqlSessionFactory" ref="sqlSessionFactory" />4     </bean>

 

  3、測試程式

 1 public class MyBatis_mapper_test { 2     private ApplicationContext applicationContext; 3     @Before 4     public void init() throws IOException { 5         this.applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); 6     } 7     @Test 8     public void testFindUserById() throws Exception { 9         // 建立UserMapper對象10         UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");11         // 調用userMapper的方法12         User user = userMapper.findUserById(10);13         // 列印客戶資訊14         System.out.println(user);15     }16 }
  • Mapper代理開發

  1、在Spring中配置Mapper介面

  (1)、使用org.mybatis.spring.mapper.MapperFactoryBean,根據mapper介面組建代理程式對象

1 <bean id="" class="org.mybatis.spring.mapper.MapperFactoryBean">2     <property name="mapperInterface" value="mapper介面地址"/>3   <property name="sqlSessionFactory" ref="sqlSessionFactory"/>4 </bean>

  需要針對每個mapper進行配置,比較麻煩。

  (2)、通過MapperScannerConfigurer進行mapper掃描

 

1 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">2  <property name="basePackage" value="mapper介面包地址"></property>3 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>4 </bean>

 

  basePackage:掃描包路徑,中間可以用逗號或分號分隔定義多個包。

  這種方式mapper.xml的檔案名稱和mapper的介面名稱保持一致,且放在同一個目錄。如果將mapper.xml和mapper介面的名稱保持一致且放在一個目錄 則不用在sqlMapConfig.xml中進行配置。

  可以看出將Mybatis整合到Spring中,可以減少模板代碼的書寫,並且將Dao和Mapper通過配置放入到Spring容器中,可以實現代碼解耦。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.