问题:Ibatis中,由于parameterMap无法将传入的java属性转换为数据库的字段。查了Ibatis的文档,针对Ado.Net是存在column字段的,而针对java没有。搞不明白为什么针对java没有?
Ado.net的配置大致如下,参考:http://developer.51cto.com/art/200907/138508.htm
- ﹤parameterMap class="Employee" id="Employee_SelectParameterMap"﹥
- ﹤parameter column="EmployeeID" property="EmployeeID" dbType="int" type="int" direction="Input"/﹥
- ﹤parameter column="LastName" property="LastName" dbType="nvarchar" type="string" direction="Input"/﹥
- ﹤parameter column="Country" property="Country" dbType="nvarchar" type="string" direction="Input"/﹥
- ﹤/parameterMap﹥
Java的parameterMap 配置大致如下:【注意】其中的 $sort$ $dir$,防止ibatis添加"'"
- <parameterMap class="java.util.Map" id="paramQueryMap">
- <parameter property="appId" />
- <parameter property="listValue" />
- <parameter property="sort" />
- <parameter property="dir" />
- <parameter property="begin" />
- <parameter property="increment" />
- </parameterMap>
- <select id="getBlackList" resultMap="HummockBlackListResult"
- parameterMap="paramQueryMap">
- SELECT
- <include refid="columns" />
- <![CDATA[
- FROM black_list
- ]]>
- <dynamic prepend="where">
- <isNotEmpty prepend="and" property="appId">
- app_Id = #appId#
- </isNotEmpty>
- <isNotEmpty prepend="and" property="listValue">
- list_value =
- #listValue#
- </isNotEmpty>
- <isNotEmpty prepend="order by" property="sort">
- $sort$ $dir$
- </isNotEmpty>
- </dynamic>
- limit #begin#,#increment#
- </select>
自己写的转换操作的方法如下:
- public class PropertyColumnWrapper {
- private static Logger logger = LoggerFactory.getLogger(PropertyColumnWrapper.class);
- public static String getColumn(String property) {
- if (StringUtils.isBlank(property)) {
- logger.warn("property should not null");
- return null;
- }
- StringBuffer buffer = new StringBuffer(property);
- for (int i = 0; i < buffer.length(); i++) {
- char c = buffer.charAt(i);
- if (c >= 'A' && c <= 'Z') {
- //插入'_'之后,位置+1
- buffer.insert(i++, '_');
- }
- }
- return buffer.toString();
- }
- public static String getProperty(String column) {
- if (StringUtils.isBlank(column)) {
- logger.warn("column should not null");
- return null;
- }
- return column.replaceAll("_", "");
- }
- }
本文转自 zhouhaipeng 51CTO博客,原文链接:http://blog.51cto.com/tianya23/716079,如需转载请自行联系原作者