MyBatis conversion case problem when the project is stepping on the pit

MyBatis conversion case problem when the project is stepping on the pit

Since the MySQL database is not strictly case-sensitive in the Windows environment, we use an underscore "_" between two words when naming tables and fields in the database, such as "user_name". However, when we were developing the project, in order to comply with the code specification, the attributes in the entity class were all named in camel case. In this way, MyBatis may report an error for the get and set methods without this attribute.

Solution:
1. Use resultMap to perform one-to-one mapping through the column value (database field name) and property value (entity class attribute name) in resultMap.

<?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="com.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.domain.User">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="user_name" jdbcType="VARCHAR" property="userName" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    
  </resultMap>
  <select id="xxx" parameterType="xxx" resultMap="BaseResultMap" >
  ......
  </select>

2. Configure MyBatis:
(1) Add the following code to the mybatis-config.xml configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 
<configuration>
	<!-- 配置mybatis自动转换为驼峰命名 -->
	<settings>
		<setting name="mapUnderscoreToCamelCase" value="true" />
	</settings>
</configuration>

(2) Configure in the application.yml file:

#mybatis配置
mybatis:
	configuration:
	  map-underscore-to-camel-case: true

The map-underscore-to-camel-case can also be written as mapUnderscoreToCamelCase (not tried), and then the corresponding entity class is directly used in the MyBatis xml file to receive data.

<?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="com.mapper.UserMapper">
  <select id="xxx" parameterType="xxx" resultType="User" >
  ......
  </select>

When the method (1) and the method (2) are configured at the same time, the priority of the method (2) is higher. (Refer to other blogs)
3. Custom configuration class configuration: add a ConfigurationCustomizer to the container.

@Configuration
public class MyBatisConfig {
    
    
    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
    
    
        return new ConfigurationCustomizer() {
    
    
            @Override
            public void customize(org.apache.ibatis.session.Configuration configuration) {
    
    
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}

The first two methods refer to the blog: https://blog.csdn.net/qq_38738510/article
The third method refers to the blog: https://blog.csdn.net/liu_sisi/article/details/88360155

Guess you like

Origin blog.csdn.net/problemRecord/article/details/115251589