1. 程式人生 > >Mysql在mapper中的條件查詢

Mysql在mapper中的條件查詢

1. 多條件查詢

type 屬性用於指定獲取sql語句的指定類

method 屬性用於指定類中要執行獲取sql語句的方法

package com.msp.whg.mapper;

import com.msp.whg.domain.Attendances;
import com.msp.whg.util.AppMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.SelectProvider;
import java.util.List;

public interface AttendancesMapper extends AppMapper<Attendances> {

    @SelectProvider(type = AttendancesMapper.listAttendancess.class, method = "listAttendance")
    public List<Attendances> listAttendances(Attendances attendances);

    class listAttendancess {
        public String listAttendance(Attendances attendances) {
            String sql = "select  at.SUH_ITEMCODE suhItemcode,at.SUH_NAME suhName,\n" +
                    "at.SUH_COURSES suhCourses,at.SUH_TIME suhTime,at.SUH_STATE suhState,\n" +
                    "at.SUH_TYPE suhType,at.SUH_MODE suhMode,at.SUH_CARDTIME suhCardtime  \n" +
                    "from Attendances at where 1 = 1";
            if(StringUtils.isNotBlank(attendances.getSuhItemcode())){
                sql+=" and at.SUH_ITEMCODE ='"+attendances.getSuhItemcode()+"'";
            }
            if(StringUtils.isNotBlank(attendances.getSuhType())){
                sql+=" and at.SUH_TYPE ='"+attendances.getSuhType()+"'";
            }
            if(StringUtils.isNotBlank(attendances.getSuhName())){
                sql+=" and at.SUH_NAME LIKE '%"+attendances.getSuhName()+"%'";
            }
            if(StringUtils.isNotBlank(attendances.getSuhCourses())){
                sql+=" and at.SUH_COURSES ='"+attendances.getSuhCourses()+"'";
            }
            if(StringUtils.isNotBlank(attendances.getSuhCardtime())){
                sql+=" and at.SUH_CARDTIME LIKE '%"+attendances.getSuhCardtime()+"%'";
            }
            sql +=" ORDER BY SUH_CARDTIME DESC ";
            return sql;
        }
    }

}

方法二:

@Param註解的用法解析:

package com.msp.whg.mapper;
import com.msp.whg.domain.DeclareManage;
import com.msp.whg.util.AppMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

public interface DeclareMmanageMapper extends AppMapper<DeclareManage>{

    @Select(" SELECT * FROM declare_manage WHERE declare_xtbh  = #{icItemcode}")
    public Map<String,Object> dataListOne(@Param("icItemcode") String icItemcode);

}