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 ...
随机推荐
- mysqll 数据库相互堵塞问题
192.168.11.186 远程访问192.168.11.185 数据库 186上看到: centos6.5:/root#mysql -uroot -p'kjk123123' -h192.168.1 ...
- perl unload utf-8 oracle Wide character in print at unload_oracle.pl line 105.
#!/usr/bin/perl use DBI; use Encode; my $dbName = 'oadb'; my $dbUser = 'vxspace'; my $dbUserPass = ' ...
- VS2013 ASP.NET MVC 修改Web项目的IISExpress的端口固定
[首先]关闭防火墙,或防火墙开放端口 在解决方案中,右键某项目,属性——Web——服务器——选择IISExpress URL输入:http://localhost:8000/ 直接将8000更改 ...
- Specify a culture in string conversion explicitly
Specify a culture in string conversion explicitly There are different methods of grouping symbols, l ...
- hadoop2.2编程:序列化
测试序列化后的长度 提示:需要用到的类,以及继承关系如下: 1.java.lang.Object |__ java.io.OutputStream |__ java.io.ByteArrayOutpu ...
- Muduo源码库研究(笔记汇总)
声明: 本人学习Muduo源码, 有些代码会对其进行精简, 加上本人的一些理解, 所以与作者的代码可能有些不同. 如有理解错误的地方欢指出. Muduo基础库-时间戳类 http://www.cnbl ...
- The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
The server is temporarily unable to service your request due to maintenance downtime or capacity pro ...
- (转载)JS事件监听 JS:attachEvent和addEventListener使用方法
(转载)http://www.chhua.com/web-note146 attachEvent和addEventListener使用方法 Js代码 <html> <head> ...
- Text Reverse
Text Reverse Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- __str__
__str__是被print函数调用的,一般都是return一个什么东西.这个东西应该是以字符串的形式表现的.如果不是要用str()函数转换.当你打印一个类的时候,那么print首先调用的就是类里面的 ...