Mybatis 多条件查询(模糊查询 使用in查询)


Mybatis支持多个条件的查询,使用if标签拼接。

下面是三个条件的查询得例子,使用的数据库是mysql,用到了模糊查询及in做条件查询。

xml文件:

  1. <?xml version=“1.0” encoding=“UTF-8” ?>  
  2. <!DOCTYPE mapper  
  3.   PUBLIC ”-//mybatis.org//DTD Mapper 3.0//EN”  
  4.   “http://mybatis.org/dtd/mybatis-3-mapper.dtd”>  
  5. <mapper namespace=“”>  
  6.     <select id=“selectTeacher” parameterType=“map” resultType=“map”>  
  7.         select t.tid,t.tname,t.taddr  
  8.         from test_teacher t  
  9.         where 1=1  
  10.         <if test=“tid != ” and tid != null”>  
  11.             and tid=#{tid}  
  12.         </if>  
  13.         <if test=“tname != ” and tname != null”>  
  14.             and tname like #{tname}  
  15.         </if>  
  16.         <if test=“addrs != ” and addrs != null”>  
  17.             and taddr in  
  18.             <foreach item=“item” index=“index” collection=“addrs” open=“(“  
  19.                 separator=“,” close=“)”>  
  20.                 #{item}  
  21.             </foreach>  
  22.         </if>  
  23.   
  24.     </select>  
  25.   
  26.   
  27. </mapper>  
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="">
    <select id="selectTeacher" parameterType="map" resultType="map">
        select t.tid,t.tname,t.taddr
        from test_teacher t
        where 1=1
        <if test="tid != '' and tid != null">
            and tid=#{tid}
        </if>
        <if test="tname != '' and tname != null">
            and tname like #{tname}
        </if>
        <if test="addrs != '' and addrs != null">
            and taddr in
            <foreach item="item" index="index" collection="addrs" open="("
                separator="," close=")">
                #{item}
            </foreach>
        </if>

    </select>


</mapper>


dao层:

  1. package mybatis.dao;  
  2.   
  3. import java.util.List;  
  4.   
  5. public interface TestTeacherDao {  
  6.     public List selectTeacher(String tid,String tname,List addrs);  
  7. }  
package mybatis.dao;

import java.util.List;

public interface TestTeacherDao {
public List selectTeacher(String tid,String tname,List addrs);
}


  1. package mybatis.dao;  
  2.   
  3. import java.io.InputStream;  
  4. import java.util.ArrayList;  
  5. import java.util.HashMap;  
  6. import java.util.List;  
  7. import java.util.Map;  
  8.   
  9. import org.apache.ibatis.session.SqlSession;  
  10. import org.apache.ibatis.session.SqlSessionFactory;  
  11. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  12.   
  13. public class TestTeacherDaoImpl implements TestTeacherDao {  
  14.     private int res=-1;  
  15.     private SqlSession sqlSession=null;  
  16.     public  SqlSession getSqlSession(){  
  17.           
  18.         try{  
  19.             String resource=”mybatis-config.xml”;  
  20.             InputStream is=org.apache.ibatis.io.Resources.getResourceAsStream(resource);  
  21.             SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);  
  22.             sqlSession=sqlSessionFactory.openSession(true);  
  23.               
  24.               
  25.         }catch(Exception e){  
  26.             System.out.println(”出错”);  
  27.             e.printStackTrace();  
  28.         }  
  29.         return sqlSession;  
  30.     }  
  31.     @Override  
  32.     public List selectTeacher(String tid,String tname,List addrs) {  
  33.         Map map=new HashMap();  
  34.         map.put(”tid”, tid);  
  35.         map.put(”tname”“%”+tname+“%”);  
  36.         map.put(”addrs”,addrs);  
  37.   
  38.         sqlSession=getSqlSession();  
  39.         List list=sqlSession.selectList(”selectTeacher”, map);  
  40.         return list;  
  41.     }  
  42.   
  43. }  
package mybatis.dao;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class TestTeacherDaoImpl implements TestTeacherDao {
private int res=-1;
private SqlSession sqlSession=null;
public SqlSession getSqlSession(){

    try{
        String resource="mybatis-config.xml";
        InputStream is=org.apache.ibatis.io.Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(is);
        sqlSession=sqlSessionFactory.openSession(true);


    }catch(Exception e){
        System.out.println("出错");
        e.printStackTrace();
    }
    return sqlSession;
}
@Override
public List selectTeacher(String tid,String tname,List addrs) {
    Map map=new HashMap();
    map.put("tid", tid);
    map.put("tname", "%"+tname+"%");
    map.put("addrs",addrs);

    sqlSession=getSqlSession();
    List list=sqlSession.selectList("selectTeacher", map);
    return list;
}

}


测试:

  1. package mybatis.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import mybatis.dao.TestTeacherDao;  
  8. import mybatis.dao.TestTeacherDaoImpl;  
  9.   
  10. public class Test {  
  11.   
  12.     public static void main(String[] args) {  
  13.         TestTeacherDao td=new TestTeacherDaoImpl();  
  14.         List addrs=new ArrayList();  
  15.         addrs.add(”shandong”);  
  16.         addrs.add(”beijing”);  
  17.         List list=td.selectTeacher(”““”,addrs);  
  18.         for(int i=0;i<list.size();i++){  
  19.             Map map=(Map)(list.get(i));                   
  20.             System.out.println(map.get(”tname”));  
  21.         }  
  22.     }  
  23.   
  24. }  
package mybatis.test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import mybatis.dao.TestTeacherDao;
import mybatis.dao.TestTeacherDaoImpl;

public class Test {

public static void main(String[] args) {
    TestTeacherDao td=new TestTeacherDaoImpl();
    List addrs=new ArrayList();
    addrs.add("shandong");
    addrs.add("beijing");
    List list=td.selectTeacher("", "",addrs);
    for(int i=0;i&lt;list.size();i++){
        Map map=(Map)(list.get(i));                 
        System.out.println(map.get("tname"));
    }
}

}







  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值