Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sqlMap
  3. PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
  4. "http://ibatis.apache.org/dtd/sql-map-2.dtd">
  5.  
  6. <sqlMap namespace="Contact">
  7. <!--- Showing all data of table -->
  8. <select id="getAll" resultClass="com.nik.Contact">
  9. select * from contact
  10. <dynamic prepend="where ">
  11. salary like '%'
  12. <isNotNull property="orderby" >
  13. order by #orderby#, #orderby2#
  14. </isNotNull>
  15. </dynamic>
  16. </select>
  17. </sqlMap>
  18.  
  19. package com.nik;
  20.  
  21. public class Contact {
  22. private String firstName;
  23. private String lastName;
  24. private String email;
  25. private String salary;
  26. private String mobile;
  27. private String orderby;
  28. private String orderby2;
  29. private int id;
  30.  
  31. public Contact() {}
  32.  
  33. public Contact(
  34. String firstName,
  35. String lastName,
  36. String email) {
  37. this.firstName = firstName;
  38. this.lastName = lastName;
  39. this.email = email;
  40. }
  41.  
  42. public String getEmail() {
  43. return email;
  44. }
  45. public void setEmail(String email) {
  46. this.email = email;
  47. }
  48. public String getFirstName() {
  49. return firstName;
  50. }
  51. public void setFirstName(String firstName) {
  52. this.firstName = firstName;
  53. }
  54. public int getId() {
  55. return id;
  56. }
  57. public void setId(int id) {
  58. this.id = id;
  59. }
  60. public String getLastName() {
  61. return lastName;
  62. }
  63. public void setLastName(String lastName) {
  64. this.lastName = lastName;
  65. }
  66.  
  67. public String getSalary() {
  68. return salary;
  69. }
  70.  
  71. public void setSalary(String salary) {
  72. this.salary = salary;
  73. }
  74.  
  75. public String getMobile() {
  76. return mobile;
  77. }
  78.  
  79. public void setMobile(String mobile) {
  80. this.mobile = mobile;
  81. }
  82.  
  83. public String getOrderby() {
  84. return orderby;
  85. }
  86.  
  87. public void setOrderby(String orderby) {
  88. this.orderby = orderby;
  89. }
  90.  
  91. public String getOrderby2() {
  92. return orderby2;
  93. }
  94.  
  95. public void setOrderby2(String orderby2) {
  96. this.orderby2 = orderby2;
  97. }
  98. }
  99.  
  100. package com.nik;
  101.  
  102. import com.ibatis.common.resources.Resources;
  103. import com.ibatis.sqlmap.client.SqlMapClient;
  104. import com.ibatis.sqlmap.client.SqlMapClientBuilder;
  105. import java.io.*;
  106. import java.sql.SQLException;
  107. import java.util.*;
  108.  
  109. import org.apache.log4j.Logger;
  110.  
  111. public class IbatisExample{
  112. public static void main(String[] args)
  113. throws IOException,SQLException{
  114. // get a logger instance named "com.foo"
  115. Logger logger = Logger.getLogger("com.nik");
  116. Reader reader = Resources.getResourceAsReader("ibatis-config.xml");
  117. SqlMapClient sqlMap =
  118. SqlMapClientBuilder.buildSqlMapClient(reader);
  119. //Output all contacts
  120. System.out.println("All Contacts");
  121. Contact c1 = new Contact();
  122. c1.setOrderby("salary");
  123. c1.setOrderby2("mobile");
  124. List<Contact> contacts = (List<Contact>)
  125. sqlMap.queryForList("Contact.getAll",c1);
  126. Contact contact = null;
  127. for (Contact c : contacts) {
  128. System.out.print(" " + c.getId());
  129. System.out.print(" " + c.getFirstName());
  130. System.out.print(" " + c.getLastName());
  131. System.out.print(" " + c.getEmail());
  132. System.out.print(" " + c.getSalary());
  133. System.out.print(" " + c.getMobile());
  134. contact = c;
  135. System.out.println("");
  136. }
  137. }
  138. }
  139.  
  140. All Contacts
  141. DEBUG [main] - Created connection 71786792.
  142. DEBUG [main] - {conn-100000} Connection
  143. DEBUG [main] - {pstm-100001} PreparedStatement: select * from contact where salary like '%' order by ?, ?
  144. DEBUG [main] - {pstm-100001} Parameters: [salary, mobile]
  145. DEBUG [main] - {pstm-100001} Types: [java.lang.String, java.lang.String]
  146. DEBUG [main] - {rset-100002} ResultSet
  147. DEBUG [main] - {rset-100002} Header: [id, firstName, lastName, email, salary, mobile]
  148. DEBUG [main] - {rset-100002} Result: [1, abc, 111, abc@hyahoo.com, 5000, 400]
  149. DEBUG [main] - {rset-100002} Result: [2, def, 222, def@yahoo.com, 2000, 100]
  150. DEBUG [main] - {rset-100002} Result: [3, xyz, 333, xyz@yahoo.com, 3000, 300]
  151. DEBUG [main] - Returned connection 71786792 to pool.
  152. 1 abc 111 abc@hyahoo.com 5000 400
  153. 2 def 222 def@yahoo.com 2000 100
  154. 3 xyz 333 xyz@yahoo.com 3000 300
  155.  
  156. All Contacts
  157. DEBUG [main] - Created connection 71786792.
  158. DEBUG [main] - {conn-100000} Connection
  159. DEBUG [main] - {pstm-100001} PreparedStatement: select * from contact where salary like '%' order by salary, mobile
  160. DEBUG [main] - {pstm-100001} Parameters: []
  161. DEBUG [main] - {pstm-100001} Types: []
  162. DEBUG [main] - {rset-100002} ResultSet
  163. DEBUG [main] - {rset-100002} Header: [id, firstName, lastName, email, salary, mobile]
  164. DEBUG [main] - {rset-100002} Result: [2, def, 222, def@yahoo.com, 2000, 100]
  165. DEBUG [main] - {rset-100002} Result: [3, xyz, 333, xyz@yahoo.com, 3000, 300]
  166. DEBUG [main] - {rset-100002} Result: [1, abc, 111, abc@hyahoo.com, 5000, 400]
  167. DEBUG [main] - Returned connection 71786792 to pool.
  168. 2 def 222 def@yahoo.com 2000 100
  169. 3 xyz 333 xyz@yahoo.com 3000 300
  170. 1 abc 111 abc@hyahoo.com 5000 400
  171.  
  172. public void setOrderBy(String orderBy) {
  173. if (orderBy.equals("name") or orderBy.equals(" salary") or orderBy.equals("dept")) {
  174. this.orderBy = orderBy;
  175. } else {
  176. this.orderBy = NULL;
  177. }
  178. }
  179.  
  180. <?xml version="1.0" encoding="UTF-8"?>
  181. <!DOCTYPE sqlMap
  182. PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
  183. "http://ibatis.apache.org/dtd/sql-map-2.dtd">
  184.  
  185. <sqlMap namespace="Contact">
  186. <!--- Showing all data of table -->
  187. <select id="getAll" resultClass="com.nik.Contact" parameterClass="java.util.Map">
  188. select * from contact where salary like '%'
  189.  
  190. <dynamic prepend="order by">
  191. <isEqual
  192. property="orderby"
  193. compareValue="salary">
  194. salary,
  195. </isEqual>
  196. <isEqual
  197. property="orderby"
  198. compareValue="mobile">
  199. mobile,
  200. </isEqual>
  201. <isEqual
  202. property="orderby2"
  203. compareValue="salary">
  204. salary
  205. </isEqual>
  206. <isEqual
  207. property="orderby2"
  208. compareValue="mobile">
  209. mobile
  210. </isEqual>
  211. </dynamic>
  212. </select>
  213. </sqlMap>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement