您好,登錄后才能下訂單哦!
本篇文章給大家分享的是有關(guān)Spring中怎么整合MyBatis,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
1.導(dǎo)入所需要的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實(shí)體類(lèi)
3.service層接口
4.serviceimpl實(shí)現(xiàn)類(lèi)
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"/>--> <!-- 配置數(shù)據(jù)源 spring內(nèi)置的數(shù)據(jù)源--> <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"/> <!--注冊(cè)DAO層:mapper的代理對(duì)象--> <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層對(duì)象--> <bean id="personService" class="com.spring.service.PersonServiceImpl"> <property name="dao" ref="personDao"></property> </bean> <!-- sqlSessionFactory 創(chuàng)建SqlSession對(duì)象的工廠 --> <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 事務(wù)管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 事務(wù)通知--> <tx:advice transaction-manager="transactionManager" id="txAdvice"> <tx:attributes> <!--get系列方法設(shè)置事務(wù)的隔離級(jí)別和傳播行為--> <tx:method name="get*" isolation="READ_COMMITTED" propagation="REQUIRED"/> </tx:attributes> </tx:advice> </beans>
8.測(cè)試類(lèi)
結(jié)果:
以上就是Spring中怎么整合MyBatis,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。