mybatis通过将sql配置xml文件中,通过解析xml动态标签来实现动态sql
如下样例 xml文件
<?xml version = "1.0" ?> <!doctype script system "script-1.0.dtd"> <script namespace="user"> <common id="commonorder"> order by id desc </common> <sql id="queryuser"> select * from user <where> <if test='id != null '> id = #{id} </if> <if test="names != null and names.size() >0"> and name in <foreach collection="names" item="item" separator="," open="(" close=")"> ${item} </foreach> </if> </where> <ref id="commonorder"/> </sql> <sql id="updateuser"> update user set name = ${name} <where> <if test='id != null '> id = #{id} </if> </where> </sql> </script>
1.xml文件读取
xml标签编写规则
<!element script (#pcdata | sql | common)*> <!attlist script namespace cdata #required > <!element sql (#pcdata | trim | where | set | foreach | choose | if | ref)*> <!attlist sql id cdata #required > <!element common (#pcdata | trim | where | set | foreach | choose | if)*> <!attlist common id cdata #required > <!element ref (#pcdata)*> <!attlist ref id cdata #required > <!element trim (#pcdata | trim | where | set | foreach | choose | if)*> <!attlist trim prefix cdata #implied prefixoverrides cdata #implied suffix cdata #implied suffixoverrides cdata #implied > <!element where (#pcdata | trim | where | set | foreach | choose | if | ref)*> <!element set (#pcdata | trim | where | set | foreach | choose | if)*> <!element foreach (#pcdata | trim | where | set | foreach | choose | if)*> <!attlist foreach collection cdata #required item cdata #implied index cdata #implied open cdata #implied close cdata #implied separator cdata #implied > <!element choose (when* , otherwise?)> <!element when (#pcdata | trim | where | set | foreach | choose | if)*> <!attlist when test cdata #required > <!element otherwise (#pcdata | trim | where | set | foreach | choose | if)*> <!element if (#pcdata | trim | where | set | foreach | choose | if)*> <!attlist if test cdata #required >
documentbuilderfactory 是jdk自带的解析xml文件操作,位于 javax.xml.parsers 包,如图其是一个抽象类,因此无法被实例化
需要通过 newinstance 来实例化
实例化对象后的属性 setvalidating(true); 即通过xml的 验证规则进行xml格式验证
setnamespaceaware 设置忽略命名空间
对于xml文件的命名空间的定义,详见
private document buildxml(inputstream scriptfile) throws parserconfigurationexception, saxexception, ioexception { documentbuilderfactory factory = documentbuilderfactory .newinstance(); //默认情况下,解析器不验证文档。将这个参数设置为 true 可打开验证功能。 factory.setvalidating(true); //是否设置命名空间 factory.setnamespaceaware(false); //确定是否要忽略文件中的注释。其默认值为 false。 factory.setignoringcomments(true); //确定是否要忽略元素内容中的空白(类似于浏览器对待 html 的方式)。其默认值为 false。 factory.setignoringelementcontentwhitespace(false); //定解析器是否要将 cdata 节点转换为文本,以及是否要和周围的文本节点合并(如果适用的话)。其默认值为 false。 factory.setcoalescing(false); //确定是否要展开外部实体引用。如果为 true,外部数据将插入文档。其默认值为 true factory.setexpandentityreferences(true); documentbuilder builder = factory.newdocumentbuilder(); //设置验证规则文件 dtd文件 builder.setentityresolver(new entityresolver() { @override public inputsource resolveentity(string publicid, string systemid) throws saxexception, ioexception { return new inputsource(new classpathresource("script-1.0.dtd").getinputstream()); } }); //设置错误解析器 builder.seterrorhandler(new errorhandler() { @override public void error(saxparseexception exception) throws saxexception { throw exception; } @override public void fatalerror(saxparseexception exception) throws saxexception { throw exception; } @override public void warning(saxparseexception exception) throws saxexception { } }); return builder.parse(scriptfile); }
2.xml 文件解析
到此这篇关于mybatis动态sql实现逻辑代码详解的文章就介绍到这了,更多相关mybatis动态sql内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!