mybatis获取xml⽂件中property对应的column
mybatis根据property获取column
mybatis根据类的属性获取xml⽂件中对应的column
mybatis获取xml⽂件中property对应的column
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
蕃薯耀 2016年5⽉6⽇ 11:40:00 星期五
Java代码
1. utils;
2.
3. import java.util.Date;
4. import java.util.List;
5.
6. import org.dom4j.Document;
7. import org.dom4j.Element;
8. import org.dom4j.io.SAXReader;
9.
10.
11.
12. public class XmlUtils {
13.
14. /**
15. * 根据类的属性名表的列名(取⼀个的时候可以使⽤此⽅法)
16. * @param fileName 类对应的Mapper xml⽂件
17. * @param id 唯⼀的id
18. * <p>
19. * 如:resultMap id="BaseResultMap" type="beans.User" 中的id
20. * </p>
21. * @param property 属性名(对应的Java对象属性名)
22. * @return
23. */
24. public static String getMapperColumnByProperty(String fileName, String id, String property){
25. try {
26. SAXReader saxReader = new SAXReader();
27. Document document = ad(ClassLoader().getResourceAsStream(fileName));
28. if(document != null){
29. Element root = RootElement();
30. if(root != null){
31. @SuppressWarnings("unchecked")
32. List<Element> resultMaps = root.elements("resultMap");
33. for (Element resultMap : resultMaps) {
34. if(resultMap != null && resultMap.attributeValue("id").equals(id)){
35. @SuppressWarnings("unchecked")
36. List<Element> properties = resultMap.elements();
37. for (Element prop : properties) {
38. if(prop != null && prop.attributeValue("property").equals(property)){
39. return prop.attributeValue("column");
40. }
41. }
42. }
43. }
44. }
45. }
46. } catch (Exception e) {
47. e.printStackTrace();
48. }
49. return null;
50. }
51.
52. /**
53. * 返回ResultMap对应Element对象(取2次以上的时候,建议先把Element对象到,再根据此Element对象再去c
olumn,效率⾼很多)
54. * @param fileName 类对应的Mapper xml⽂件
55. * @param id 唯⼀的id
56. * <p>
57. * 如:resultMap id="BaseResultMap" type="beans.User" 中的id
58. * </p>
59. * @return
60. */
61. public static Element getResultMapElement(String fileName, String id){
62. try {
63. SAXReader saxReader = new SAXReader();
64. Document document = ad(ClassLoader().getResourceAsStream(fileName));
65. if(document != null){
66. Element root = RootElement();
67. if(root != null){
使用dom4j解析xml文件68. @SuppressWarnings("unchecked")
69. List<Element> resultMaps = root.elements("resultMap");
70. for (Element resultMap : resultMaps) {
71. if(resultMap != null && resultMap.attributeValue("id").equals(id)){
72. return resultMap;
73. }
74. }
75. }
76. }
77. } catch (Exception e) {
78. e.printStackTrace();
79. }
80. return null;
81. }
82.
83. /**
84. * 在Element根据property表的列名(和⽅法getResultMapElement()结合使⽤,多次取Column时效率⾼出很多倍
)
85. * @param resultMapElement Mapper xml⽂件解析后得到的Element对象(⽅法:getResultMapElement())
86. * @param property 属性名(对应的Java对象属性名)
87. * @return
88. */
89. public static String getMapperColumnByElement(Element resultMapElement, String property){
90. try {
91. if(resultMapElement != null){
92. @SuppressWarnings("unchecked")
93. List<Element> properties = resultMapElement.elements();
94. for (Element prop : properties) {
95. if(prop != null && prop.attributeValue("property").equals(property)){
96. return prop.attributeValue("column");
97. }
98. }
99. }
100. } catch (Exception e) {
101. e.printStackTrace();
102. }
103. return null;
104. }
105.
106.
107. public static void main(String[] args) {
108. long startTime = new Date().getTime();
109.
110. /*System.out.println(getMapperColumnByProperty("l","BaseResultMap", "userName"));
111. System.out.println(getMapperColumnByProperty("l","BaseResultMap", "loginName"));
112. System.out.println(getMapperColumnByProperty("l","BaseResultMap", "orgName"));
113. System.out.println(getMapperColumnByProperty("l","BaseResultMap", "sex"));*/
114.
115. Element e = getResultMapElement("l","BaseResultMap");
116. System.out.println(getMapperColumnByElement(e, "userName"));
117. System.out.println(getMapperColumnByElement(e, "loginName"));
118. System.out.println(getMapperColumnByElement(e, "orgName"));
119. System.out.println(getMapperColumnByElement(e, "sex"));
120.
121. long endTime = new Date().getTime();
122. System.out.println("所⽤的时间间隔是:"+ (endTime-startTime));
123.
124. }
125.
126.
127. }
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
蕃薯耀 2016年5⽉6⽇ 11:40:00 星期五
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论