动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦。
使用动态 SQL 并非一件易事,但借助可用于任何 SQL 映射语句中的强大的动态 SQL 语言,MyBatis 显著地提升了这一特性的易用性。<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<cache
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> eviction="FIFO"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> flushInterval="60000"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> size="512"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> readOnly="true"
/> <transactionManager type="JDBC"/>
<cache
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> eviction="FIFO"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> flushInterval="60000"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> size="512"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> readOnly="true"
/> <dataSource type="POOLED">
<properties resource="db.properties"></properties><property name="driver" value="${driver}"/>
<properties resource="db.properties"></properties><property name="url" value="${url}"/>
<properties resource="db.properties"></properties><property name="username" value="${username}"/>
<properties resource="db.properties"></properties><property name="password" value="${password}"/>
<cache
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> eviction="FIFO"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> flushInterval="60000"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> size="512"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> readOnly="true"
/> </dataSource>
</environment>
</environments>
<mappers>
<mapper resource="你自己配置的xml文件路径"/>
</mappers>
</configuration><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<cache
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> eviction="FIFO"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> flushInterval="60000"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> size="512"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> readOnly="true"
/> <transactionManager type="JDBC"/>
<cache
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> eviction="FIFO"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> flushInterval="60000"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> size="512"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> readOnly="true"
/> <dataSource type="POOLED">
<properties resource="db.properties"></properties><property name="driver" value="${driver}"/>
<properties resource="db.properties"></properties><property name="url" value="${url}"/>
<properties resource="db.properties"></properties><property name="username" value="${username}"/>
<properties resource="db.properties"></properties><property name="password" value="${password}"/>
<cache
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> eviction="FIFO"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> flushInterval="60000"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> size="512"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> readOnly="true"
/> </dataSource>
</environment>
</environments>
<mappers>
<mapper resource="你自己配置的xml文件路径"/>
</mappers>
</configuration>
<properties resource="db.properties"></properties>
<properties resource="db.properties"></properties>
<properties resource="db.properties"></properties>
<properties resource="db.properties"></properties>
<properties resource="db.properties"></properties>
<cache
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> eviction="FIFO"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> flushInterval="60000"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> size="512"
<cache type="org.mybatis.caches.ehcache.EhcacheCache"/> readOnly="true"
/> -Mybatis官网
if新建一个工程用于接下来学习(导入jar包和修改配置文件略):
choose(when,otherwise)
trim(where,set)
foreach
where 元素只会在子元素返回任何内容的情况下才插入 “WHERE” 子句。而且,若子句的开头为 “AND” 或 “OR”,where 元素也会将它们去除。大白话就是,如果只有一个查询条件,会自动去掉条件前面的and。如果子句前面有and/or,而且前面无其他条件时,也会去掉。
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |