mybatis根据property获取column

mybatis根据类的属性获取xml文件中对应的column

mybatis获取xml文件中property对应的column

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

蕃薯耀 2016年4月29日 15:44:59 星期五

http://fanshuyao.iteye.com/

xml解析采用Dom4j(Dom4j使用详情见:http://fanshuyao.iteye.com/blog/2279679)

可以在附件中下载

package xxx.xxx.common.utils;

import java.util.Date;
import java.util.List; import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; public class XmlUtils { /**
* 根据类的属性名找表的列名(取一个的时候可以使用此方法)
* @param fileName 类对应的Mapper xml文件
* @param id 唯一的id
* <p>
* 如:resultMap id="BaseResultMap" type="com.chinagas.org.beans.User" 中的id
* </p>
* @param property 属性名(对应的Java对象属性名)
* @return
*/
public static String getMapperColumnByProperty(String fileName, String id, String property){
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(XmlUtils.class.getClassLoader().getResourceAsStream(fileName));
if(document != null){
Element root = document.getRootElement();
if(root != null){
@SuppressWarnings("unchecked")
List<Element> resultMaps = root.elements("resultMap");
for (Element resultMap : resultMaps) {
if(resultMap != null && resultMap.attributeValue("id").equals(id)){
@SuppressWarnings("unchecked")
List<Element> properties = resultMap.elements();
for (Element prop : properties) {
if(prop != null && prop.attributeValue("property").equals(property)){
return prop.attributeValue("column");
}
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 返回ResultMap对应Element对象(取2次以上的时候,建议先把Element对象找到,再根据此Element对象再去找column,效率高很多)
* @param fileName 类对应的Mapper xml文件
* @param id 唯一的id
* <p>
* 如:resultMap id="BaseResultMap" type="com.chinagas.org.beans.User" 中的id
* </p>
* @return
*/
public static Element getResultMapElement(String fileName, String id){
try {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(XmlUtils.class.getClassLoader().getResourceAsStream(fileName));
if(document != null){
Element root = document.getRootElement();
if(root != null){
@SuppressWarnings("unchecked")
List<Element> resultMaps = root.elements("resultMap");
for (Element resultMap : resultMaps) {
if(resultMap != null && resultMap.attributeValue("id").equals(id)){
return resultMap;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 在Element根据property找表的列名(和方法getResultMapElement()结合使用,多次取Column时效率高出很多倍)
* @param resultMapElement Mapper xml文件解析后得到的Element对象(方法:getResultMapElement())
* @param property 属性名(对应的Java对象属性名)
* @return
*/
public static String getMapperColumnByElement(Element resultMapElement, String property){
try {
if(resultMapElement != null){
@SuppressWarnings("unchecked")
List<Element> properties = resultMapElement.elements();
for (Element prop : properties) {
if(prop != null && prop.attributeValue("property").equals(property)){
return prop.attributeValue("column");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
} public static void main(String[] args) {
long startTime = new Date().getTime(); /*System.out.println(getMapperColumnByProperty("UserMapper.xml","BaseResultMap", "userName"));
System.out.println(getMapperColumnByProperty("UserMapper.xml","BaseResultMap", "loginName"));
System.out.println(getMapperColumnByProperty("UserMapper.xml","BaseResultMap", "orgName"));
System.out.println(getMapperColumnByProperty("UserMapper.xml","BaseResultMap", "sex"));*/ Element e = getResultMapElement("UserMapper.xml","BaseResultMap");
System.out.println(getMapperColumnByElement(e, "userName"));
System.out.println(getMapperColumnByElement(e, "loginName"));
System.out.println(getMapperColumnByElement(e, "orgName"));
System.out.println(getMapperColumnByElement(e, "sex")); long endTime = new Date().getTime();
System.out.println("所用的时间间隔是:"+ (endTime-startTime)); } }

 

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.

蕃薯耀 2016年4月29日 15:44:59 星期五

http://fanshuyao.iteye.com/

mybatis根据property获取column的更多相关文章

  1. mybatis中association的column传入多个参数值

    顾名思义,association是联合查询. 在使用association中一定要注意几个问题.文笔不好,白话文描述一下. 1: <association property="fncg ...

  2. Mybatis 学习---${ }与#{ }获取输入参数的区别、Foreach的用法

    一.Mybatis中用#{}和${}获取输入参数的区别 1.“#{}“和“${}”都可以从接口输入中的map对象或者pojo对象中获取输入的参数值.例如 <mapper namespace=&q ...

  3. Mybatis的ResultMap对column和property

    首先,先看看这张图,看能不能一下看明白:   select元素有很多属性(这里说用的比较多的): id:命名空间唯一标识,可以被用来引用这条语句 parameterType:将会传入这条语句的参数类的 ...

  4. mybatis连接数据库出错获取不到SQLsession

    采用mybatis连接数据库时候出现的问题描述: 数据库连接配置正确,mybatis-config数据库等部分配置均正确,连接数据库是OK的 <properties resource=" ...

  5. Mybatis按顺序获取数据

    sql语句select * from producttg where hospitalcode in (1,2,3)  获取到的数据并不是按照条件1,2,3的顺序排列,如果要成下面形式(mybatis ...

  6. mybatis整合spring获取配置文件信息出错

    描述:mybatis整合spring加载jdbc.properties文件,然后使用里面配置的值来 配置数据源,后来发现用户变成了admin- jdbc.properties的配置: 加载配置: 报错 ...

  7. mybatis拦截器获取sql

    mybatis获取sql代码 package com.icourt.alpha.log.interceptor; import org.apache.ibatis.executor.Executor; ...

  8. Mybatis根据配置文件获取session(多数据源)

    1.config.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configura ...

  9. MyBatis 批量插入获取自增 id 问题解决

    问题: 通过 MyBatis 进行批量插入时,如果我们想获取插入对象所对应的主键 id,做法是直接在 <insert> 标签中直接加入 useGeneratedKeys.keyProper ...

随机推荐

  1. 远程仓库版本回退方法 good

    1 简介 最近在使用git时遇到了远程分支需要版本回滚的情况,于是做了一下研究,写下这篇博客. 2 问题 如果提交了一个错误的版本,怎么回退版本? 如果提交了一个错误的版本到远程分支,怎么回退远程分支 ...

  2. VS2012中使用纯C实现COM的小问题

    用VS2012新建C++工程都预定义了宏__cplusplus,所以引用到的都是C++的定义.但是要用C实现的话,一般都是也就不是C++的了.比如以下代码: #undef INTERFACE #def ...

  3. 查看本机上的端口使用情况netstat -an

    1.查找本机上的端口使用情况 netstat -an 2.查找指定端口的使用情况 C:\Windows\System32>netstat -ano | find "8002" ...

  4. 在Raspberry配置优化安装LNMP环境总结

    在Raspberry配置优化安装LNMP环境总结 apt-get update apt-get install nginx apt-get install php5-fpm php5-cli php5 ...

  5. HTML5 Canvas核心技术—图形、动画与游戏开发.pdf7

    性能 运行putImageData()比drawImage()慢,同等条件下优先考虑drawImage() 操作图像数据需要遍历大量数据,应该注意几点: 1)避免在循环体中直接访问对象属性,应当保存在 ...

  6. 存储过程实例总结(开发中的错误与总结,调试,数据库函数DATEDIFF计算当前日期是否在本周内)

    USE [POND] GO /****** Object: StoredProcedure [dbo].[OrderChargeList] Script Date: 04/16/2014 13:32: ...

  7. ab apache Benchmarking中链接的写法 记得加上/

    C:\wamp\bin\apache\Apache2.2.21\bin>ab.exe -n5 -c5 http://www.baidu.com ab.exe: invalid URL Usage ...

  8. 鸟哥的Linux私房菜 第十八章、认识系统服务 (daemons)

    什么是 daemon 与服务 (service) Linux Daemon (守护进程)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些事件.它不需要用户输入就能运行 ...

  9. Day 1 @ RSA Conference Asia Pacific & Japan 2016

    # 国内出发 早上8:45的航班,首次从深圳机场乘坐国际航班(先前去日本.欧洲都从香港走),就提前了3个小时出发. 乘taxi到机场30分钟不到,135元.到了T3 4号出发口,发现check-in的 ...

  10. jQuery之事件even

    jQuery之事件       W3C:http://www.w3school.com.cn/jquery/jquery_ref_events.asp 一.事件列表 1.blur() 当失去焦点时触发 ...