mybatis下划线转驼峰配置

1、mybatis下划线转驼峰配置(ssm项目)

只需要在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="mapUnderscoreToCamelCase" value="true"/>
		<!--<setting name="cacheEnable" value="true"/> -->
		<setting name="defaultStatementTimeout" value="3000" />
		<!--<setting name="mapUnderscoreToCamelCase" value="true"/> -->
		<!--<setting name="proxyFactory" value="CGLIB"/> -->
		<!--<setting name="lazyLoadingEnabled" value="true"/> -->
	</settings>
</configuration>

需要注意的是:

  • 此处的下划线转驼峰,是指在pojo、dao、service等使用驼峰,而数据库映射文件,相应的mapper中还需使用下划线进行查询。
<?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.xxx.xxx.dao.xxxDao">
    <select id="getBridgeCount" resultType="int">
        select count(*) from bridge_status
    </select>
    <select id="getBridgeType" resultType="string">
        select bridge_type from bridge_status
    </select>
<mapper>

如此,ssm项目中在后端和前端页面中均可以直接使用驼峰命名对应数据库字段。

2、mybatis下划线转驼峰配置(springboot项目)

需要新建配置包config,在配置包里面新建配置类MyBatisConfig,在配置类中进行设置。
自定义MyBatis的配置规则;给容器中添加一个ConfigurationCustomizer;

@org.springframework.context.annotation.Configuration
public class MyBatisConfig {

    @Bean
    public ConfigurationCustomizer configurationCustomizer(){
        return new ConfigurationCustomizer(){

            @Override
            public void customize(Configuration configuration) {
                configuration.setMapUnderscoreToCamelCase(true);
            }
        };
    }
}

如此,springboot项目中在后端和前端页面中均可以直接使用驼峰命名对应数据库字段。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值