mapper注册(@Mapper @MapperScan)

isixe 2022-09-28 15:39:29
Categories: Tags:

 

 

参数

描述

value

扫描的包名

basePackages

用于扫描 MyBatis 接口的基本包。

basePackageClasses

basePackages() 的类型安全替代方案,用于指定要扫描带注释组件的包。

nameGenerator

用于命名 Spring 容器中检测到的组件的 BeanNameGenerator 类。

annotationClass

此属性指定扫描器将搜索的注解

markerInterface

此属性指定扫描程序将搜索的父项。

sqlSessionTemplateRef

指定在 spring 上下文中有多个的情况下使用哪个 SqlSessionTemplate

sqlSessionFactoryRef

指定在 spring 上下文中有多个的情况下使用哪个 SqlSessionFactory

factoryBean

指定一个自定义 MapperFactoryBean 以返回一个 mybatis 代理作为 spring bean

lazyInitialization

是否开启ma​​pper bean的延迟初始化

defaultScope

指定扫描映射器的默认范围

示例

@Mapper

public interface UserLoginMapper {

    //查询

    public List<UserLogin> queryAll();

    //添加数据

    public int add(UserLogin userLogin);

    //根据用户名查询数据

    public UserLogin queryByName(String username);

}

示例

@MapperScan("com.example.mapper") //扫描的mapper

@SpringBootApplication

public class DemoApplication {

 

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

    }

}