MyBatis:Mybatis 参数传递用法

Posted yy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis:Mybatis 参数传递用法相关的知识,希望对你有一定的参考价值。

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

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

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

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

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);
}
View Code

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();
    }
}
View Code

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;
    }
}
View Code

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;
    }
}
View Code

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 + "]";
    }

}
View Code

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());
         

以上是关于MyBatis:Mybatis 参数传递用法的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis传递参数

Mybatis标签bind用法

mybatis查询结果date?

Mybatis resultMap 嵌套集合

MyBatis String类型传递参数问题

mybatis 传递参数的方法总结