MyBatis的一系列问题的处理(遍历Map集合和智能标签和属性和字段不一样的解决办法)

一、字段名与属性名(数据库的名字)不一样怎么办?

  方案一:在小配置中配置一个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());
        }

    }

  小总结:

      

四、智能标签

他们共同用到的是如下

    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>
时间: 2025-01-06 15:25:12

MyBatis的一系列问题的处理(遍历Map集合和智能标签和属性和字段不一样的解决办法)的相关文章

Java遍历Map集合方法

package testMap; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; /**  * 循环遍历Map集合  *   * @author Administrator  *   */ pub

遍历Map集合的几种方式

1 import java.util.HashMap; 2 import java.util.Iterator; 3 import java.util.Map; 4 import java.util.Map.Entry; 5 6 /** 7 * <p>遍历Map集合</p> 8 * @author:[email protected] 9 * @date:2017-5-30 10 */ 11 public class Test { 12 public static void main

25.使用Iterator和增强型for循环遍历Map集合

/** * 宠物类,狗狗和企鹅的父类. */ public abstract class Pet { protected String name = "无名氏";// 昵称 protected int health = 100;// 健康值 protected int love = 0;// 亲密度 public abstract void eat(); //抽象方法eat(),负责宠物吃饭功能. /** * 无参构造方法. */ public Pet() { } /** * 有参构造

不同方式遍历Map集合

Map是一种以键值对的形式存在的集合,其中每个键映射到一个值,几乎所有通用 Map 都使用哈希映射.位于java.util包中.其子类有HashMap,TreeMap.HashMap缺省的情况下是线程非安全的;当多线程访问时可能需要提供同步机制,key和value的值允许为null,键值允许重复,没有顺序. 普通JAVA类遍历Map集合 Map map=new HashMap(); map.put("a", "1"); map.put("b",

(1)集合 ---遍历map集合

Map接口     实现Map接口的类用来存储键(key)-值(value) 对.Map 接口的实现类有HashMap和TreeMap等.Map类中存储的键-值对通过键来标识,所以键值不能重复. HashMap: 线程不安全,效率高. 允许key或value为nullHashTable:线程安全,效率低. 不允许key或value为nullProperties : HashTable的子类,key和value都是string常用的方法: Object put(Object key, Object

遍历Map集合的几种方法

遍历Map集合的几种方法 方法1:使用迭代器iterator遍历集合 HashMap<Integer, Long> map = new HashMap<Integer, Long>(); for (int i = 1; i <= 50; i++) { map.put(i, Math.round(3.14*i*i)); } // map转换为set集合 Set<Entry<Integer, Long>> set = map.entrySet(); //

迭代器遍历map集合的步骤 黑马程序员

迭代器遍历map集合的步骤: Map map = new HashMap(); map.put(1, "a"); map.put(2, "b"); map.put(3, "c"); map.put(4, "d"); Iterator it = map.entrySet().iterator();//取得map集合的中每个键值对所对应的iterator对象 while(it.hasNext()){ String str = it

遍历Map集合四中方法

<embed wmode="transparent" src="http://chabudai.sakura.ne.jp/blogparts/honehoneclock/honehone_clock_tr.swf" quality="high" bgcolor="#ffffff" width="160" height="70" name="honehoneclock&qu

键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:&quot;java&quot;,&quot;txt&quot;)作为key, 用个数作为value,放入到map集合中,遍历map集合

package cn.it.zuoye5; import java.io.File;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Scanner;import java.util.Set; /** 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt&qu