1. 程式人生 > 程式設計 >Spring整合MyBatis圖示過程解析

Spring整合MyBatis圖示過程解析

這篇文章主要介紹了Spring整合MyBatis圖示過程解析,文中通過示例程式碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

1.匯入所需要的jar依賴

!--MyBatis和Spring的整合包 由MyBatis提供-->
<dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis-spring</artifactId>
 <version>1.3.0</version>
</dependency>
<!--MyBatis的核心jar檔案-->
<dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis</artifactId>
 <version>3.4.1</version>
</dependency>

2.MyBatis和Spring整合(XML版)

1.dao介面

2.entity實體類

3.service層介面

4.serviceimpl實現類

5.PersonDao.xml配置

6.jdbc.properties配置

7.大配置檔案

<?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: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.xsd

    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  <!--<context:component-scan base-package="com.spring"/>-->
  <!-- 配置資料來源 spring內建的資料來源-->
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
  </bean>
  <!-- 引入屬性檔案 -->
  <context:property-placeholder location="jdbc.properties.properties"/>
  <!--註冊DAO層:mapper的代理物件-->
  <bean id="personDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="com.spring.dao.PersonDao"></property>
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
  </bean>
  <!--配置service層物件-->
  <bean id="personService" class="com.spring.service.PersonServiceImpl">
    <property name="dao" ref="personDao"></property>
  </bean>
  <!-- sqlSessionFactory 建立SqlSession物件的工廠 -->
  <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <!-- Mybatis的大配置檔案 -->
    <property name="typeAliasesPackage" value="com.spring.entity"></property>
    <!-- 掃描sql配置檔案:mapper需要的xml檔案 -->
    <property name="mapperLocations" value="classpath*:mapper/*.xml"/>
  </bean>
  <!-- MapperScannerConfigurer 掃描mapper檔案掃描器 -->
  <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.spring.dao"></property>
  </bean>

  <!-- transactionManager 事務管理器-->
  <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"></property>
  </bean>
  <!-- 事務通知-->
  <tx:advice transaction-manager="transactionManager" id="txAdvice">
    <tx:attributes>
      <!--get系列方法設定事務的隔離級別和傳播行為-->
      <tx:method name="get*" isolation="READ_COMMITTED" propagation="REQUIRED"/>
    </tx:attributes>
  </tx:advice>
</beans>

8.測試類

結果:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。