服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 使用Mybatis如何实现多个控制条件查询

使用Mybatis如何实现多个控制条件查询

2022-09-12 14:55black小黑黑 Java教程

这篇文章主要介绍了使用Mybatis如何实现多个控制条件查询,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

实现多个控制条件查询

扩展知识

1.给包起别名用<typeAliases>标签

< typeAliases>
< typeAlias type=“MybatiesAnimal.Animal” alias=“animal”/>
< !-- 指定实体类在哪个包里 -->
< package name=“MybatiesAnimal”/>
< /typeAliases>

2.在<mappers>中通过<package>标签引Mapper.xml

使用Mybatis如何实现多个控制条件查询

如果想用package直接引入所有mapper/xml, 那么要求接口和xml在一个包里

 

实现多个条件简单查询

操作步骤如下:

1.在Mapper.xml中输入要查询的SQL语句

使用Mybatis如何实现多个控制条件查询

2.接口文件中参数用@Param接

语句如下:

public List selAnimalBy(@Param(“NAME”) String a,@Param(“kind”) String b);

如果传的是2个java简单类型 那么需要用@Param(“name”) 指定参数名

3.text文档中输出

语句如下:

List< Animal> animal1=sqlsession.getMapper(AnimalMapper.class).selAnimalBy(“老虎”, “猫科”);
System.out.println(animal1);

 

数据库的字段名和实体类的属性名不一致时

因为数据库命名多个单词之间用下划线连接,而Java命名规则为第二个字母大写,命名规则不同时,需要传值。

方法一:修改SQL语句

SELECT a_sid sid,kind,numbers,address,NAME FROM animal

给a_sid 取别名为sid

xml中语句修改如下:

使用Mybatis如何实现多个控制条件查询

方法二:通过配置resultMap标签

原查询语句:

< select id=“selAnimalBy” resultType=“animal” >
SELECT * FROM animal WHERE NAME=#{NAME} AND kind=#{kind}
< /select>

修改为:

使用Mybatis如何实现多个控制条件查询

 

实现多个条件复杂查询

例如:查询动物列表中猫科中包含虎字段的数据

使用Mybatis如何实现多个控制条件查询

SQL语句为:

SELECT * FROM animal WHERE 1=1 AND kind=“猫科” AND NAME LIKE ‘%虎%"

1.配置xml中的select标签

如下图:

使用Mybatis如何实现多个控制条件查询

2.添加接口中的执行语句

public List< Animal> selCon(Animal animal);

3.text文档检验结果

Animal animal1=new Animal();
animal1.setKind(“猫科”);
animal1.setName(“虎”);
List< Animal>animal2=sqlsession.getMapper(AnimalMapper.class).selCon(animal1);
System.out.println(animal2);

输出结果如下:

使用Mybatis如何实现多个控制条件查询

 

MyBatis条件查询总结

 

1.if条件语句

<!--  if(判断参数) - 将实体类不为空的属性作为where条件 -->  
    <select id="getStudentList_if" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">  
        SELECT ST.STUDENT_ID,  
               ST.STUDENT_NAME,  
               ST.STUDENT_SEX,  
               ST.STUDENT_BIRTHDAY,  
               ST.STUDENT_PHOTO,  
               ST.CLASS_ID,  
               ST.PLACE_ID  
          FROM STUDENT_TBL ST   
         WHERE  
        <if test="studentName !=null ">  
            ST.STUDENT_NAME LIKE CONCAT(CONCAT("%", #{studentName, jdbcType=VARCHAR}),"%")  
        </if>  
        <if test="studentSex != null and studentSex != "" ">  
            AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
        </if>  
        <if test="studentBirthday != null ">  
            AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
        </if>  
        <if test="classId != null and classId!= "" ">  
            AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
        </if>  
        <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=" " ">  
            AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
        </if>  
        <if test="placeId != null and placeId != "" ">  
            AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}  
        </if>  
        <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != "" ">  
            AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}  
        </if>  
        <if test="studentId != null and studentId != "" ">  
            AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}  
        </if>   
    </select> 

 

2.choose (when otherwise)

<!--  choose(判断参数) - 按顺序将实体类 User 第一个不为空的属性作为:where条件 -->  
<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">  
    SELECT *  
      FROM User u   
    <where>  
        <choose>  
            <when test="username !=null ">  
                u.username LIKE CONCAT(CONCAT("%", #{username, jdbcType=VARCHAR}),"%")  
            </when >  
            <when test="sex != null and sex != "" ">  
                AND u.sex = #{sex, jdbcType=INTEGER}  
            </when >  
            <when test="birthday != null ">  
                AND u.birthday = #{birthday, jdbcType=DATE}  
            </when >  
            <otherwise>  
            </otherwise>  
        </choose>  
    </where>    
</select>  
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
        select * from t_blog where 1 = 1 
        <choose>
            <when test="title != null">
                and            </when>
            <when test="content != null">
                and content = #{content}
            </when>
            <otherwise>
                and owner = "owner1"
            </otherwise>
        </choose>
</select>

 

4.in的用法

    <select id="getNewListByLabelID" resultMap="BaseResultMap" parameterType="arraylist">
        SELECT
        <include refid="Base_Column_List"/>
        FROM tb_problem
        WHERE id IN
        <foreach collection="ids" index="index" item="id" separator="," close=")" open="(">
            #{id}
        </foreach>
    </select>

还可以这样

<select id="queryAllOpenProduct" parameterType="com.tims.open.domain.OpenProductQueryCondition"
    resultType="com.tims.open.domain.OpenProduct">
    SELECT
         *
    FROM
        product_db.product p
    WHERE
        p.isvalid = 1
    <if test="list != null">
        <foreach collection="list" index="index" item="item" separator="," open="AND p.id IN (" close=")">
               #{item}
        </foreach>
    </if>
</select>
//查询condition类
Public OpenProductQueryCondition{
    private Integer productId;  
    private List<Integer> list;
}

 

5.模糊查询

<!-- ******************** 模糊查询的常用的3种方式:********************* -->
    <select id="getUsersByFuzzyQuery" parameterType="User" resultType="User">
        select <include refid="columns"/> from users
        <where>
            <!--
                方法一: 直接使用 % 拼接字符串 
                注意:此处不能写成  "%#{name}%" ,#{name}就成了字符串的一部分,
                会发生这样一个异常: The error occurred while setting parameters,
                应该写成: "%"#{name}"%",即#{name}是一个整体,前后加上%
            -->
            <if test="name != null">
                name like "%"#{name}"%"
            </if>
            <!--方法二: 使用concat(str1,str2)函数将两个参数连接 -->
            <if test="phone != null">
                and phone like concat(concat("%",#{phone}),"%")
            </if>
            <!--方法三: 使用 bind 标签,对字符串进行绑定,然后对绑定后的字符串使用 like 关键字进行模糊查询 -->
            <if test="email != null">
                <bind name="pattern" value=""%"+email+"%""/>
                and email like #{pattern}
            </if>
        </where>
    </select>

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文地址:https://blog.csdn.net/weixin_43689040/article/details/84717412

延伸 · 阅读

精彩推荐