MyBatis智能标签查询

     StudentINfo接口:  
       //模糊查询
public   List<StudentINfo> selectlink(StudentINfo info);  
            //查询学生信息
public List<StudentINfo> findtion(Map<String,Object> map);
 
             //根据索引查询学生信息
 一个问题:如果有人遇到 参数不匹配,args0,args1     

public   List<StudentINfo>  findmutilages(String  stuName,int  stuAge); 
             //智能标签if
public List<StudentINfo> findByif(StudentINfo stu);
            //智能标签Choose
public List<StudentINfo> fingChoose(StudentINfo stu);
          //智能标签Foreach Array
public List<StudentINfo> findforeachArry(int[]  ids);
         //智能标签foreach List<Integer>
public   List<StudentINfo> findforeachList(List<Integer> list); 
         //智能标签foreach List<StudentINfo>
public List<StudentINfo> findForeachStudent(List<StudentINfo> list);

StudentInfo.xml小配置:
   
<!--模糊查询-->
    <!--SELECT  *  from studentinfo WHERE stuName LIKE  '%' #{stuName} '%' and  stuAge>#{stuAge}-->
    <!-- SELECT  *  from  studentinfo  WHERE stuName LIKE  concat('%',#{stuName},'%') and stuAge>#{stuAge}-->
    <select id="selectlink" resultType="StudentINfo">
     SELECT  *  from studentinfo where stuName LIKE  '%${stuName}%' and  stuAge>#{stuAge}
    </select>

<!--多条件查询-->
<select id="findtion" resultType="StudentINfo">
    select *  FROM  studentinfo  WHERE stuName LIKE  '%' #{stuName} '%' and  stuAge>#{stuAge}
</select>

    <!--多条件使用索引-->
    <select id="findmutilages" resultType="StudentINfo">
        SELECT  *  from   studentinfo  WHERE stuName LIKE  '%' #{0} '%' and  stuAge>#{1}
    </select>
    
    <!--智能标签if-->
    <select id="findByif" resultType="StudentINfo">
        SELECT  *  from  studentinfo
        where 1=1
        <if test="stuName!=null">
            AND   stuName LIKE  '%' #{stuName} '%'
        </if>
        <if test="stuAge!=null">
            and stuAge>#{stuAge}
        </if>
    </select>
    
    <!--只能标签Choose-->
    <select id="fingChoose" resultType="StudentINfo">
        SELECT  *  from studentinfo
        <where>
            <choose>
                <when test="stuName!=null">
                    and stuName LIKE  '%' #{stuName} '%'
                </when>
                <when test="stuAge!=null">
                    and stuAge>#{stuAge}
                </when>
                <otherwise>
                    AND  1=2
                </otherwise>
            </choose>
        </where>
    </select>

    <!--智能标签Foreach Array-->
  <select id="findforeachArry" resultType="StudentINfo">
      SELECT  *  from  studentinfo
      <where>
          <if test="array.length>0">
              stuId IN
              <foreach collection="array" open="(" close=")" separator="," item="stuno">
                  #{stuno}
              </foreach>
          </if>
      </where>
  </select>

    <!--智能标签Foreach AList<Integer>-->
    <select id="findforeachList" resultType="StudentINfo">
        SELECT  *  from  studentinfo
        <where>
            <if test="list.size>0">
                stuId IN
                <foreach collection="list" open="(" close=")" separator="," item="stuno">
                    #{stuno}
                </foreach>
            </if>
        </where>
    </select>


    <!--智能标签Foreach  List<StudentINfo>-->
    <select id="findForeachStudent" resultType="StudentINfo">
        SELECT  *  from  studentinfo
        <where>
            <if test="list.size>0">
                stuId IN
                <foreach collection="list" open="(" close=")" separator="," item="stu">
                    #{stu.stuId}
                </foreach>
            </if>
        </where>
    </select>

   StudentInfotest测试类:
//多条件查询
@Test
public   void  testLink() throws IOException {
    Userinfoutil   uf=new Userinfoutil();
    SqlSession  session=uf.sqls();
    StudentINfoDao   dao=session.getMapper(StudentINfoDao.class);
    Map<String,Object>  map=new HashMap<String,Object>();
    map.put("stuName","水");
    map.put("stuAge",11);
    List<StudentINfo> list=dao.findtion(map);
    for (StudentINfo  stu:list) {
        System.out.println(stu.getStuName());
    }
    session.close();
}


//多条件查询,按索引查询
@Test
public  void  testMuil() throws IOException {
    Userinfoutil   h=new Userinfoutil();
    SqlSession sc=h.sqls();
    StudentINfoDao  s=sc.getMapper(StudentINfoDao.class);
    List<StudentINfo> list=s.findmutilages("水",11);
    for (StudentINfo  ss:list) {
        System.out.println(ss.getStuName());
    }
    sc.close();
}

//智能标签if
@Test
public   void  testIf() throws IOException {
    Userinfoutil   h=new Userinfoutil();
    SqlSession sc=h.sqls();
    StudentINfoDao  s=sc.getMapper(StudentINfoDao.class);
    StudentINfo  stu=new StudentINfo();
  /*  stu.setStuName("水");
    stu.setStuAge(11);*/
    List<StudentINfo>  list=s.findByif(stu);
    for (StudentINfo  ds:list) {
        System.out.println(ds.getStuName());
    }
    sc.close();
}

//智能标签Choose
@Test
public   void  testChoose() throws IOException {
    Userinfoutil   h=new Userinfoutil();
    SqlSession sc=h.sqls();
    StudentINfoDao  s=sc.getMapper(StudentINfoDao.class);
    StudentINfo  stu=new StudentINfo();
   /* stu.setStuName("水");
    stu.setStuAge(11);*/
    List<StudentINfo> list=s.fingChoose(stu);
    for (StudentINfo info:list) {
        System.out.println(info.getStuName());
    }
    sc.close();
}

//智能标签foreach  array
@Test
public   void  testForeachArray() throws IOException {
    Userinfoutil   h=new Userinfoutil();
    SqlSession sc=h.sqls();
    StudentINfoDao  s=sc.getMapper(StudentINfoDao.class);
  int[]  ids={2,5};
  List<StudentINfo>  list=s.findforeachArry(ids);
    for (StudentINfo info:list) {
        System.out.println(info.getStuName());
    }
    sc.close();
}


//智能标签foreach  List<Integer>
@Test
public   void  testForeachList() throws IOException {
    Userinfoutil   h=new Userinfoutil();
    SqlSession sc=h.sqls();
    StudentINfoDao  s=sc.getMapper(StudentINfoDao.class);
    List<Integer> lds=new ArrayList<Integer>();
    lds.add(2);
    lds.add(5);
    List<StudentINfo>  list=s.findforeachList(lds);
    for (StudentINfo info:list) {
        System.out.println(info.getStuName());
    }
    sc.close();
}



//智能标签foreach  List<StudentINfo>
@Test
public   void  testForeachStudentINfo() throws IOException {
    Userinfoutil   h=new Userinfoutil();
    SqlSession sc=h.sqls();
    StudentINfoDao  s=sc.getMapper(StudentINfoDao.class);
    List<StudentINfo> lds=new ArrayList<StudentINfo>();
   StudentINfo   ld=new StudentINfo();
   ld.setStuId(2);
   StudentINfo  ls=new StudentINfo();
   ls.setStuId(5);
   lds.add(ld);
   lds.add(ls);
    List<StudentINfo>  list=s.findForeachStudent(lds);
    for (StudentINfo info:list) {
        System.out.println(info.getStuName());
    }
    sc.close();
}

DeptDao接口:
     
//根据部门编号,检索部门名称以及该部门下所有员工的信息
public Dept getDeptNO(int  deptNO);

DeptDao.xml小配置:     
<?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="cn.bdqn.dao.DeptDao">
    <!--根据部门编号,查询部门对象,对象里面包含着员工的集合-->
    <resultMap id="depeMper" type="Dept">
        <id column="deptNo" property="deptNo"></id>
        <result column="deptName" property="deptName"/>
        <collection property="emps" ofType="Emp">
            <id column="empNo" property="empNo"></id>
            <result column="empName" property="empName"/>
        </collection>
    </resultMap>
 <select id="getDeptNO" resultMap="depeMper">
 select dept.deptNo,deptName,empNo,empName
        from dept,emp
        where dept.deptNo=emp.deptNo
        and dept.deptNo=#{deptNo}

 </select>

</mapper>
 
Depttest测试类:
     
public   void  testDept() throws IOException {
    Userinfoutil  user=new Userinfoutil();
    SqlSession  sqlSession=user.sqls();
    DeptDao  dept=sqlSession.getMapper(DeptDao.class);
    Dept  dent=dept.getDeptNO(3);
    System.out.println(dent.getDeptName());
    for (Emp emp:dent.getEmps()) {
        System.out.println(emp.getEmpName());
    }
    sqlSession.close();
}
大配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    <typeAliases>
        <package name="cn.bdqn.entity"></package>
    </typeAliases>


    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
                <property name="username" value="sha" />
                <property name="password" value="sha" />
            </dataSource>
        </environment>
    </environments>
    <!--映射文件:描述某个实体和数据库表的对应关系 -->
    <mappers>

      <!-- <mapper resource="cn/bdqn/dao/DeptDao.xml"></mapper>      //这种也可以,只是有点麻烦
        <mapper resource="cn/bdqn/dao/StudentINfo.xml"></mapper>-->

       <package name="cn.bdqn.dao"></package>
</mappers>
</configuration>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值