使用Spring扫描Mybatis的mapper接口的三种配置

Spring扫描Mybatis的mapper接口的配置

1.前言

mybatis支持与spring结合使用,使得mybatis中的mapper接口可以作为spring容器中的bean被应用代码中相关类,如Service类,通过@Autowired自动注入进来。

在使用方面需要在项目中引入以下包:

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>1.3.1</version>
</dependency>

2. 在spring中可以通过三种方式

来自动扫描获取应用代码的mybatis相关的mapper接口定义:

2.1在applicationContext.xml中使用<mybatis:scan />

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

   ...

    <!-- mybatis -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <property name="mapperLocations" value="classpath:mybatis/mapper/**/*.xml" />
     <property name="configLocation" value="classpath:mybatis/mybitas-config.xml"
        />
   </bean>
   <mybatis:scan base-package="cn.cggeeker.dao"/>

   ...
</beans>

2.2在applicationContext.xml中

使用bean标签注册MapperScannerConfigurer,即:

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

   ...

    <!-- mybatis -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <property name="mapperLocations" value="classpath:mybatis/mapper/**/*.xml" />
     <property name="configLocation" value="classpath:mybatis/mybitas-config.xml"            />
   </bean>

   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <property name="basePackage" value="cn.cggeeker.dao" />
   </bean>

   ...

</beans>

2.3如果应用是使用Java配置方式而不是XML

则在@Configuration配置类使用@MapperScan或者@MapperScans注解:

@Configuration
@MapperScan("cn.cggeeker.dao")
public class AppConfig {

    @Bean
    public DataSource dataSource() {
        return new PooledDataSource("com.mysql.jdbc.Driver",
                   "jdbc:mysql://localhost:3306/test", "root", "root");
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBeansessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        return sessionFactory.getObject();
    }
}

Spring配置扫描mybatis的mapper文件注意

一般会将不业务的mapper文件放到不同的包中:

spring配置扫描就需要配置下面的方式(两个*):

