mybatis根据property获取column
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的更多相关文章
- mybatis中association的column传入多个参数值
顾名思义,association是联合查询. 在使用association中一定要注意几个问题.文笔不好,白话文描述一下. 1: <association property="fncg ...
- Mybatis 学习---${ }与#{ }获取输入参数的区别、Foreach的用法
一.Mybatis中用#{}和${}获取输入参数的区别 1.“#{}“和“${}”都可以从接口输入中的map对象或者pojo对象中获取输入的参数值.例如 <mapper namespace=&q ...
- Mybatis的ResultMap对column和property
首先,先看看这张图,看能不能一下看明白: select元素有很多属性(这里说用的比较多的): id:命名空间唯一标识,可以被用来引用这条语句 parameterType:将会传入这条语句的参数类的 ...
- mybatis连接数据库出错获取不到SQLsession
采用mybatis连接数据库时候出现的问题描述: 数据库连接配置正确,mybatis-config数据库等部分配置均正确,连接数据库是OK的 <properties resource=" ...
- Mybatis按顺序获取数据
sql语句select * from producttg where hospitalcode in (1,2,3) 获取到的数据并不是按照条件1,2,3的顺序排列,如果要成下面形式(mybatis ...
- mybatis整合spring获取配置文件信息出错
描述:mybatis整合spring加载jdbc.properties文件,然后使用里面配置的值来 配置数据源,后来发现用户变成了admin- jdbc.properties的配置: 加载配置: 报错 ...
- mybatis拦截器获取sql
mybatis获取sql代码 package com.icourt.alpha.log.interceptor; import org.apache.ibatis.executor.Executor; ...
- Mybatis根据配置文件获取session(多数据源)
1.config.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configura ...
- MyBatis 批量插入获取自增 id 问题解决
问题: 通过 MyBatis 进行批量插入时,如果我们想获取插入对象所对应的主键 id,做法是直接在 <insert> 标签中直接加入 useGeneratedKeys.keyProper ...
随机推荐
- http://www.cnblogs.com/xdp-gacl/p/3951952.html
http://www.cnblogs.com/xdp-gacl/p/3951952.html http://www.cnblogs.com/kristain/articles/2409021.html
- Class类文件结构、类加载机制以及字节码执行
一.Class类文件结构 Class类文件严格按照顺序紧凑的排列,由无符号数和表构成,表是由多个无符号数或其他数据项构成的符合数据结构. Class类文件格式按如下顺序排列: 类型 名称 数量 u ...
- android使用webview上传文件(支持相册和拍照)
老夫最近需要做一个项目,需要调用服务器段的一些网页来选择文件,刚开始还挺纠结的,不知从何下手,网上大致预览了大神们走过的路,他们传统的方式都是使用一下代码: public void openFileC ...
- perl静态编译DBD
编译DBD 项目中经常使用perl,但perl在连接数据库时,需要依赖DBI,DBD驱动,但默认安装DBD驱动时,需要依赖数据库的lib库. 比如perl连接MySQL,需要安装MySQL clien ...
- jmap(Memory Map For Java)
功能 jmap(Memory Map For Java)命令用于生成堆转储快照(一般称为heaphump或dump文件).如果不使用jmap命令,要想获取Java堆转储快照还有一些比较“暴力”的手 ...
- POJ 3107
#include<iostream> #include<cstdio> #include<cstring> #include<string> #incl ...
- HDU-3790 最短路径问题
最短路径问题 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
- SDPLR的安装过程(matlab)
SDPLR 半正定规划优化工具的安装过程很简单,只要按照SDPLR 1.03-beta User's Guide (short version).pdf的介绍安装就可以. 运行在下载的工具包目录里运行 ...
- ubuntu adb devices 找不到任何东西,安装驱动
在Android平台下做开发,adb总是需要使用到的,同时,因为linux没有windows这样操作傻瓜化,有些东西还是需要自行设置的,否则将会连接不上. 关于这些内容,google也有一定的描述,可 ...
- Installing Windows Identity Foundation on Windows 8 - The Certificate for the signer of the message is invalid or not found.
Just a very quick note here, in case you’re struggling to get Windows Identity Foundation installed ...