溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring如何整合MyBatis

發布時間:2021-08-12 12:49:56 來源:億速云 閱讀:106 作者:chen 欄目:編程語言

本篇內容主要講解“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>

到此,相信大家對“Spring如何整合MyBatis”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女