Mybatis-@MapperScan和mybatis:scan分析

MyBatis-Spring-1.2.0 新增了两种新的扫描映射器 Mapper 接口的方法:
使用<mybatis:scan/>元素
使用@MapperScan 注解(需要 Spring3.1+版本)


<mybatis:scan>

<mybatis:scan>元素将在特定的以逗号分隔的包名列表中搜索映射器 Mapper 接口。 使用这个新的 MyBatis-Spring 名空间你需要添加以下的 schema 声明:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://mybatis.org/schema/mybatis-spring
http://mybatis.org/schema/mybatis-spring.xsd
">
<mybatis:scan base-package="com.mybatis.x.mappers" />
</beans>
<mybatis:scan> 元素提供了下列的属性来自定义扫描过程:
annotation : 扫描器将注册所有的在 base-package 包内并且匹配指定注解的映射器 Mapper 接口。
factory - ref : Spring 上 下 文 中 有 多 个 SqlSessionFactory 实 例 时 , 需 要 指 定 某 一 特 定 的
SqlSessionFactory 来创建映射器 Mapper 接口。正常情况下,只有应用程序中有一个以上的数据源
才会使用。
marker - interface : 扫描器将注册在 base-package 包中的并且继承了特定的接口类的映射器 Mapper

template - ref : Spring 上下文中有多个 SqlSessionTemplate 实例时,需要指定某一特定的
SqlSessionTemplate 来创建映射器 Mapper 接口。 正常情况下,只有应用程序中有一个以上的数据源

才会使用。

 name-generator:BeannameGenerator 类的完全限定类名,用来命名检测到的组件。



MapperScan
   Spring 3.x+版本支持使用@Configuration @Bean 注解来提供基于 Java 的配置。如果使用基于java的配置,可以使用@MapperScan 注解来扫描映射器 Mapper 接口。 @MapperScan <mybatis:scan/>工作方式
相同,并且也提供了对应的自定义选项。

@Configuration
@MapperScan(
"com.mybatis.x.mappers")
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();
}
}

@MapperScan 注解有以下属性供自定义扫描过程使用 :
annotationClass : 扫描器将注册所有的在 base-package 包内并且匹配指定注解的映射器 Mapper 接口。
markerInterface : 扫描器将注册在 base-package 包中的并且继承了特定的接口类的映射器 Mapper 接口
sqlSessionFactoryRef : Spring 上 下 文 中 有 一 个以 上 的 SqlSesssionFactory 时 , 用 来 指 定 特 定
SqlSessionFactory
sqlSessionTemplateRef : Spring 上下文中有一个以上的 sqlSessionTemplate 时,用来指定特定
sqlSessionTemplate
-nameGenerator :BeanNameGenerator 类用来命名在 Spring 容器内检测到的组件。
basePackageClasses :basePackages() 的类型安全的替代品。 包内的每一个类都会被扫描。
basePackages : 扫描器扫描的基包,扫描器会扫描内部的 Mapper 接口。 注意包内的至少有一个方法声明的才会被

注册。 具体类将会被忽略。


当然还可以在 applicationContext.xml 配置如下

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name
="basePackage" value="com.mybatis3.mappers" />
</bean>
使用 MapperScannerConfigurer 来扫描包 package ("com.mybatis3.mappers")下的所有 映射器 Mapper 接口,并自动地注册





版本声明:原创文章,转载请注明出处












### MyBatis-Plus Boot Starter 3.5.1 XML Configuration Example For integrating `mybatis-plus-boot-starter` version 3.5.1 within a Spring Boot project, the primary configurations are typically handled through YAML or properties files rather than traditional XML configurations. However, when it comes to defining SQL mappings and customizing certain behaviors of MyBatis-Plus, XML can still play an important role. #### Application Properties via YAML Before diving into XML specifics, configuring basic data source settings is essential using YAML: ```yaml spring: datasource: username: root password: root url: jdbc:mysql://localhost/demo?useUnicode=true&characterEncoding=utf8 driver-class-name: com.mysql.cj.jdbc.Driver mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl global-config: db-config: logic-delete-value: 1 logic-not-delete-value: 0 ``` This setup ensures that the database connection details along with logging and logical deletion features are properly configured[^3]. #### Mapper XML Files Mapper XML files serve as templates where actual SQL queries reside. An example of such a file might look like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.mapper.UserMapper"> <!-- Select all users --> <select id="selectAllUsers" resultType="com.example.demo.entity.User"> SELECT * FROM user; </select> </mapper> ``` Each `<mapper>` element corresponds to one interface (or DAO), which contains various CRUD operations defined either by annotations directly on methods inside interfaces or explicitly written out here in XML format[^2]. #### Integrating Mappers Within Spring Boot To ensure these mappers get picked up automatically during application startup without needing explicit bean definitions for each individual mapper class, adding specific package scan paths becomes necessary. This action usually takes place within the main application class annotated with `@SpringBootApplication`. ```java @SpringBootApplication @MapperScan("com.example.demo.mapper") // Specify your own path accordingly. public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` By specifying the base packages containing mapper interfaces, Spring will register them appropriately at runtime, allowing seamless interaction between Java code and underlying relational databases facilitated by MyBatis-Plus[^1]. --related questions-- 1. How does one configure multiple datasources while working with MyBatis Plus? 2. What steps should be taken to enable debug-level logging specifically for MyBatis Plus SQL statements? 3. Can you provide guidance on implementing pagination support effectively using MyBatis Plus? 4. In what ways do MyBatis Plus interceptors enhance query performance optimization? 5. Is there any difference in setting up MyBatis Plus under different versions of Spring Boot applications?
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值