之前文章中对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>

src/main/resources/log.properties

log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

src/main/resources/jdbc.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
name=root
password=123456

pom.xml

        <!--MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--MySql数据库驱动 -->
<!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency> <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

LogMapper.java(Mybatis mapper类)

package com.dx.test.dao; 

import java.util.List;
import java.util.Map; import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update; import com.dx.test.dao.sqlprovider.LogSqlProvider;
import com.dx.test.model.Log;
import com.dx.test.model.enums.ModuleType;
import com.dx.test.model.enums.OperateType; @Mapper
public interface LogMapper {
/**
* 入库日志
*
* @param log 待入库实体
* @return 影响条数
*/
@Options(useCache = true, flushCache = Options.FlushCachePolicy.TRUE, useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
@InsertProvider(type = LogSqlProvider.class, method = "insert")
public int insert(Log log); /**
* 根据文章id,查询日志详情
*
* @param id 日志id
* @return 返回查询到的日志详情
*/
@Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000)
@Results(id = "logResult", value = {
@Result(property = "id", column = "id", id = true),
@Result(property = "title", column = "title"),
@Result(property = "content", column = "content"),
@Result(property = "moduleType", column = "module_type", javaType = ModuleType.class),
@Result(property = "operateType", column = "operate_type", javaType = OperateType.class),
@Result(property = "dataId", column = "data_id"),
@Result(property = "createUser", column = "create_user"),
@Result(property = "createUserId", column = "create_user_id"),
@Result(property = "createTime", column = "create_time")
})
@Select({ "select * from `log` where `id`=#{id}" })
Log getById(@Param("id") Long id); @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); @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> getByParameter(@Param("title") String title,@Param("moduleType") ModuleType moduleType,@Param("operateType") OperateType operateType); @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> getByMap(Map<String, Object> map); @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000)
@ResultMap("logResult")
@Select({
"<script>",
"select * from `log` " ,
"<where>" ,
" <choose> ",
" <when test=\"dataId!=null\">",
" and data_id=#{dataId}",
" </when>",
" <when test=\"id!=null\">",
" and id=#{id}",
" </when>",
" <otherwise>",
" and 1=1",
" </otherwise>",
" </choose>",
"</where>" ,
"</script>"})
List<Log> getList(final Log log); @Options(useCache = true, flushCache = Options.FlushCachePolicy.TRUE)
@Update({
"<script>",
"update `log` " ,
"<set>" ,
" <if test=\"dataId!=null\">",
" `data_id`=#{dataId},",
" </if>",
" <if test=\"title!=null\">",
" `title`=#{title},",
" </if>",
" <if test=\"content!=null\">",
" `content`=#{content} ",
" </if>",
"</set>" ,
" where id=#{id}",
"</script>"})
int update(final Log log); @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000)
@ResultMap("logResult")
@Select({ "select * from `log` where `id`<#{log.id}" })
List<Log> getListWithPager(@Param("log")Log log,@Param("pageNum") int pageNum,@Param("pageSize") int pageSize);
}

LogSqlProvider.java(LogMapper中使用sql代理类)

public class LogSqlProvider {
/**
* 生成插入日志SQL
* @param log 日志实体
* @return 插入日志SQL
* */
public String insert(Log log) {
return new SQL() {
{
INSERT_INTO("log");
INTO_COLUMNS("title", "module_type", "operate_type","data_id", "content", "create_time","create_user","create_user_id");
INTO_VALUES("#{title}", "#{moduleType,typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler}", "#{operateType,typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler}","#{dataId}", "#{content}", "now()","#{createUser}","#{createUserId}");
}
}.toString();
}
}

ModuleType.java(enum)

package com.dx.test.model.enums;

public enum ModuleType {
Unkown(0),
/**
* 文章模块
*/
Article_Module(1),
/**
* 文章分类模块
**/
Article_Category_Module(2),
/**
* 配置模块
*/
Settings_Module(3); private int value; ModuleType(int value) {
this.value = value;
} public int getValue() {
return this.value;
}
}

OperateType.java(enum)

package com.dx.test.model.enums;

public enum OperateType {
/**
* 如果0未占位,可能会出现错误。
* */
Unkown(0),
/**
* 新增
*/
Create(1),
/**
* 修改
*/
Modify(2),
/**
* 删除
*/
Delete(3),
/**
* 查看
*/
View(4),
/**
* 作废
*/
UnUsed(5); private int value; OperateType(int value) {
this.value = value;
} public int getValue() {
return this.value;
}
}

Log.java(实体类)

package com.dx.test.model;

import java.util.Date;

import com.dx.test.model.enums.ModuleType;
import com.dx.test.model.enums.OperateType; public class Log {
private Long id; // 自增id
private String title;// 日志msg
private ModuleType moduleType;// 日志归属模块
private OperateType operateType; // 日志操作类型
private String dataId; // 操作数据id
private String content; // 日志内容简介
private Date createTime; // 新增时间
private String createUser; // 新增人
private String createUserId; // 新增人id public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} public ModuleType getModuleType() {
return moduleType;
} public void setModuleType(ModuleType moduleType) {
this.moduleType = moduleType;
} public OperateType getOperateType() {
return operateType;
} public void setOperateType(OperateType operateType) {
this.operateType = operateType;
} public String getDataId() {
return dataId;
} public void setDataId(String dataId) {
this.dataId = dataId;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} public String getCreateUser() {
return createUser;
} public void setCreateUser(String createUser) {
this.createUser = createUser;
} public String getCreateUserId() {
return createUserId;
} public void setCreateUserId(String createUserId) {
this.createUserId = createUserId;
} @Override
public String toString() {
return "Log [id=" + id + ", title=" + title + ", moduleType=" + moduleType + ", operateType=" + operateType
+ ", dataId=" + dataId + ", content=" + content + ", createTime=" + createTime + ", createUser="
+ createUser + ", createUserId=" + createUserId + "]";
} }

MybatisTest.java(测试入口类)

package com.dx.test;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.dx.test.dao.LogMapper;
import com.dx.test.model.Log;
import com.dx.test.model.enums.ModuleType;
import com.dx.test.model.enums.OperateType; public class MybatisTest {
public static void main(String[] args) {
InputStream config = null;
try {
config = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
} SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
SqlSession sqlSession = sqlSessionFactory.openSession();
LogMapper logMapper = sqlSession.getMapper(LogMapper.class); // choose:
Log queryLog= new Log();
queryLog.setDataId("1");
List<Log> logByDataIdList=logMapper.getList(queryLog);
for (Log item : logByDataIdList) {
System.out.println(item);
} System.out.println("==========================================================");
String[] titleList = new String[] { "test", "test2", "awr", "a", "c", "tes", "ll", "gg", "dd", "22" };
ModuleType[] moduleTypes = new ModuleType[] { ModuleType.Article_Category_Module, ModuleType.Article_Module,ModuleType.Settings_Module };
OperateType[] operateTypes = new OperateType[] { OperateType.Create, OperateType.Delete, OperateType.Modify,OperateType.Modify, OperateType.UnUsed };
for (int i = 0; i < 10; i++) {
Log waitingInsertLog = new Log(); waitingInsertLog.setTitle("log " + titleList[i]);
waitingInsertLog.setContent("test content" + titleList[i]);
waitingInsertLog.setCreateTime(new Date());
waitingInsertLog.setCreateUser("test user");
waitingInsertLog.setCreateUserId("test user id");
waitingInsertLog.setDataId(String.valueOf(i + 100));
waitingInsertLog.setModuleType(moduleTypes[i % 3]);
waitingInsertLog.setOperateType(operateTypes[i % 5]);
int newLogId = logMapper.insert(waitingInsertLog);
System.out.println(waitingInsertLog.getId());
} // set: 测试
System.out.println("=========================================");
Log waitingInsertLog = new Log(); waitingInsertLog.setTitle("log");
waitingInsertLog.setContent("test content");
waitingInsertLog.setCreateTime(new Date());
waitingInsertLog.setCreateUser("test user");
waitingInsertLog.setCreateUserId("test user id");
waitingInsertLog.setDataId("9999");
waitingInsertLog.setModuleType(ModuleType.Article_Module);
waitingInsertLog.setOperateType(OperateType.View);
int newLogId = logMapper.insert(waitingInsertLog); System.out.println("insert result:"+logMapper.getById(waitingInsertLog.getId())); Log waitingUpdateLodLog=new Log();
waitingUpdateLodLog.setId(waitingInsertLog.getId());
waitingUpdateLodLog.setTitle("1111");
waitingUpdateLodLog.setDataId("10000");
waitingUpdateLodLog.setContent("test content test....");
int updateStatus= logMapper.update(waitingUpdateLodLog); System.out.println("update result:"+logMapper.getById(waitingUpdateLodLog.getId())); // where:Pojo Parameter Map 三种传递参数的用法
System.out.println("========================================="); String title = "test";
ModuleType moduleType = ModuleType.Article_Category_Module;
OperateType operateType = OperateType.Create; Log log = new Log();
log.setTitle(title);
log.setModuleType(moduleType);
log.setOperateType(operateType);
List<Log> logList = logMapper.getByPojo(log);
for (Log item : logList) {
System.out.println(item);
}
System.out.println("=========================================================="); logList = logMapper.getByParameter(title, moduleType, operateType);
for (Log item : logList) {
System.out.println(item);
}
System.out.println("=========================================================="); Map<String, Object> parameterMap = new HashMap<String, Object>();
parameterMap.put("title", title);
parameterMap.put("moduleType", moduleType);
parameterMap.put("operateType", operateType);
logList = logMapper.getByMap(parameterMap);
for (Log item : logList) {
System.out.println(item);
} sqlSession.commit();
sqlSession.close();
}
}

