Monday, March 25, 2013

Ibatis queryForList returning a List of String objects

Using an IBatis query that returns mutiple rows from the database, each row can be returned as an object and all these individual objects can be encompassed in a List.

query
<select id="getEmpNames" resultClass="java.lang.String">
        select names from emp_address where dept_id = #value#
    </select>
   
    Java code
    public class EmployeeDaoImpl extends SqlMapClientDaoSupport {
    ...
    ...
        public List<String> getEmpNames(Long deptId) {
       
         List<String> tempList = getSqlMapClientTemplate().queryForList("getEmpNames", deptId);
         return tempList;
         }
     ..
     }
   

For example if the above code snippet returns 5 rows from the database, IBatis internally constructs 5 String objects and returns a List<String>.

Friday, March 8, 2013

Convert java attributes to start with upperCase when being written to xml file using XStream

    if (xstream == null) {
                xstream = new XStream(){
                  
                    @Override
                    protected MapperWrapper wrapMapper(MapperWrapper next) {
                            return new UpperCaseMapper(next);
                    }
                };
            }
           
    private static class UpperCaseMapper extends MapperWrapper {
        public UpperCaseMapper(Mapper wrapped) {
                super(wrapped);
        }

        @Override
        public String serializedMember(Class type, String memberName) {
            String camelCase = memberName.substring(0, 1).toUpperCase() + memberName.substring(1);
            return super.serializedMember(type, camelCase);
        }
       
        @Override
        public String realMember(Class type, String serialized) {
                String camelCase = serialized.substring(0, 1).toLowerCase()
                                + serialized.substring(1);
                return super.realMember(type, camelCase);
        }
    }