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. win7怎么调屏幕自动休眠时间

    win7怎么调屏幕自动休眠时间 2013-03-28 17:13匿名 | 分类:Windows | 浏览1327次 我也不知道怎么说 我的电脑的问题就是 电脑放那不动过2,3分钟屏幕就暗了 要是不动过 ...

  2. 基础算法(搜索):NOIP 2015 斗地主

    Description 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3& ...

  3. SVN服务器及客户端的使用

    Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上了,下载地址:http://s ...

  4. 你认为A和B所在方格颜色相同吗?

    [你认为A和B所在方格颜色相同吗? ]据说全世界只有0.003%的人和photoshop能看出它们的颜色是相同的. 我属于那 99.9997% 的人...因为我不是神...

  5. Oracle11g客户端for centos 5.5安装文档

    前提:安装centos的时候,最好把系统的所有的安装包都安装上.以下命令需手动输入,不要复制粘贴. 1.以root用户登录centos5.5的操作系统:创建oracle用户组和用户. groupadd ...

  6. SQL语句 DML,DDL,DCL(转载)

    数据控制语言(DCL)是用来设置或者更改数据库用户或角色权限的语句,这些语句包括GRANT.DENY.REVOKE等语句,在默认状态下,只有 sysadmin.dbcreator.db_owner或d ...

  7. (转载)VC/MFC 工具栏上动态添加组合框等控件的方法

    引言 工具条作为大多数标准的Windows应用程序的 一个重要组成部分,使其成为促进人机界面友好的一个重要工具.通过工具条极大方便了用户对程序的操作,但是在由Microsoft Visual C++开 ...

  8. JavaScript高级程序设计32.pdf

    样式 在HTML中定义样式的方式有3种:通过<link/>元素包含外部样式表文件.使用<style/>元素定义嵌入式样式,以及使用style特性定义针对特定元素的样式,“DOM ...

  9. 忘记gitlab的root密码

    甚至也忘记了邮箱,或许是默认邮箱无法使用. 需要在服务器上面直接修改数据: gitlab-rails console production user = User.where().first user ...

  10. PHP两个字符串比较(人为出错),两字符串类型和数据表面相等,但strcmp()结果不为0

    PHP中,比较两个字符串是否相等用:strcmp(): PHP strcmp() 函数 PHP String 函数 定义和用法 strcmp() 函数比较两个字符串. 该函数返回: 0 - 如果两个字 ...