Spring整合mybatis使用

本文详细介绍了在Spring框架中整合MyBatis进行数据库操作的过程,包括依赖包配置、Spring XML文件配置、myBatis配置文件及SQL配置文件的创建、DAO接口和实现类的编写以及Controller层的调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。

Spring框架中整合mybatis主要下面几个步骤:


1. 需要解决依赖包。在POM.XML中,配置一下依赖(希望你在看这篇文章的时候,已经熟练使用mvn了,否则挺麻烦的!)

[html]  view plain copy print ?
  1. <!-- mybatis -->  
  2.          <dependency>  
  3.             <groupId>org.mybatis</groupId>  
  4.             <artifactId>mybatis</artifactId>  
  5.             <version>3.1.1</version>  
  6.         </dependency>    
  7.             <dependency>    
  8.                 <groupId>org.mybatis</groupId>    
  9.                 <artifactId>mybatis-spring</artifactId>    
  10.                 <version>1.1.1</version>    
  11.             </dependency>    
  12.         <dependency>  
  13.             <groupId>commons-dbcp</groupId>  
  14.             <artifactId>commons-dbcp</artifactId>  
  15.             <version>1.4</version>  
  16.         </dependency>  
  17.         <dependency>  
  18.             <groupId>mysql</groupId>  
  19.             <artifactId>mysql-connector-java</artifactId>  
  20.             <version>5.1.23</version>  
  21.         </dependency>  

2. 依赖包解决之后,我们首先要配置spring的xml文件,我们配置了一个service-dal.xml的文件,这个文件会被web.xml包含进去,这个文件中主要做了几个工作:JDBC的数据库链接,sqlSessionFactory,SqlSessionTemplate以及扫描Impl的包。

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns:beans="http://www.springframework.org/schema/beans"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  6.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"  
  7.     default-autowire="byName">  
  8.     <!-- 数据库JDBC的配置 -->  
  9.     <beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  10.         <beans:property name="driverClassName" value="com.mysql.jdbc.Driver"></beans:property>  
  11.         <beans:property name="url" value="${account.mysql.url}"></beans:property>  
  12.         <beans:property name="username" value="${account.mysql.username}"></beans:property>  
  13.         <beans:property name="password" value="${account.mysql.password}"></beans:property>  
  14.         <beans:property name="defaultAutoCommit" value="true"></beans:property>  
  15.     </beans:bean>  
  16.     <!-- 加载myBatis-config.xml配置文件,以及扫描myBatis/目录下每个DAO对应的SQL配置的XML文件 -->  
  17.     <beans:bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
  18.         <beans:property name="dataSource" ref="dataSource" />  
  19.         <beans:property name="configLocation" value="classpath:myBatis-config.xml" />  
  20.         <beans:property name="mapperLocations" value="classpath:myBatis/*.xml" />  
  21.     </beans:bean>  
  22.     <!-- SqlSessionTemplate是我们代码中使用的SQL模板对象,用来操作数据库 -->  
  23.     <beans:bean class="org.mybatis.spring.SqlSessionTemplate">  
  24.         <beans:constructor-arg ref="sqlSessionFactory"/>  
  25.     </beans:bean>  
  26.     <!-- Spring Bean扫描 -->  
  27.     <context:component-scan base-package="com.xxx.account.dal">  
  28.         <context:include-filter type="regex" expression=".*Impl" />  
  29.     </context:component-scan>  
  30. </beans:beans>  

3. 上面这个配置文件中,我们导入了myBatis-config.xml配置文件。由于我们开启了mapperLocations自动扫描DAO对应的SQL配置的XML文件,所以myBatis-config.xml非常简单。

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  3. "http://mybatis.org/dtd/mybatis-3-config.dtd">  
  4. <configuration>  
  5.     <mappers></mappers>  
  6. </configuration>  

4. myBatis/目录下是SQL配置的文件,以一个简单的XML文件为例,我们使用如下:(下面的例子是删除一个数据)

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="UTF-8" ?>    
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">   
  3. <!--这边的namespace很重要哦,调用的时候需要这个namespace来调哦!-->  
  4. <mapper namespace="com.mybatis.UserDao">  
  5.     <delete id="countAll">  
  6.         DELETE FROM guest_book WHERE id = 41  
  7.     </delete>  
  8. </mapper>  

5. 配置已经基本完成了,剩下,我们需要写一个DAO了,因为删除语句比较简单,DAO也比较简单

声明接口

[java]  view plain copy print ?
  1. package com.xxx.account.dal;  
  2.   
  3. public interface OpenapiInfoDal {  
  4.       
  5.       
  6.     Integer getAll();  
  7. }  

基类,注解的方式载入SqlSessionTemplate实例

[java]  view plain copy print ?
  1. package com.xxx.account.dal;  
  2.   
  3. import org.mybatis.spring.SqlSessionTemplate;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5.   
  6. public class BaseDal {  
  7.       
  8.     @Autowired  
  9.     protected SqlSessionTemplate sqlSessionTemplate;  
  10. }  

具体实现:

[java]  view plain copy print ?
  1. package com.xxx.account.dal.impl;  
  2.   
  3. import com.xxxx.account.dal.BaseDal;  
  4. import com.xxx.account.dal.OpenapiInfoDal;  
  5.   
  6.   
  7. public class OpenapiInfoDalImpl extends BaseDal implements OpenapiInfoDal {  
  8.       
  9.     public Integer getAll() {  
  10.         sqlSessionTemplate.delete("com.mybatis.UserDao.countAll");  
  11.         return 1;  
  12.     }  
  13. }  

Controller中调用:

[java]  view plain copy print ?
  1. package com.xxx.account.web;  
  2.   
  3.   
  4. /** 
  5.  * Handles requests for the application home page. 
  6.  */  
  7. @Controller  
  8. public class HomeController {  
  9.       
  10.       
  11.     @Autowired  
  12.     private OpenapiInfoDal openapiInfoDal;  
  13.       
  14.     @RequestMapping(value = "/test", method = RequestMethod.GET)  
  15.     @ResponseBody  
  16.     public String test(HttpServletRequest request) {  
  17.         openapiInfoDal.getAll(); //调用删除方法  
  18.           
  19.     }  
  20.       
  21. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值