mybaits-plus common configuration

mybaitsplus common configuration

There are a lot of configurations in MP, some of which are native configurations of Mybatis, and the other are configurations of MP. For details, please refer to the documentation on the official website: https://baomidou.com/pages/56bac0/

Below we explain the commonly used configurations.

configLocations

configLocations is the location of the MyBatis configuration file. If you have a separate MyBatis configuration, please configure its path to configLocation. For the specific content of MyBatis Configuration, please refer to the official documentation of MyBatis

Example: 1 Create mybatis-config.xml under resources

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <!--mapUnderscoreToCamelCase=true-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <!--<plugins>-->
    <!--<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor"></plugin>-->
    <!--</plugins>-->

</configuration>

Configure configLocations under application.properties as follows:

#指定mybatis-config.xml的位置
mybatis-plus.config-location = classpath:mybatis/mybatis-config.xml

insert image description here

mapperLocations

#指定mapper文件位置
mybatis-plus.mapper-locations = classpath*:mybatis/mapper/*.xml

mapperLocations is the location of the mapper configuration file corresponding to MyBatis Mapper. If you have a custom method in Mapper (there is a custom implementation in XML), you need to perform this configuration and tell Mapper the corresponding XML file location.

If mapperLocations is not configured, the storage path of mapper's xml file needs to be consistent with the mapper class file, and the file name should be consistent, as follows: (It is better to configure it)
insert image description here

mapper.xml code example

New UserMapper.xml:

You can put this file under resources/cn/itcast/mp/mapper, here we put it directly under mybatis/mapper/UserMapper.xml, and then configure it

#指定mapper文件位置
mybatis-plus.mapper-locations = classpath*:mybatis/mapper/*.xml

Write in UserMapper.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="cn.itcast.mp.mapper.UserMapper">

    <select id="findById" resultType="user" parameterType="java.lang.Long">
      select * from tb_user where id = #{id}
    </select>
</mapper>

Added findById method to UserMapper interface class

package cn.itcast.mp.mapper;

import cn.itcast.mp.pojo.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

/**
 * Created by Administrator.
 */
public interface UserMapper extends BaseMapper<User> {
    
    

    public User findById(Long id);
}

Add a method to find the id in the test class

code show as below:

@Test
public void testFindByid(){
    
    
    //查询tb_user记录
    User user = userMapper.findById(2L);
    System.out.println(user.toString());
}

Running result:
insert image description here
Note : The scan path of Maven multi-module project needs to start with classpath*: (that is, load XML files under multiple jar packages)

typeAliasesPackage

Set the MyBaits alias package scan path. Through this attribute, you can register an alias for the class in the package. After registration, the class name can be used directly in the XML file corresponding to Mapper instead of using the fully qualified class name (that is, it is not required when calling in XML. including package name)

mybatis‐plus.type‐aliases‐package = cn.itcast.mp.pojo

mapUnderscoreToCamelCase

type: boolean
default value: true

Whether to enable automatic camel case mapping, that is, a similar mapping from the classic database column name A_COLUMN (underscore) to the classic Java property name aColumn (camel case).

Note : In MyBatis-Plus, the default value of this property is true, which is used to generate the final SQL statement. If your database name conforms to the rules, you do not need to use the @TableField annotation to specify the database field name

#开启自动驼峰映射
#mybatis-plus.configuration.map-underscore-to-camel-case=true

Note : configure configuration.map-underscore-to-camel-case, you cannot configure config-location

So after configuring config-location, what should I do when I want to configure mapUnderscoreToCamelCase?

Here you can use the previous configuration of mybaits, and add the configuration in mybatis/mybatis-config.xml:

    <settings>
        <!--mapUnderscoreToCamelCase=true-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

just fine

Turn on the automatic hump, we can block @TableField,
insert image description here
and then test, no problem.
insert image description here
insert image description here
If there are definitions that conform to the hump rule and some do not, it is recommended to use @TableField uniformly.

If you configure mybatis-plus.configuration in application.properties while using mybatis-config.xml, an error will be reported

Property 'configuration' and 'configLocation' can not specified with together 

Workaround: Use only one configuration method.

This case shields mybatis-plus.configuration.map-underscore-to-camel-case=true, and configures settings in mybatis-config.xml.

<settings> 
	<setting name="mapUnderscoreToCamelCase" value="true"/> 
</settings>

Guess you like

Origin blog.csdn.net/weixin_45525272/article/details/123694959