驼峰命名法和数据库下划线问题和一个mybatis的源码解释网站

参考:   https://segmentfault.com/a/1190000010240142

 

 

驼峰式命名开关,数据库列和字段名全一致。 开启 后不论多少下划线都可以  我这种方法不行

这种就是在mybatis-xml文件里面 

Mybatis提供了一个配置项。开启开配置项后,在匹配时,能够根据数据库列名找到对应对应的驼峰式命名后的字段.

我试过这种方法不管用可能是我配置问题.

Select时指定AS。

当我们的数据库列名和对象字段之间不是驼峰式命名的关系,我们可以在Select时使用AS,使得列名和对象名匹配上。
映射文件中是本次会执行的sql,我们会查出id,city_id,city_name,city_en_name。 按照开启的驼峰式命名开关,我们会对应到对象的id,cityId,cityName,cityEnName字段。

这种太麻烦..每次添加字段都要配置.

resultMap 最稳健。

  作为一名一线应用开发人员,“配置”一词,可能已经听得耳朵都长茧了。但是,一个程序或者说是一个库,具有可配置性,是非常必要的,否则就得以纯编程的方式使用它们。试想一下,如果你在使用数据库产品时,你还需要通过编程来使用,那将是多么地糟糕!

     配置不仅仅是使用的人要用,这个库或者框架的开发者自己也需要用,否则,如何组织内部的各个构件,将会是一件硬编码的事情。总之,配置,就是要组织出一个完整的逻辑或形式系统,以达到使用者的目的。在框架内部来说,通过配置,还在看清楚整个架构。

     通过MyBatis的配置,可以看出整个框架的顶级特性。为什么这么说呢,把这些代码列出来就一目了然了。

点击(此处)折叠或打开

  1. public class Configuration {
  2.  
  3.   protected Environment environment;
  4.  
  5.   protected boolean safeRowBoundsEnabled = true;
  6.   protected boolean mapUnderscoreToCamelCase = false;
  7.   protected boolean lazyLoadingEnabled = false;
  8.   protected boolean aggressiveLazyLoading = true;
  9.   protected boolean multipleResultSetsEnabled = true;
  10.   protected boolean useGeneratedKeys = false;
  11.   protected boolean useColumnLabel = true;
  12.   protected boolean cacheEnabled = true;
  13.   protected Integer defaultStatementTimeout;
  14.   protected ExecutorType defaultExecutorType = ExecutorType.SIMPLE;
  15.   protected AutoMappingBehavior autoMappingBehavior = AutoMappingBehavior.PARTIAL;
  16.  
  17.   protected Properties variables = new Properties();
  18.   protected ObjectFactory objectFactory = new DefaultObjectFactory()
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值