温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring如何整合MyBatis

发布时间:2021-08-12 12:49:56 来源:亿速云 阅读:92 作者: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