mybatisplus配置详解

目录

配置

1.基本配置mybatis-config.xml

1.configLocation配置

springboot在application.yml文件添加(config-location)

spring在application.xml文件添加(configLocation)

2.mapperLocation配置

springboot在application.yml文件添加(mapper-locations)

3.typeAliases

2.进阶设置

1.mapUnderscoreToCamelCase驼峰映射

2.cacheEnabled缓存配置

3.DB策略配置

1.Idtype(配置后可省略注解@TableId)

2.tablePrefix表前缀(配置后可省略注解@TableName)


配置

在mp中有大量的配置,其中有一部分时mybatis原生的配置,另一部分是mp的配置,详情mybatis配置文件官方文档https://mybatis.org/mybatis-3/zh/configuration.html

1.基本配置mybatis-config.xml

在mybatis-config.xml文件添加插件


    
    
        
        
    

1.configLocation配置

springboot在application.yml文件添加(config-location

将单独的mybatis配置文件添加到config-location中

  • 类型:String
  • 默认值:null
#导入全局配置文件
mybatis-plus:
  config-location: classpath:mybatis-config.xml

这样就可以直接使用Page分页

    @Autowired(required = false)
    User_plusDAO user_plusDAO;

    @Test
    public void selectPage(){
        //分页设置
        IPage ipage=new Page<>();
        //当前页
        ipage.setCurrent(2);
        //每页数量
        ipage.setSize(2);

        //条件
        QueryWrapper wrapper=new QueryWrapper<>();
        wrapper.eq("age",20);
        IPage page = user_plusDAO.selectPage(ipage,wrapper);
        System.out.println("当前页:"+page.getCurrent());
        System.out.println("总页数:"+page.getPages());
        System.out.println("总条数:"+page.getTotal());
        System.out.println("查询当前页的集合:"+page.getRecords());
        
    }

spring在application.xml文件添加(configLocation

    
    

        

        

    

2.mapperLocation配置

在DAO接口创建方法

public interface User_plusDAO extends BaseMapper {

    public List findAll();
}

在resources下创建mapper文件夹存放mapper文件,创建UserMapper.xml文件实现DAO接口





    
    

springboot在application.yml文件添加(mapper-locations

mybatis-plus:
  #mybatis配置文件
  config-location: classpath:mybatis-config.xml
  #mapper配置文件。可能有多个,所以Mapper.xml结尾的都添加进来
  mapper-locations: classpath:mapper/*Mapper.xml

即可测试方法

spring在application.xml文件添加(mapperLocation

    
    

        

        

        

    

3.typeAliases

springboot在application.yml文件添加(mapper-locations

#导入全局配置文件
mybatis-plus:
  #mybatis配置文件
  config-location: classpath:mybatis-config.xml
  #mapper配置文件
  mapper-locations: classpath:mapper/*Mapper.xml
  #批量注册指定包下的类
  type-aliases-package: pojo

 类注册之后,在UserMapper.xml文件中就可以直接使用类名,不需要加包名


    

    

spring在application.xml文件添加(mapperLocation

    
    

        

        

        

    

    

2.进阶设置

1.mapUnderscoreToCamelCase驼峰映射

springboot在application.yml文件添加

#导入全局配置文件
mybatis-plus:
  #mybatis配置文件
  #config-location: classpath:mybatis-config.xml
  #mapper配置文件
  mapper-locations: classpath:mapper/*Mapper.xml
  #批量注册指定包下的类
  type-aliases-package: pojo
  #类属性与表字段的驼峰映射,mybatiplus默认true开启,mybatis需要手动配置,且config-location和configuration不能同时出现
  configuration:
    map-underscore-to-camel-case: false

spring在application.xml文件添加

    
    

        

        

        

    

        
            
                
            
        

    

2.cacheEnabled缓存配置

springboot在application.yml文件添加

mybatis-plus:
  #mybatis配置文件
  #config-location: classpath:mybatis-config.xml
  #mapper配置文件
  mapper-locations: classpath:mapper/*Mapper.xml
  #批量注册指定包下的类
  type-aliases-package: pojo
  configuration:
    #类属性与表字段的驼峰映射,mybatiplus默认true开启,mybatis需要手动配置
    map-underscore-to-camel-case: false
    #缓存
    cache-enabled: false

spring在application.xml文件添加

        
            
                
            
        

3.DB策略配置

1.Idtype(配置后可省略注解@TableId)

springboot在application.yml文件添加

mybatis-plus:
  #全局配置
  global-config:
    #数据库配置
    db-config:
      #主键策略
      id-type: auto

spring在application.xml文件添加

    
        
            
                
                    
                        
                    
                
            
        
    

2.tablePrefix表前缀(配置后可省略注解@TableName)

springboot在application.yml文件添加

mybatis-plus:
  #全局配置
  global-config:
    #数据库配置
    db-config:
      #表名前缀为tb_,表名为前缀拼接类名(小写)
      table-prefix: tb_

spring在application.xml文件添加

    
        
            
                
                    
                        
                    
                
            
        
    

你可能感兴趣的:(java,mybatisplus,springboot,config)