Saturday, June 15, 2013
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>.
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);
}
}
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);
}
}
Saturday, January 26, 2013
Soundgarden setlist @ Orpheum Theatre, Boston, MA in 2013
Searching With My Good Eye Closed
Spoonman
Jesus Christ Pose
Outshined
By Crooked Steps
Taree
My Wave
Been Away Too Long
The Day I Tried to Live
Blow Up the Outside World
Fell on Black Days
Ugly Truth
Drawing Flies
Hunted Down
Non-State Actor
Blood on the Valley Floor
Mailman
A Thousand Days Before
Burden in My Hand
Head Down
Loud Love
4th of July
Encore:
Incessant Mace
Rusty Cage
Black Hole Sun
Slaves & Bulldozers
source: www.setlist.fm
Spoonman
Jesus Christ Pose
Outshined
By Crooked Steps
Taree
My Wave
Been Away Too Long
The Day I Tried to Live
Blow Up the Outside World
Fell on Black Days
Ugly Truth
Drawing Flies
Hunted Down
Non-State Actor
Blood on the Valley Floor
Mailman
A Thousand Days Before
Burden in My Hand
Head Down
Loud Love
4th of July
Encore:
Incessant Mace
Rusty Cage
Black Hole Sun
Slaves & Bulldozers
source: www.setlist.fm
Saturday, January 12, 2013
Ibatis return custom java object from query as List
Using IBatis if a sql query returns a number of rows and you need to IBatis to convert these rows to a java Object then you need to
1) create a resultMap entry where you map the propery in the java object to a column in a table
2) create a statement which contains the query and specify a resultMap (This is has been defined by above step 1)
using the above two configurations, Ibatis converts each row into a new MyClass() object and then populates it with the data from that row.
java code snippet
Map parameterMap = new HashMap();
parameterMap.put("inputNames", "abc");
List nameList = (List) getSqlMapClientTemplate().queryForList("getEmpDetails", parameterMap);
ibatis config
<sqlMap namespace="myQueries">
<resultMap id="idMap" class="com.test.MyClass">
<result property="myId" column="ID_COLUMN_IN_DATABASE_TABLE"/>
<result property="name" column="NAME_COLUMN_IN_DATABASE_TABLE"/>
</resultMap>
<statement id="getEmpDetails" resultMap="idMap"
parameterClass="java.util.Map">
select mytable.ID_COLUMN_IN_DATABASE_TABLE, mytable.NAME_COLUMN_IN_DATABASE_TABLE
from mytable
where mytable.name = #inputNames#
</statement>
</sqlMap>
1) create a resultMap entry where you map the propery in the java object to a column in a table
2) create a statement which contains the query and specify a resultMap (This is has been defined by above step 1)
using the above two configurations, Ibatis converts each row into a new MyClass() object and then populates it with the data from that row.
java code snippet
Map
parameterMap.put("inputNames", "abc");
List
ibatis config
<sqlMap namespace="myQueries">
<resultMap id="idMap" class="com.test.MyClass">
<result property="myId" column="ID_COLUMN_IN_DATABASE_TABLE"/>
<result property="name" column="NAME_COLUMN_IN_DATABASE_TABLE"/>
</resultMap>
<statement id="getEmpDetails" resultMap="idMap"
parameterClass="java.util.Map">
select mytable.ID_COLUMN_IN_DATABASE_TABLE, mytable.NAME_COLUMN_IN_DATABASE_TABLE
from mytable
where mytable.name = #inputNames#
</statement>
</sqlMap>
Using XStream to set xmlns attribute (xml namespace)
Thoughtworks xstream does not have an API to add xmlns attribute to xml root element. There is a work around to do the same using their useAttributeFor() API. Its a bit of hack but serves the purpose.
java pojo
public class TestXmlns {
private String name;
private String address;
private String xmlns;
...get/set for above attributes.
}
create an instance of xstream and add below configuration
xstream.useAttributeFor(TestXmlns.class, "xmlns");
Example:
TestXmlns x1 = new TestXmlns();
x1.setName("abc");
x1.setAddress("zyx");
z1.setXmlns("http://com.company.mytest");
XStream xstream = new XStream();
xstream.useAttributeFor(TestXmlns.class, "xmlns");
String xmlString = xstream.toXML(x1);
output
<TestXmlns xmlns="http://com.company.mytest">
<name>abc</name>
<address>xyz</address>
</TestXmlns>
java pojo
public class TestXmlns {
private String name;
private String address;
private String xmlns;
...get/set for above attributes.
}
create an instance of xstream and add below configuration
xstream.useAttributeFor(TestXmlns.class, "xmlns");
Example:
TestXmlns x1 = new TestXmlns();
x1.setName("abc");
x1.setAddress("zyx");
z1.setXmlns("http://com.company.mytest");
XStream xstream = new XStream();
xstream.useAttributeFor(TestXmlns.class, "xmlns");
String xmlString = xstream.toXML(x1);
output
<TestXmlns xmlns="http://com.company.mytest">
<name>abc</name>
<address>xyz</address>
</TestXmlns>
Convert java.util.Date to xsd:dateTime xml format
When converting java.util.Date to xml xsd dateTime format, one of the approaches is to use XMLGregorianCalendar class to format it to xml xsd format specifications.
input
new java.util.Date()
output
2013-01-12T10:30:09.274-05:00
code snippet:
String convertedDate = "";
GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("EST"));
//use below line to pass in a java.util.Date as an argument
//gc.setTime(date);
try {
convertedDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc).toXMLFormat();
} catch (DatatypeConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
input
new java.util.Date()
output
2013-01-12T10:30:09.274-05:00
code snippet:
String convertedDate = "";
GregorianCalendar gc = new GregorianCalendar(TimeZone.getTimeZone("EST"));
//use below line to pass in a java.util.Date as an argument
//gc.setTime(date);
try {
convertedDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc).toXMLFormat();
} catch (DatatypeConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Subscribe to:
Comments (Atom)
