MyBatis的平等多元问题之处理(遍历Map集合和智能标签和性质与字段不同等的解决办法 和sql片段)(三)

同样、字段名与属于性名(数据库的名)不相同怎么收拾?

  方案一:在有些布置中配置一个resultMapper

<!--方案一:resultMapper 字段名与属性名不一致 -->

     <resultMap type="Student" id="StudentMapper">
        <result column="stuname2" property="stuname"/>
    </resultMap> 


   <!-- 查询所有 -->
 <select id="findAll" resultMap="StudentMapper">
      select * from student
    </select>

  方案二:在稍布置中之查询语句用as

<!-- 方案二:as别名的方式 -->
 <select id="findAll" resultType="Student">
      select stuname2 as stuname from student
    </select>

 二、Mapper动态代理剔除实现类似

先是步改动的地方是有点布置的<mapper namespace=”cn.happy.dao.IStudentDAO”>写及接口

<?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.happy.dao.IStudentDAO">

 <!-- 方案二:as别名的方式 -->
 <select id="findAll" resultType="Student">
      select stuname2 as stuname from student
    </select>


</mapper>

仲步是以测试类调用接口的时光用getMapper获取一个接口实现类似

public class MyTest {
    IStudentDAO dao;
    @Before
    public void initData() throws IOException{
        SqlSession session = MybatisUtil.getSession();
        //动态踢出实现类
        //首先要改StudentDAO.xml改成<mapper namespace="cn.happy.dao.IStudentDAO">
        dao=session.getMapper(IStudentDAO.class);
    }

    /**
     * selectALl学生
     * @throws IOException
     */

    @Test
    public void findAll() throws IOException{
        List<Student> list = dao.findAll();
        for (Student student : list) {
            System.out.println(student.getStuname());
            /*System.out.println(student.getStuage());*/
        }

    }
    }

其三、用Map集合取值和运用索引号

(一)用map集合获取值

  1每当接口IStudentDAO定制一个计

 //多条件查询封装成map
   public List<Student> findStudentMore(Map<String, Object> map);

2于有点布置中id=“findStudentMore”要与接口的方法名要平等

 <!-- 多条件查询-->
<select id="findStudentMore" resultType="Student">
  <!--   mysql数据库 -->
      <!--  select * from student where stuname like '%' #{stuname} '%' and stuage>#{stuage} -->
     <!--   orcl数据库 -->
     select * from student where stuname like '%'||#{stuname}||'%' and stuage>#{stuage}
    </select>

3以测试接近吃

/**
     * 多条件查询
     * @throws IOException
     */

    @Test
    public void findStudentMore(){

        Map<String, Object> maplist=new HashMap<String, Object>();
        maplist.put("stuname", "123");
        maplist.put("stuage", 11);
        List<Student> list = dao.findStudentMore(maplist);
        for (Student student : list) {
            System.out.println(student.getStuname());
        }

    }

(二)使用索引

1在接口IStudentDAO定制一个主意

//多条件查询引号
   public List<Student> findStudentByCondition(String name,int age);

2于聊布置中id=“findStudentMore”要跟接口的不二法门法名要平等

<select id="findStudentByCondition" resultType="Student">
        <!--  mysql数据库        
         select * from student where stuname like '%' #{0} '%' and stuage>#{1}
        orcl数据库 -->
        select * from student where stuname like '%'||#{0}||'%' and stuage>#{1}

    </select>

3每当测试类中

@Test
    public void findStudentByCondition() throws IOException{
        String name="人";
        int age=12;
        List<Student> list = dao.findStudentByCondition(name,age);
        for (Student student : list) {
            System.out.println(student.getStuname());
        }

    }

  小总结:

      MyBatis 1

季、智能标签

她俩同采取的凡之类

    1定义一个措施以接口中

 public List<Student> findStudentByif(Student stu);

   2测试类

public class MyTest {
    IStudentDAO dao;
    @Before
    public void initData() throws IOException{
        SqlSession session = MybatisUtil.getSession();
        //动态踢出实现类
        //首先要改StudentDAO.xml改成<mapper namespace="cn.happy.dao.IStudentDAO">
        dao=session.getMapper(IStudentDAO.class);
    }




    /**
     * 多条件查询
     * @throws IOException
     */


    @Test
    public void findStudentByCondition() throws IOException{
        String name="人";
        int age=12;
        Student stu=new Student();
        stu.setStuage(age);
        stu.setStuname(name);
        List<Student> list = dao.findStudentByif(stu);
        for (Student student : list) {
            System.out.println(student.getStuname());
        }

    }