备注:
1)这里moduleType、operateType都是enum类型,在mybatis-config.xml中已经注册typeHandlers:

    <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>

因此,这里完全不需要使用typeHandler、javaType属性

{fieldName,typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler,javaType=com.dx.test.model.enums.OperateType}

,当然如果加上这两个属性也不会抛出异常。
2)如果字段属性类型为enum时,不能判定该值是否不为空字符串或者不为字符串0,这两种用法都不正确,都会导致最终抛出异常:比如:

<if test=\"moduleType!=null and moduleType!='' \">
and `module_type`=#{moduleType}
</if>
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: invalid comparison: com.dx.test.model.enums.ModuleType and java.lang.String
### Cause: java.lang.IllegalArgumentException: invalid comparison: com.dx.test.model.enums.ModuleType and java.lang.String
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy13.getByPojo(Unknown Source)
at com.dx.test.MybatisTest.main(MybatisTest.java:71)

<if test=\"moduleType!=null and moduleType!='0' \">
and `module_type`=#{moduleType}
</if>
Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.NumberFormatException: For input string: "Article_Category_Module"
### Cause: java.lang.NumberFormatException: For input string: "Article_Category_Module"
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy13.getByPojo(Unknown Source)
at com.dx.test.MybatisTest.main(MybatisTest.java:71)

3)上边例子中接收参数并未标注参数名称,如果加上参数别名标注:List<Log> getByPojo(@Param("log") Log log);,这时在<script>中的sql中访问相关属性要访问log对象下属性:

        @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000)
@ResultMap("logResult")
@Select(value={
"<script>",
"select * from `log` " ,
"<where>" ,
" <if test=\"log.title!=null and log.title!=''\">" ,
" and `title` like CONCAT('%', #{log.title}, '%') " ,
" </if>" ,
" <if test=\"log.moduleType!=null \">" ,
" and `module_type`=#{log.moduleType} " ,
" </if>" ,
" <if test=\"log.operateType!=null \">" ,
" and `operate_type`=#{log.operateType} " ,
" </if>" ,
"</where>",
"</script>"
})
List<Log> getByPojo(@Param("log") Log log);

否则会找不到相关属性,抛出异常:

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter 'title' not found. Available parameters are [log, param1]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'title' not found. Available parameters are [log, param1]
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:150)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy13.getByPojo(Unknown Source)
at com.dx.test.MybatisTest.main(MybatisTest.java:71)

通过普通多个参数传递

        @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> getByParameter(@Param("title") String title,@Param("moduleType") ModuleType moduleType,@Param("operateType") OperateType operateType);

这种方法比较容易理解,但是缺点需要逐个定义相关参数。

通过Map传递参数

        @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> getByMap(Map<String, Object> map);

