mybatis-plus基本配置

    技术2022-07-10  142

    0.配置完成版

    (1)模板

    #mybatisplus的相关配置 mybatis-plus: #指定全局配置文件【二选一】 # config-location: classpath:mybatis-config.xml #mapper配置文件 mapper-locations: classpath*:mapper/*.xml #开启实体类包路径扫描 type-aliases-package: cn.pojo configuration: #开启驼峰命名:默认开启,【二选一】 map-underscore-to-camel-case: true #开启二级缓存,默认开启 cache-enabled: true global-config: db-config: # 全局主键自增长模式 id-type: auto # # 全局的数据库表名的前缀配置【不推荐】 # table-prefix: tb_

    (2)范例

    一.configlocation

    1.作用

    开启全局配置文件。mybatis主配置文件

    2.语法

    (1)springboot的yml方式

    #mybatisplus的相关配置 mybatis-plus: #指定全局配置文件【二选一】 config-location: classpath:mybatis-config.xml

    (2)spring原生方式

    <!-- 注意:需要放置到sqlFactory标签下--> <!-- 开启mybatis主配置文件的扫描 --> <property name="configLocation" value="classpath:mybatis-config.xml"></property>

    3.范例

    (1)springboot整合mybatis-plus【推荐】

    (2)spring原生整合

    二.mapper配置文件

    1.作用

    扫描mapper映射配置文件。

    2.语法

    #mybatisplus的相关配置 mybatis-plus: #mapper配置文件 mapper-locations: classpath*:mapper/*.xml

    3.范例

    三.pojo实体类配置

    1.作用

    扫描实体类的包路径。

    2.语法

    #mybatisplus的相关配置 mybatis-plus: #开启实体类包路径扫描 type-aliases-package: cn.pojo

    3.范例

    四.开启二级缓存和驼峰式命名

    1.作用

    开启二级缓存和自动实体类驼峰式命名。

    2.语法

    #mybatisplus的相关配置 mybatis-plus: configuration: #开启驼峰命名:默认开启,【二选一】 map-underscore-to-camel-case: true #开启二级缓存,默认开启 cache-enabled: true

    3.范例

    五.配置主键生成策略

    1.作用

    设置主键的生成方式。

    2.语法

    (1)springboot的yml配置方式

    #mybatisplus的相关配置 mybatis-plus: global-config: db-config: # 全局主键自增长模式 id-type: auto # 全局的数据库表名的前缀配置【不推荐】 table-prefix: tb_

    (2)spring配置

    <!--配置实体类的主键生成策略 --> <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>

    3.范例

    (1)springboot的yml配置方式

    (2)spring配置

    六.源码

    1.springboot整合

    mybatisplus.rar

    2.spring整合

    mybatis-plus.rar

    Processed: 0.010, SQL: 9