目录

配置

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配置文件官方文档

1.基本配置mybatis-config.xml

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

<plugins>
    <!--        配置分页插件-->
    <plugin interceptor="com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor">
        <property name="@page" value="com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor"/>
        <property name="page:dbType" value="MYSQL"/>
    </plugin>
</plugins>

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<User_plus> ipage=new Page<>();
        //当前页
        ipage.setCurrent(2);
        //每页数量
        ipage.setSize(2);

        //条件
        QueryWrapper<User_plus> wrapper=new QueryWrapper<>();
        wrapper.eq("age",20);
        IPage<User_plus> 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)

<!-- 3、配置mybatis-plus的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!--  配置数据源 -->
        <property name="dataSource" ref="dataSource" />
<!-- 配置mybatis全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>

    </bean>

2.mapperLocation配置

在DAO接口创建方法

public interface User_plusDAO extends BaseMapper<User_plus> {

    public List<User_plus> findAll();
}

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

<?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="mapper.User_plusDAO">
    <!-- 需要执行的sql语句,例如select,insert,update,delete,-->
    <select id="findAll" resultType="pojo.User_plus">
        SELECT id,name,age,email FROM user
    </select>
</mapper>

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、配置mybatis-plus的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!--  配置数据源 -->
        <property name="dataSource" ref="dataSource" />
<!-- 配置mybatis全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 配置mapper全局配置文件 -->
        <property name="mapperLocation" value="classpath:mapper/*.xml"/>

    </bean>

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文件中就可以直接使用类名,不需要加包名

<mapper namespace="mapper.User_plusDAO">
    <!-- 需要执行的sql语句,例如select,insert,update,delete,-->
<!-- 直接用类名,与前文中可对比-->
    <select id="findAll" resultType="User_plus">
        SELECT id,name,age,email FROM user
    </select>
</mapper>

spring在application.xml文件添加(mapperLocation

<!-- 3、配置mybatis-plus的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!--  配置数据源 -->
        <property name="dataSource" ref="dataSource" />
<!-- 配置mybatis全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 配置mapper全局配置文件 -->
        <property name="mapperLocation" value="classpath:mapper/*.xml"/>
<!-- 加载实体类 -->
    <property name="typeAliasesPackage" value="pojo"/>

    </bean>

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文件添加

<!-- 3、配置mybatis-plus的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<!--  配置数据源 -->
        <property name="dataSource" ref="dataSource" />
<!-- 配置mybatis全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 配置mapper全局配置文件 -->
        <property name="mapperLocation" value="classpath:mapper/*.xml"/>
<!-- 加载实体类 -->
    <property name="typeAliasesPackage" value="pojo"/>
<!-- 配置驼峰映射configLocation和configuration不能同时出现否则报错 -->
        <property name="configuration" >
            <bean class="com.baomidou.mybatisplus.core.MybatisConfiguration">
                <property name="mapUnderscoreToCamelCase" value="false"/>
            </bean>
        </property>

    </bean>

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文件添加

<property name="configuration" >
            <bean class="com.baomidou.mybatisplus.core.MybatisConfiguration">
                <property name="cacheEnabled" value="false"/>
            </bean>
        </property>

3.DB策略配置

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

springboot在application.yml文件添加

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

spring在application.xml文件添加

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="globalConfig">
            <bean class="com.baomidou.mybatisplus.core.config.GlobalConfig">
                <property name="dbConfig">
                    <bean class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig">
                        <property name="idType" value="AUTO"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

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

springboot在application.yml文件添加

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

spring在application.xml文件添加

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
        <property name="globalConfig">
            <bean class="com.baomidou.mybatisplus.core.config.GlobalConfig">
                <property name="dbConfig">
                    <bean class="com.baomidou.mybatisplus.core.config.GlobalConfig$DbConfig">
                        <property name="tablePrefix" value="tb_"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>