MyBatis中怎么對Spring進(jìn)行整合,很多新手對此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
成都創(chuàng)新互聯(lián)專注于唐河網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供唐河營銷型網(wǎng)站建設(shè),唐河網(wǎng)站制作、唐河網(wǎng)頁設(shè)計(jì)、唐河網(wǎng)站官網(wǎng)定制、重慶小程序開發(fā)公司服務(wù),打造唐河網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供唐河網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
1)MyBatis 框架所需的 JAR 包
圖 1MyBatis相關(guān)的JAR包
2)Spring 框架所需的 JAR 包
aopalliance-1.0.jar
aspectjweaver-1.6.9.jar
spring-aop-3.2.13.RELEASE.jar
spring-aspects-3.2.13.RELEASE.jar
spring-beans-3.2.13.RELEASE.jar
spring-context-3.2.13.RELEASE.jar
spring-core-3.2.13.RELEASE.jar
spring-expression-3.2.13.RELEASE.jar
spring-jdbc-3.2.13.RELEASE.jar
spring-tx-3.2.13.RELEASE.jar
3)MyBatis 與 Spring 整合的中間 JAR 包
該中間 JAR 包的版本為 mybatis-spring-1.3.1.jar,此版本可以從網(wǎng)址“http://mvnrepository.com/artifact/org.mybatis/mybatis-spring/1.3.1”下載。
4)數(shù)據(jù)庫驅(qū)動 JAR 包
教程所使用的Mybatis數(shù)據(jù)庫驅(qū)動包為 MySQL-connector-java-5.1.25-bin.jar。
5)數(shù)據(jù)源所需的 JAR 包
在整合時(shí)使用的是 DBCP 數(shù)據(jù)源,需要準(zhǔn)備 DBCP 和連接池的 JAR 包。
本教程所用版本的 DBCP 的 JAR 包為 commons-dbcp2-2.2.0.jar,可以從網(wǎng)址“htttp://commons.apache.org/proper/commons-dbcp/download_dbcp.cgi”下載。
最新版本的連接池的 JAR 包為 commons-pool2-2.5.0.jar,可以從網(wǎng)址“http://commons.apache.org/proper/commons-pool/download_pool.cgi”下載。
在Spring中配置MyBatis工廠
通過與 Spring 的整合,MyBatis 的 SessionFactory 交由 Spring 來構(gòu)建,在構(gòu)建時(shí)需要在 Spring 的配置文件中添加如下代碼:
<!--配置數(shù)據(jù)源--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/springtest?seUnicode=true&characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="1128" /> <!-- 最大連接數(shù) --> <property name="maxTotal" value="30"/> <!-- 最大空閑連接數(shù) --> <property name="maxIdle" value="10"/> <!-- 初始化連接數(shù) --> <property name="initialSize" value="5"/> </bean> <!-- 配置SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 引用數(shù)據(jù)源組件 --> <property name="dataSource" ref="dataSource" /> <!-- 引用MyBatis配置文件中的配置 --> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean>
使用 Spring 管理 MyBatis 的數(shù)據(jù)操作接口
使用 Spring 管理 MyBatis 數(shù)據(jù)操作接口的方式有多種,其中最常用、最簡潔的一種是基于 MapperScannerConfigurer 的整合。該方式需要在 Spring 的配置文件中加入以下內(nèi)容:
<!-- Mapper代理開發(fā),使用Spring自動掃描MyBatis的接口并裝配 (Sprinh將指定包中的所有被@Mapper注解標(biāo)注的接口自動裝配為MyBatis的映射接口) --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- mybatis-spring組件的掃描器,com.dao只需要接口(接口方法與SQL映射文件中的相同) --> <property name="basePackage" value="com.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean>
項(xiàng)目結(jié)構(gòu)
第一步:entity層
public class City implements Serializable { private long cid; private String cname; private long pid; public long getCid() { return cid; } public void setCid(long cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public long getPid() { return pid; } public void setPid(long pid) { this.pid = pid; } }
第二步:Dao層
@MapperScan public interface UserMapper { public City getUserList(Integer cid); }
第三步:service層
public interface UserService { public City getUserList(Integer cid); }
第四步:service實(shí)現(xiàn)層
@Service("userService") public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper;//聲明UserMapper接口引用 @Override public City getUserList(Integer cid) { return userMapper.getUserList(cid); } }
第五步:CityMapper.xml
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTO Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.smbms.dao.UserMapper"> <!--查--> <select id="getUserList" resultType="cn.smbms.entity.City"><!--resultMap屬性值指向resultMap節(jié)點(diǎn)的ID--> select * from city where cid=#{cid} </select> </mapper>
第六步:applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!--根節(jié)點(diǎn)是我們的beans節(jié)點(diǎn)--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 自動掃描 --> <context:component-scan base-package="cn.smbms" /> <!-- 引入配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 數(shù)據(jù)源配置--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--別名--> <property name="typeAliasesPackage" value="cn.smbms.entity"/> <!-- 自動掃描mapping.xml文件,**表示迭代查找 --> <property name="mapperLocations" value="classpath*:mapper/*.xml" /> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 ,包下的類需要使用@MapperScan注解,否則容器注入會失敗 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.smbms.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
第七步:jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?useUniCode=true&characterEncoding=utf-8 jdbc.username=root jdbc.password=root
第八步:mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- xml文件的頭文件,起到對文件的約束作用(例如:必須存在哪些節(jié)點(diǎn)) --> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><!--核心配置--> </configuration>
第九步:測試
@Test public void shouldAnswerWithTrue() { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService)ctx.getBean("userService"); City userList = userService.getUserList(130000); System.out.println(userList.getCname()); }
以上代碼是基于注解,如果想要XML方式,下面就是,謝謝根據(jù)上面的進(jìn)行修改:修改applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!--根節(jié)點(diǎn)是我們的beans節(jié)點(diǎn)--> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.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-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 自動掃描 --> <context:component-scan base-package="cn.smbms" /> <!-- 引入配置文件 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 數(shù)據(jù)源配置--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--別名--> <property name="typeAliasesPackage" value="cn.smbms.entity"/> <!-- 自動掃描mapping.xml文件,**表示迭代查找 --> <property name="mapperLocations" value="classpath*:mapper/*.xml" /> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 ,包下的類需要使用@MapperScan注解,否則容器注入會失敗 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.smbms.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> </bean> <bean id="userService" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="cn.smbms.dao.UserMapper"/> <property name="sqlSessionFactory" ref="sqlSessionFactory"/> </bean> <!-- Service層--> <bean id="iBankService" class="cn.smbms.service.impl.UserServiceImpl"> <property name="UserMapper" ref="userService"/> </bean>--> <!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
修改Dao層
public interface UserMapper { public City getUserList(Integer cid); }
修改Service實(shí)現(xiàn)類
public class UserServiceImpl implements UserService { public UserMapper getUserMapper() { return userMapper; } public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } private UserMapper userMapper;//聲明UserMapper接口引用 @Override public City getUserList(Integer cid) { return userMapper.getUserList(cid); } }
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)的支持。
本文標(biāo)題:MyBatis中怎么對Spring進(jìn)行整合
路徑分享:http://www.rwnh.cn/article20/jdjico.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、關(guān)鍵詞優(yōu)化、網(wǎng)站導(dǎo)航、品牌網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)