数据库字段转bean属性(驼峰)

/**
	 * 列名转属性名(驼峰)
	 * @param column
	 * @return
	 */
	public static String column2modelProperty(String column) {
		int i = column.indexOf("_");
		if(i == column.length() -1) {
			return column.substring(0, i);
		}
		if(i == -1) {
			return column;
		}
		if(i+2<column.length()) {
			column = column.substring(0, i)+column.substring(i+1, i+2).toUpperCase()+column.substring(i+2);
		} else {
			column = column.substring(0, i)+column.substring(i+1, i+2).toUpperCase();
		}
		if(column.indexOf("_") != -1) {
			return column2modelProperty(column);
		}
		return column;
	}

方法入参前先转化为小写。

在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整合的情况下将数据库中下划线命字段换为驼峰字段
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值