<!-- mybatis文件配置,扫描所有mapper文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:dataSource-ref="dataSource" p:configLocation="classpath:conf/mybatis-config.xml"
        p:mapperLocations="classpath:mapper/**/*.xml" />

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • spring与mybatis三种整合方法

    1.采用MapperScannerConfigurer,它将会查找类路径下的映射器并自动将它们创建成MapperFactoryBean. spring-mybatis.xml: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3

  • Spring Boot集成MyBatis实现通用Mapper的配置及使用

    什么是通用Mapper 通用Mapper就是为了解决单表增删改查,基于Mybatis的插件.开发人员不需要编写SQL,不需要在DAO中增加方法,只要写好实体类,就能支持相应的增删改查方法. 关于MyBatis,大部分人都很熟悉.MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Pla

  • SpringBoot+Mybatis使用Mapper接口注册的几种方式

    目录 I. 环境准备 1. 数据库准备 2. 项目环境 II. 实例演示 1. 实体类,Mapper类 2. 注册方式 2.1 @MapperScan注册方式 2.2 @Mapper 注册方式 2.3 MapperScannerConfigurer注册方式 3. 小结 III. 不能错过的源码和相关知识点 SpringBoot项目中借助Mybatis来操作数据库,对大部分java技术栈的小伙伴来说,并不会陌生:我们知道,使用mybatis,一般会有下面几个 Entity: 数据库实体类 Mapp

  • Mybatis MapperScannerConfigurer自动扫描Mapper接口生成代理注入到Spring的方法

    前言 Mybatis MapperScannerConfigurer 自动扫描 将Mapper接口生成代理注入到Spring Mybatis在与Spring集成的时候可以配置 MapperFactoryBean来生成Mapper接口的代理. 例如: <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mappe

  • Spring整合MyBatis的三种方式

    1.整合之前的环境准备 导入相关的jar包 Junit测试 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> MyBatis <dependency> <groupId

  • 使用Spring扫描Mybatis的mapper接口的三种配置

    Spring扫描Mybatis的mapper接口的配置 1.前言 mybatis支持与spring结合使用,使得mybatis中的mapper接口可以作为spring容器中的bean被应用代码中相关类,如Service类,通过@Autowired自动注入进来. 在使用方面需要在项目中引入以下包: <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifac

  • SpringBoot+Mybatis实现Mapper接口与Sql绑定几种姿势

    目录 I. 环境准备 1. 数据库准备 2. 项目环境 II. 实例演示 1. 实体类,Mapper接口 2. sql文件 3. Mapper与Sql绑定 3.1 默认方式 3.2 SpringBoot配置 3.3 Mapper标签 3.4 SqlSessionFactory 4. 小结 III. 不能错过的源码和相关知识点 通常我们在使用Mybatis进行开发时,会选择xml文件来写对应的sql,然后将Mapper接口与sql的xml文件建立绑定关系,然后在项目中调用mapper接口就可以执行

  • mapper接口注入两种方式详解

    这篇文章主要介绍了mapper接口注入两种方式详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.使用模板方式: <!--使用模板类实现mybatis --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg name="sqlSessionFacto

  • Spring+SpringMVC+MyBatis深入学习及搭建(三)之MyBatis全局配置文件解析

    前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(二)--MyBatis原始Dao开发和mapper代理开发 MyBatis的全局配置文件SqlMapConfig.xml,配置内容和顺序如下: properties(属性) setting(全局配置参数) typeAliases(类名别名) typeHandlers(类名处理器) objectFactory(对象工厂) plugins(插件) environments(环境集合属性对象) environment(环境子属性

  • java调用Restful接口的三种方法

    目录 1,基本介绍 2,HttpURLConnection实现 3.HttpClient实现 4.Spring的RestTemplate 1,基本介绍 Restful接口的调用,前端一般使用ajax调用,后端可以使用的方法比较多, 本次介绍三种: 1.HttpURLConnection实现 2.HttpClient实现 3.Spring的RestTemplate 2,HttpURLConnection实现 @Controller public class RestfulAction { @Aut

  • MyBatis批量插入数据的三种方法实例

    目录 前言 准备工作 1.循环单次插入 2.MP 批量插入 ① 控制器实现 ② 业务逻辑层实现 ③ 数据持久层实现 MP 性能测试 MP 源码分析 3.原生批量插入 ① 业务逻辑层扩展 ② 数据持久层扩展 ③ 添加 UserMapper.xml 原生批量插入性能测试 缺点分析 解决方案 总结 前言 批量插入功能是我们日常工作中比较常见的业务功能之一,之前我也写过一篇关于<MyBatis Plus 批量数据插入功能,yyds!>的文章,但评论区的反馈不是很好,主要有两个问题:第一,对 MyBat

  • Spring Boot 教程之创建项目的三种方式

    目录 一.前言 二.Spring Boot 简介 三.如何创建 Spring Boot 项目 在线创建 IntelliJ IDEA 创建 Maven 创建 四.常见项目结构 代码层 资源文件结构 五.@SpringBootApplication 注解分析 相关代码 说明 六.pom.xml 分析 七.总结 一.前言 如果你是一个浸淫 SpringBoot 已久的老手,那么可能下面的内容可能不那么适合你,写得很简单.但如果是 对于一个刚学习 SpringBoot 的新手而言,我想多少还是有些用的.

  • Springboot打印接口的三种方式分享

    目录 1 aop切面的方式 1.1 实现思路 1.2 代码实现 1.3 功能测试 2 过滤器的方式 3 拦截器的方式 1 aop切面的方式 1.1 实现思路 引入aop依赖 自定义注解 定义切面,采用环绕通知 1.2 代码实现 1)引入依赖 xml <!--aop--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-

  • Spring框架通过工厂创建Bean的三种方式实现

    工厂模式 Spring中bean的创建,默认是框架利用反射new出来的bean实例.有时候也会有一些复杂的情况. 假设有一个飞机,属性如下,现在需要造很多同型号的飞机,那么唯一需要改变的属性只有DriverName(机长姓名),此时可以使用工厂模式帮我们创建对象,有一个专门帮我们创建对象的类帮我们创建对象,这个类就叫工厂. public class AirPlane { private String DriverName;// 机长姓名 private String AirPlaneName;/

随机推荐