【无标题】

有参数的查询JAVAWEB

我们设置多个参数的方法有三种, 我们一 一介绍一下。
在这里插入图片描述

散装参数

在这里先贴一下 我的文件路径:
在这里插入图片描述
petMapper.java和petMapper.xml文件是我们实现多条件查询的源码文件。在petMapper.java定义实现接口,在petMapper.xml文件中实现SQL语句,实现数据库操作。
在petMapper.xml中加入如下代码:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.huihui.mapper.petMapper">
    <sql id="pet_columns">pet_id as petId, pet_name as petName, pet_variety as petVariety,
        pet_sex as petSex, pet_age as petAge, pet_status as petStatus, pet_info as petInfo,
        client_id as clientId, pet_photo as petPhoto
    </sql>
    <resultMap id="pet_resultMap" type="com.huihui.pojo.pet">
        <result column="pet_id" property="petId"/>
        <result column="pet_name"  property="petName"/>
        <result column="client_id " property="clientId"/>
        <result column="pet_variety" property="petVariety"/>
        <result column="pet_status" property="petStatus"/>
    </resultMap>
    <select id="selectBlog" resultMap="pet_resultMap">
        select <include refid="pet_columns"></include> from pet
    </select>
    <select id="selectVarity" resultType="com.huihui.pojo.pet">
        select * from pet where pet_variety='金毛犬' </select>
    <select id="selectByPetIdPets" resultMap="pet_resultMap">
        select * from pet where pet_id=#{id}
    </select>
    <select id="selectByCondition" resultMap="pet_resultMap">
        select * from pet where pet_status like #{petStatus} and pet_variety like #{petVariety}
    </select>

</mapper>

我们把查询的结果放到resultMap中,resultMap中是数据库变量和pet类中变量的一一对应。同时我们定义了多条件查询语句。

package com.huihui.mapper;

import com.huihui.pojo.pet;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface petMapper {
    public List<pet>  selectBlog();
   public List<pet> selectVarity();
   List<pet> selectByPetIdPets(int id);
   List<pet> selectByCondition(@Param("petStatus") String petStatus, @Param("petVariety") String petVariety);

}

我们把这些代码放入petMapper.java。我们在代码中使用 @Param,注意@Param(“petVariety”) String petVariety中的变量名必须和XML文件中的#{petVariety}一致。

map集合

以HashMap为参数传递到接口
在petMapper.java文件中加入接口:

List<pet> selectByConditionSingle(HashMap map);

在petMapper.xml中加入如下代码:

<select id="selectByConditionSingle" resultMap="pet_resultMap">
        select * from pet <where>
        <choose>
            <when test="petVariety!=null">
                pet_variety like #{petVariety}
            </when>
            <when test="petStatus!=null">
                pet_status like #{petStatus}
            </when>
            <otherwise>
                1=1
            </otherwise>
        </choose>
    </where>
    </select>

在test.java中加入:

 HashMap map1=new HashMap();
        map1.put("petStatus","良好");
        List<pet> pet1=petMapper.selectByConditionSingle(map1);
        System.out.println(pet1);

这个可以实现单条件查询。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值