mybatis 多条件查询

本文介绍了一个基于Java的后台查询案例,展示了如何通过Controller调用Service并最终由Dao执行数据库查询的过程。具体包括创建AccountBean对象设置查询条件、利用MyBatis进行参数传递及SQL构建等关键技术细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Controller:

  

AccountBean accountBean=new AccountBean();
                accountBean.setAccount_id(Integer.parseInt(id));
                List<AccountBean> accountBeans=getAccountService.getAccountByAid(accountBean);

Service:

 public List<AccountBean> getAccountByAid(AccountBean accountBean)
    {
        return getAccountDao.getAccountByAid(accountBean);
    }

Dao:

List<AccountBean> getAccountByAid(@Param("accountBean") AccountBean accountBean);

Mapper.xml

    <select id="getAccountByAid" parameterType="com.ctrlv.suntime.simulate.comm.entity.AccountBean" resultMap="GetAccountMap">
        SELECT account_id,account_code,account_name,cash_available,cash_init,cash_balance,total_value FROM sst_account
        <where>
            <if test="accountBean.account_id!=null">
                and account_id=#{accountBean.account_id}
            </if>
        </where>
    </select>

parameterType 对应我们的实体类 使用 <where></where> 组合查询条件  自动去除第一个and   

accountBean.account_id!=null  对象名应和Dao层的对象名一致

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值