1. 程式人生 > >mybatis 多條件查詢

mybatis 多條件查詢

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層的物件名一致