备注:
1)这种方案由于传递的也是对象,和传递POJO一样,如果不定义@Param在<script>内部直接方案相关属性即可;
2)当在参数前定义了@Param时,比如:List<Log> getByMap(@Param("log") Map<String, Object> map);,此时访问属性时,必须加上map.前缀。

        @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000)
@ResultMap("logResult")
@Select(value={
"<script>",
"select * from `log` " ,
"<where>" ,
" <if test=\"map.title!=null and map.title!=''\">" ,
" and `title` like CONCAT('%', #{map.title}, '%') " ,
" </if>" ,
" <if test=\"map.moduleType!=null \">" ,
" and `module_type`=#{map.moduleType} " ,
" </if>" ,
" <if test=\"map.operateType!=null \">" ,
" and `operate_type`=#{map.operateType} " ,
" </if>" ,
"</where>",
"</script>"
})
List<Log> getByMap(@Param("map") Map<String, Object> map);

Trim替代Where、Set等

针对上边的用法我们可以把getByMap(Map<String,Object> map)中Script中where使用trim来替代,例如:

    @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000)
@ResultMap("logResult")
@Select(value={
"<script>",
"select * from `log` " ,
"<trim prefix=\"where\" prefixOverrides=\"and |or |abc \">" ,
" <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>" ,
"</trim>",
"</script>"
})
List<Log> getByMap(Map<String, Object> map);

1)prefixOverrides:前缀覆盖也就是说,where的后面紧跟着的是 and\or\abc,那么这些关键字都会被忽略
2)要注意 | 后面不能有空格,例如: |a 和| a 后面这个a和|之间有空格,会导致忽略失

Choose的用法:

    @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 60000)
@ResultMap("logResult")
@Select({
"<script>",
"select * from `log` " ,
"<where>" ,
" <choose> ",
" <when test=\"dataId!=null\">",
" and data_id=#{dataId}",
" </when>",
" <when test=\"id!=null\">",
" and id=#{id}",
" </when>",
" <otherwise>",
" and 1=1",
" </otherwise>",
" </choose>",
"</where>" ,
"</script>"})
List<Log> getList(final Log log);

注:choose相当于Java中的switch语句;当第一个when满足时,就只执行第一个when中的条件。当when中的条件都不满足时,就会执行默认的代码块,也就是otherwise中的语句。

Set的用法

    @Options(useCache = true, flushCache = Options.FlushCachePolicy.TRUE)
@Update({
"<script>",
"update `log` " ,
"<set>" ,
" <if test=\"dataId!=null\">",
" `data_id`=#{dataId},",
" </if>",
" <if test=\"title!=null\">",
" `title`=#{title},",
" </if>",
" <if test=\"content!=null\">",
" `content`=#{content} ",
" </if>",
"</set>" ,
" where id=#{id}",
"</script>"})
int update(final Log log);

该set用法也可以使用trim来替代:

    @Options(useCache = true, flushCache = Options.FlushCachePolicy.TRUE)
@Update({
"<script>",
"update `log` " ,
"<trim prefix=\"SET\" suffixOverrides=\", |abc \">" ,
" <if test=\"dataId!=null\">",
" `data_id`=#{dataId},",
" </if>",
" <if test=\"title!=null\">",
" `title`=#{title},",
" </if>",
" <if test=\"content!=null\">",
" `content`=#{content}, ",
" </if>",
"</trim>" ,
" where id=#{id}",
"</script>"})
int update(final Log log);

使用<trim>定义<set>规则:
1)suffixOverrides=", |abc",定义了无论是逗号","结尾还是"abc"结尾,都会被程序忽视,上面程序正常运行;
2)文中的abc规则是我添加的,原本只有过滤逗号","。

