MyBatis 配置使用小驼峰也可以访问数据库下划线属性

本文介绍了如何在应用配置中使用小驼峰命名法访问下划线命名的数据库属性,同时保持对既有数据库表如user_info的兼容性,强调了保持统一格式的重要性,主要讨论了在application.xml中的配置策略。
摘要由CSDN通过智能技术生成

这样做之后,你可以使用小驼峰来访问数据库的下划线命名属性,也仍然可以用下划线访问数据库属性,比如数据库user表的user_info,你可以使用user_info,userInfo,都能访问

但最好还是保持统一的格式

这是在配置文件application.xml里:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring和MyBatis整合的情况下,可以通过在MyBatis配置文件中配置一个插件来实现下划线驼峰。 具体操作如下: 1. 在MyBatis配置文件中添加以下插件配置: ``` <plugins> <plugin interceptor="org.apache.ibatis.plugin.Interceptor"> <property name="properties"> <property name="helperDialect" value="mysql"/> <property name="offsetAsPageNum" value="true"/> <property name="rowBoundsWithCount" value="true"/> <property name="reasonable" value="true"/> </property> <property name="typeAliases"> <typeAlias type="com.example.domain.User" alias="User"/> </property> <property name="mapperLocations" value="classpath:com/example/mapper/*.xml"/> </plugin> </plugins> ``` 2. 在Spring的配置文件中配置一个Bean,将上一步中配置的插件添加到SqlSessionFactory中: ``` <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/example/mapper/*.xml"/> <property name="plugins"> <array> <bean class="com.example.plugin.CamelCaseInterceptor"/> </array> </property> </bean> ``` 3. 编写一个插件类,实现下划线驼峰的逻辑: ``` public class CamelCaseInterceptor implements Interceptor { @Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); MappedStatement ms = (MappedStatement) args[0]; Object parameter = args[1]; BoundSql boundSql = ms.getBoundSql(parameter); String sql = boundSql.getSql(); // 下划线驼峰 sql = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, sql); BoundSql newBoundSql = new BoundSql(ms.getConfiguration(), sql, boundSql.getParameterMappings(), boundSql.getParameterObject()); MappedStatement newMs = copyFromMappedStatement(ms, new BoundSqlSqlSource(newBoundSql)); args[0] = newMs; return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { // do nothing } private MappedStatement copyFromMappedStatement(MappedStatement ms, SqlSource newSqlSource) { MappedStatement.Builder builder = new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), newSqlSource, ms.getSqlCommandType()); builder.resource(ms.getResource()); builder.fetchSize(ms.getFetchSize()); builder.statementType(ms.getStatementType()); builder.keyGenerator(ms.getKeyGenerator()); builder.keyProperty(StringUtils.join(ms.getKeyProperties(), ",")); builder.timeout(ms.getTimeout()); builder.parameterMap(ms.getParameterMap()); builder.resultMaps(ms.getResultMaps()); builder.cache(ms.getCache()); builder.useCache(ms.isUseCache()); return builder.build(); } } ``` 通过以上步骤,即可实现在Spring和MyBatis整合的情况下将数据库下划线命名的字段转换为驼峰命名的字段。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值