  3每当小布置中的配置

if标签

<!-- 多条件查询 -->
    <select id="findStudentif" resultType="Student">
        select * from student  where 1=1

            <if test="stuname!=null">
                and stuname like '%'||#{stuname}||'%'
            </if>
            <if test="stuage!=null">
                and stuage>#{stuage}
            </if>

    </select>

where标签     注意要发生<where>标签便非需where 1=1

<!-- 多条件查询 -->
    <select id="findStudentBychoose" resultType="Student">
        select * from student <!-- where 1=1如果有where标签就不需要 -->
        <where>
            <if test="stuname!=null">
                and stuname like '%'||#{stuname}||'%'
            </if>
            <if test="stuage!=null">
                and stuage>#{stuage}
            </if>
        </where>
    </select>

choose标签

    <!--多条件查询where 1=1  如果有where标签就不需要-->
    <select id="findStudentByif" resultType="Student">
        select * from student  
        <where>
        <choose>
            <when test="stuname!=null">
            and stuname like '%'||#{stuname}||'%'

            </when>
            <otherwise>
            </otherwise>
        </choose>


        </where>
    </select>

 foreach标签

(一)数组

  1以接口中

//查询是一个3,11的stuno标号  ids Araay数组
  public List<Student> findByArray(int[] ids);

  2MyBatis以多少布置中安排

    <!-- array数组 -->
    <select id="findByArray" resultType="Student">
        select * from student <!-- where 1=1如果有where标签就不需要 -->
            <if test="array.length>0">
                where stuno in

        <!-- item是自定义的 -->

                <foreach collection="array" open="(" close=")" separator="," item="myid">
                    #{myid}
                    </foreach>
            </if>

    </select>

  3在测试类

//数组
    @Test
    public void findStudentBychoose() throws IOException{
        int[] ids={3,11};//自定义数组
        List<Student> findByArray = dao.findByArray(ids);
        for (Student student : findByArray) {
            System.out.println(student.getStuname());
        }

    }

(二)自定义泛型

  1于接口中

//查询3,11 ids List自定义泛型
  public List<Student> findByListGeneric(List<Student> list);

  2在小布置中配备

<!-- list集合 -->
    <select id="findByListGeneric" resultType="Student">
        select * from student <!-- where 1=1如果有where标签就不需要 -->
            <if test="list.size>0">
                where stuno in
                <foreach collection="list" open="(" close=")" separator="," item="stu">
                    #{stu.stuno}
                    </foreach>
            </if>

    </select>

  3以测试接近吃

//自定义泛型
    @Test
    public void findByListGeneric() throws IOException{
        List<Student> rlist=new ArrayList<Student>();
        Student stu1=new Student();
        stu1.setStuno(3);

        Student stu2=new Student();
        stu2.setStuno(11);

        rlist.add(stu2);
        rlist.add(stu1);
        List<Student> findByListGeneric = dao.findByListGeneric(rlist);
        for (Student student : findByListGeneric) {
            System.out.println(student.getStuname());
        }

 (三)List集合

  1在接口

//查询3,11 ids List
  public List<Student> findByList(List<Integer> list);

 

  2以有点布置中布局

    <select id="findByList" resultType="Student">
        select * from student <!-- where 1=1如果有where标签就不需要 -->
            <if test="list.size>0">
                where stuno in
                <foreach collection="list" open="(" close=")" separator="," item="stulist">
                    #{stulist}
                    </foreach>
            </if>

    </select>

 

  3于测试类

//list集合
    @Test
    public void findByList() throws IOException{
        List<Integer> rlist=new ArrayList<Integer>();
        rlist.add(3);
        rlist.add(11);
        List<Student> findByList = dao.findByList(rlist);
        for (Student student : findByList) {
            System.out.println(student.getStuname());
        }

    }    

 

五、sql片段

  1连连上一个foreach智能标签在稍布置中(用include标签)

<!-- sql片段 -->
<sql id="sqlclum" >
    select stuno,stuname,stuage,studate
</sql>

<!-- list集合 -->
    <select id="findByList" resultType="Student">
        <!-- select * from student  --><!-- where 1=1如果有where标签就不需要 -->
        <include refid="sqlclum"/> from student 
        <if test="list.size>0">
            where stuno in
            <foreach collection="list" open="(" close=")" separator=","
                item="stulist">
                #{stulist}
            </foreach>
        </if>

    </select>

 

网站地图xml地图