MyBatis(十一):Mybatis 动态SQL语句完成多条件查询的更多相关文章

  1. MyBatis中动态SQL语句完成多条件查询

    一看这标题,我都感觉到是mybatis在动态SQL语句中的多条件查询是多么的强大,不仅让我们用SQL语句完成了对数据库的操作:还通过一些条件选择语句让我们SQL的多条件.动态查询更加容易.简洁.直观. ...

  2. Mybatis映射文件动态SQL语句-01

    因为在很多业务逻辑复杂的项目中,往往不是简单的sql语句就能查询出来自己想要的数据,所有mybatis引入了动态sql语句, UserMapper.xml <?xml version=" ...

  3. 六、SQL语句进行多条件查询,并解决参数为空的情况

    一.SQL语句进行多条件查询,并解决参数为空的情况 QueryEntity query; var whereSql = new StringBuilder("Where 1=1") ...

  4. mybatis 学习五 动态SQL语句

    3.1 selectKey 标签 在insert语句中,在Oracle经常使用序列.在MySQL中使用函数来自动生成插入表的主键,而且需要方法能返回这个生成主键.使用myBatis的selectKey ...

  5. mybatis中的动态SQL语句

    有时候,静态的SQL语句并不能满足应用程序的需求.我们可以根据一些条件,来动态地构建 SQL语句. 例如,在Web应用程序中,有可能有一些搜索界面,需要输入一个或多个选项,然后根据这些已选择的条件去执 ...

  6. MyBatis之五:动态sql语句

    在mybatis 3 或以上的版本提供了4类标签,分别是:if,choose(when,otherwise),rim(where,set),foreach.接下来将分别介绍这几种标签的具体用法,映射x ...

  7. 阶段3 1.Mybatis_08.动态SQL_01.mybatis中的动态sql语句-if标签

    创建新的工程 复制到新建的项目里面 pom.xml依赖部分复制过来 dao中整理代码 只保留四个查询 映射文件也只保留四个查询方法 增加一个根据条件查询的方法. 由于用了别名,所以parpameter ...

  8. Mybatis映射文件动态SQL语句-02

    foreach UserMapper.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYP ...

  9. SQL 中的多条件查询

    在应用程序开发中,多条件查询是个经常遇到的情况,最简单最麻烦的方法是把所有的可能情况都考虑到,但是无疑是繁琐的,而且很容易漏掉可能的情形,下面是SQL语句实现多条件查询的情况 select * fro ...

随机推荐

  1. Vue项目打包发布后CSS中的背景图片不显示

    相信有很多同学在学习vue的刚开始都遇到过项目打包发布后发现CSS中的背景图片不显示,具体如何解决只需要更改bind的配置即可 修改 build/utils.js 中的 generateLoaders ...

  2. Busness Client 客户端配置

    1,打开Busness Client,点击新建按钮: 2,选择New System Connection(SAP Logon),这个是配置GUI登陆的,就相当于用BC登陆GUI. 这里就和GUI的配置 ...

  3. 数据结构与算法16—平衡二叉(AVL)树

    我们知道,对于一般的二叉搜索树(Binary Search Tree),其期望高度(即为一棵平衡树时)为log2n,其各操作的时间复杂度O(log2n)同时也由此而决定.但是,在某些极端的情况下(如在 ...

  4. 【数据库】数据库入门(三): SQL

    SQL: 结构化查询语言(Structured Query Language) SQL 是由 IBM 公司首先开发产生,它是关系型数据库最早出现的商用语言之一.1974年,IBM 公司 San Jos ...

  5. Excel 逐条导入Mysql(数据更新)

    其实,我的业务流程是, 先读取excel/csv -> pandas 数据清洗 -> 导入Mysql, 一般是做一个表append 或者是 if exist -> replace的操 ...

  6. Appium的测试简单流程

    1.环境的搭建:jdk,SDK,appium,手机模拟器(夜神模拟器) 2.appium的运作流程图: 图中的流程步骤简单来说是: 1.测试脚本写入appium: 2.appium创建连接,将脚本利用 ...

  7. springboot2.1.3 配置前后端跨域问题

    很简单,创建一个配置类即可,如下: package com.app.gateway.common.config; import org.springframework.context.annotati ...

  8. CSS选择符总结(Selectors)

    一.通配选择符(Universal Selector):    语法:*    说明:1.*表示通配符,表示所有的            2.格式:*{样式列表}            3.用于整个页 ...

  9. Linux运维技术之scp命令使用

    命令格式: scp -P 8031  -r 路径1 路径2 命令解释, 其中-P中的P一定要大写,小写不行(这个也是问题的关键,这个大小写耽搁了好长时间) -P 2424表示更改SSH端口后的端口,如 ...

  10. noVNC 搭建

    安装VNCSERVER 在使用noVNC之前,必须得先装vncserver,方法如下: yum -y install vnc-server 安装完成后,执行vncserver配置密码 命令:vncse ...