之前文章中对in的用法做过讲解:《MyBatis(四):mybatis中使用in查询时的注意事项》
实际上对于多个参数的用法也是这是注意的:
多参&if判空&List集合判空&in用法
@Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000) @Select(value = { "<script>", " SELECT `id`,`title` ", " FROM `tb_article` ", " WHERE `category_id`=#{article.categoryId} ", " <if test='article.status!=null'>", " AND `status` = #{article.status} ", " </if>", " <if test='typeList!=null and !typeList.isEmpty()'>", " and `article_type` in", " <foreach collection=\"typeList\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">", " #{item} ", " </foreach>", " </if>", "</script>" }) @ResultMap(value = {"articleResultMap"}) List<ArticlePo> queryByCondition(final @Param("article") ArticleModel article, final @Param("typeList") List<Integer> typeList);
1)上边主要对普通参数判断空用法:<if test='article.status!=null'>
2)集合判空的用法:<if test='typeList!=null and !typeList.isEmpty()'>
3)in的用法:<foreach collection=\"typeList\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\">";
4)多参数用法,实际上多个参数如果使用@SqlProvider方式是,在ArticleSqlProvider的类中方法中接收的参数对象为Map<String,Object>,该map集合中包含两个对象:key:article的ArticleModel对象;key:typeList的List<Integer>对象。获取方式:ArticleModel aritlce=(ArticleModel)map.get("aritcle");List<Integer> typeList=(List<Integer>)map.get("typeList");。
Mybatis使用POJO传递参数:
@Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000) @ResultMap("logResult") @Select(value={ "<script>", "select * from `log` " , "<where>" , " <if test=\"title!=null and title!=''\">" , " and `title` like CONCAT('%', #{title}, '%') " , " </if>" , " <if test=\"moduleType!=null \">" , " and `module_type`=#{moduleType} " , " </if>" , " <if test=\"operateType!=null \">" , " and `operate_type`=#{operateType} " , " </if>" , "</where>", "</script>" }) List<Log> getByPojo(Log log);
src/main/resources/mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 引用db.properties配置文件 --> <properties resource="jdbc.properties"/> <!--配置全局属性--> <settings> <!-- 打开延迟加载的开关 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 将积极加载改为消极加载(即按需加载) --> <setting name="aggressiveLazyLoading" value="false"/> <!-- 打开全局缓存开关(二级缓存)默认值就是 true --> <setting name="cacheEnabled" value="true"/> <!--使用jdbc的getGeneratekeys获取自增主键值--> <setting name="useGeneratedKeys" value="true"/> <!--使用列别名替换别名 默认true select name as title form table; --> <setting name="useColumnLabel" value="true"/> <!--开启驼峰命名转换--> <setting name="mapUnderscoreToCamelCase" value="true"/> <!--打印sql日志--> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> <typeAliases> <package name="com.dx.test.model"/> </typeAliases> <!-- 元素类型为 "configuration" 的内容必须匹配 " (properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?, plugins?,environments?,databaseIdProvider?,mappers?)"。 --> <typeHandlers> <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.dx.test.model.enums.ModuleType"/> <typeHandler handler="org.apache.ibatis.type.EnumOrdinalTypeHandler" javaType="com.dx.test.model.enums.OperateType"/> </typeHandlers> <!-- 对事务的管理和连接池的配置 --> <environments default="mysql_jdbc"> <environment id="mysql_jdbc"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${name}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- <mappers> <mapper resource="resources/mapper/LogMapper.xml"/> </mappers> --> <mappers> <mapper class="com.dx.test.dao.LogMapper"></mapper> </mappers> </configuration>