rose jade框架 sql中的字段和实体类中的字段的映射关系
来源:互联网 发布:网络ip地址冲突 编辑:程序博客网 时间:2024/05/18 08:21
DAO
如果代码中有 下划线进行分割的化 实体类中需要用 大写字母 进行分割
@SQL("SELECT ID,CITY_ID,CITY_NAME,STORE_ID,STORE_CODE,STORE_NAME,PAY_CODE as PAYCODE FROM BOSS_FINANCE_STORE_CODE WHERE CITY_ID=:cityId LIMIT 1 ") public BossFinanceStoreCodeVO selectBossFinanceStoreCodeByCityId(@SQLParam("cityId") Integer cityId);
实体类
public class BossFinanceStoreCodeVO { private Integer id; private Integer cityId; // 城市ID private String cityName; // 城市名 private String storeId; // 门店ID private String storeCode; // 门店代码 private String storeName; // 门店NAME private String paycode;//门店支付码
如果代码中有 下划线进行分割的化 实体类中需要用 大写字母 进行分割
如果没有分割的化 sql中的字段和实体类中的字段一致就可以了 大小没关系的
如果有数字的化 例如
列user_1_name_2的列能映射到user1Name2属性
源码:
private String[] underscoreName(String camelCaseName) { StringBuilder result = new StringBuilder(); if (camelCaseName != null && camelCaseName.length() > 0) { result.append(camelCaseName.substring(0, 1).toLowerCase()); for (int i = 1; i < camelCaseName.length(); i++) { char ch = camelCaseName.charAt(i); if (Character.isUpperCase(ch)) { result.append("_"); result.append(Character.toLowerCase(ch)); } else { result.append(ch); } } } String name = result.toString(); // 当name为user1_name2时,使name2为user_1_name_2 // 这使得列user_1_name_2的列能映射到user1Name2属性 String name2 = null; boolean digitFound = false; for (int i = name.length() - 1; i >= 0; i--) { if (Character.isDigit(name.charAt(i))) { // 遇到数字就做一个标识并continue,直到不是时才不continue digitFound = true; continue; } // 只有上一个字符是数字才做下划线 if (digitFound && i < name.length() - 1 && i > 0) { if (name2 == null) { name2 = name; } name2 = name2.substring(0, i + 1) + "_" + name2.substring(i + 1); } digitFound = false; } return new String[] { name, name2 }; }
protected void initialize() { this.mappedFields = new HashMap<String, PropertyDescriptor>(); PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(mappedClass); if (checkProperties) { mappedProperties = new HashSet<String>(); } for (int i = 0; i < pds.length; i++) { PropertyDescriptor pd = pds[i]; // patch for thrift module if (pd.getWriteMethod() == null) { try { if (!pd.getName().toLowerCase().startsWith("set") && !pd.getName().toLowerCase().startsWith("class")) { String writeMethodName = "set" + pd.getName().substring(0, 1).toUpperCase() + pd.getName().substring(1); Method writeMethod = mappedClass.getMethod(writeMethodName, pd.getPropertyType()); pd.setWriteMethod(writeMethod); } } catch (Exception e) { } } if (pd.getWriteMethod() != null) { if (checkProperties) { this.mappedProperties.add(pd.getName()); } this.mappedFields.put(pd.getName().toLowerCase(), pd); for (String underscoredName : underscoreName(pd.getName())) { if (underscoredName != null && !pd.getName().toLowerCase().equals(underscoredName)) { this.mappedFields.put(underscoredName, pd); } } } } }
public Object mapRow(ResultSet rs, int rowNumber) throws SQLException { // spring's : Object mappedObject = BeanUtils.instantiateClass(this.mappedClass); // jade's : private Object instantiateClass(this.mappedClass); // why: 经过简单的笔记本测试,mappedClass.newInstrance性能比BeanUtils.instantiateClass(mappedClass)快1个数量级 Object mappedObject = instantiateClass(this.mappedClass); BeanWrapper bw = new BeanWrapperImpl(mappedObject); ResultSetMetaData rsmd = rs.getMetaData(); int columnCount = rsmd.getColumnCount(); boolean warnEnabled = logger.isWarnEnabled(); boolean debugEnabled = logger.isDebugEnabled(); Set<String> populatedProperties = (checkProperties ? new HashSet<String>() : null); for (int index = 1; index <= columnCount; index++) { String column = JdbcUtils.lookupColumnName(rsmd, index).toLowerCase(); PropertyDescriptor pd = this.mappedFields.get(column); if (pd != null) { try { Object value = JdbcUtils.getResultSetValue(rs, index, pd.getPropertyType()); if (debugEnabled && rowNumber == 0) { logger.debug("Mapping column '" + column + "' to property '" + pd.getName() + "' of type " + pd.getPropertyType()); } bw.setPropertyValue(pd.getName(), value); if (populatedProperties != null) { populatedProperties.add(pd.getName()); } } catch (NotWritablePropertyException ex) { throw new DataRetrievalFailureException("Unable to map column " + column + " to property " + pd.getName(), ex); } } else { if (checkColumns) { throw new InvalidDataAccessApiUsageException("Unable to map column '" + column + "' to any properties of bean " + this.mappedClass.getName()); } if (warnEnabled && rowNumber == 0) { logger.warn("Unable to map column '" + column + "' to any properties of bean " + this.mappedClass.getName()); } } } if (populatedProperties != null && !populatedProperties.equals(this.mappedProperties)) { throw new InvalidDataAccessApiUsageException( "Given ResultSet does not contain all fields " + "necessary to populate object of class [" + this.mappedClass + "]: " + this.mappedProperties); } return mappedObject; }
0 0
- rose jade框架 sql中的字段和实体类中的字段的映射关系
- ibatis框架,数据库的字段和实体类的字段映射
- 使用Mybatis时实体类属性名和表中的字段名不一致导致结果无法映射到实体类的解决办法
- hibernate中的实体类和实体关系映射文件必须遵循的原则
- Spring 注解,将实体bean上的某个属性,不与db 表中的字段映射
- Mybatis总结《四》实体类和数据库中的字段不对应时的处理
- jQuery easyui dataGrid 动态改变排序字段名,一般情况下,在使用的时候,我们会点击相应字段进行排序,这里以JAVA为例,后端的实体类字段有可能和数据库的字段不一致; 如:实体类中的
- C#中的类的“引用字段”和“非引用字段”
- NHibernate映射关系表(实体映射文件字段的属性对应的NHibernate类型)
- sql 字段中的百分号
- sql中的一些字段
- 根据实体类中的多个字段的值,对实体类进行排序
- 开发日志:hibernate映射,使表中的一个字段对应实体中的多个属性
- Spring2.5 BeanPropertyRowMapper ResultSet和实体类的字段进行实现自动映射
- TP框架的字段映射
- SQL的计算字段(非表中的自然字段)
- ibatis文件中的实体的属性和表的字段,配反了,
- sql语句中的字段的类型问题
- spring的多个AOP的优先级
- C语言--字符串操作总结
- CMake 入门实战
- PCIe bus Max_Read_Request_Size Max_Payload_Size
- 基于TCPClient的客户端服务器的简单通信demo
- rose jade框架 sql中的字段和实体类中的字段的映射关系
- gdb so 加载
- Linux音频设备驱动_ALSA驱动框架的转载链接
- Activiti部署模型乱码
- WIN32学习——定时器消息
- Spring Boot Tomcate配置 (SSL配置)
- 解决oracle表锁死
- 李白买酒的递归求法
- Java实现的二分查找算法