本文主要是介绍mybatis属性callSettersOnNulls,mapUnderscoreToCamelCase设置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
mapUnderscoreToCamelCase:
是否启用下划线与驼峰式命名规则的映射(如first_name => firstName)
开启:mybatis-config.xml
- <!-- 开启驼峰命名转换 seckill_id====>seckillId -->
- <setting name="mapUnderscoreToCamelCase" value="true"/>
在spring-dao.xml中进行配置
- <!-- mybatis- sessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <!-- 注入数据库连接池 -->
- <property name="dataSource" ref="dataSource"/>
- <!-- 配置mybatis全局配置文件 -->
- <property name="configLocation" value="classpath:mybatis-config.xml"/>
- <!-- 扫描entity包 使用别名
- org.seckill.entity.Seckill===>Seckill resultType,parameterType
- -->
- <property name="typeAliasesPackage" value="org.seckill.entity"/>
- <!-- 扫描sql配置文件 ,mapper.xml -->
- <property name="mapperLocations" value="classpath:mapper/*.xml"/>
- </bean>
callSettersOnNulls:
Spring+MyBatis开发过程中,在xxMapper.xml配置文件进行select查询时resultType="map",如果要查询的字段是空值,在返回的map中会出现找不到这个字段对应的属性。要解决这个问题需要指定 callSettersOnNulls 属性,具体过程如下:
1,创建xml配置文件。在applicationContext.xml 同一级目录中创建文件mybatis-config.xml,内容如下:
- <?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>
- <setting name="callSettersOnNulls" value="true"/>
- </settings>
- ...
- </configuration>
2,在applicationContext-mybatis.xml文件中配置 mybatis-config.xml的引用,部分代码如下:
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
- p:dataSource-ref="dataSource" p:configLocation="classpath:mybatis-config.xml"
- p:mapperLocations="classpath:mybatis/mapper/*.xml" />
这篇关于mybatis属性callSettersOnNulls,mapUnderscoreToCamelCase设置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!