后台(39)——MyBatis输入映射parameterType

探索Android软键盘的疑难杂症
深入探讨Android异步精髓Handler
详解Android主流框架不可或缺的基石
站在源码的肩膀上全解Scroller工作机制


Android多分辨率适配框架(1)— 核心基础
Android多分辨率适配框架(2)— 原理剖析
Android多分辨率适配框架(3)— 使用指南


自定义View系列教程00–推翻自己和过往,重学自定义View
自定义View系列教程01–常用工具介绍
自定义View系列教程02–onMeasure源码详尽分析
自定义View系列教程03–onLayout源码详尽分析
自定义View系列教程04–Draw源码分析及其实践
自定义View系列教程05–示例分析
自定义View系列教程06–详解View的Touch事件处理
自定义View系列教程07–详解ViewGroup分发Touch事件
自定义View系列教程08–滑动冲突的产生及其处理


版权声明


我们知道:MyBatis通过parameterType对sql的输入参数进行定义,参数的类型可以是:基本类型、HashMap、pojo。在此分别介绍为parameterType传入三种类型的不同处理方式。

基本类型

其实,从这个MyBatis学习系列开始,我们已经多次为parameterType传入基本类型的参数,比如int 、long等。故,在此不再赘述,请参见前几篇博客的示例。

HashMap

首先来看mapper.mxl中的sql语句

<select id="findStudentByHashMap" parameterType="hashmap" resultType="cn.com.Student">
        SELECT * FROM student WHERE id=#{id} and name like '%${name}%'
</select>

嗯哼,看到没有:我们为parameterType指定的输入类型是hashmap。在sql语句中从hashmap中取出id和name作为查询条件

接下来瞅瞅mapper.java中的定义

public List<Student> findStudentByHashMap(HashMap<String, Object> hashMap);

最后,再来看看测试代码:

@Test
    public void findStudentByHashMap() throws IOException {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        HashMap<String, Object> hashMap=new HashMap<String, Object>();
        hashMap.put("id", 7);
        hashMap.put("name", "木");
        List<Student> studentList = studentMapper.findStudentByHashMap(hashMap);
        for (int i = 0; i <studentList.size(); i++) {
            Student student = studentList.get(i);
            System.out.println(student);
        }
        sqlSession.commit();
        sqlSession.close();
    }

在此,创建一个HashMap且指定两个key:id和name并为它们赋值;然后执行查询即可。

pojo

有时候,我们需要执行一些复杂的查询,比如:查询的条件不仅包括学生查询条件还包括其它的查询条件(比如:课程,教师,学校等)。此时,可以使用自定义pojo传递输入参数。

首先,定义一个Student的扩展类

/**
 * 本文作者:谷哥的小弟
 * 博客地址:http://blog.csdn.net/lfdfhl
 */
package cn.com;

//Student的扩展类
public class StudentCustom extends Student{

}

再自定义包装类型的pojo

/**
 * 本文作者:谷哥的小弟
 * 博客地址:http://blog.csdn.net/lfdfhl
 */
package cn.com;
//自定义的包装类型的pojo
public class StudentQueryVO {

    //用户查询条件
    private StudentCustom studentCustom;

    public StudentCustom getStudentCustom() {
        return studentCustom;
    }

    public void setStudentCustom(StudentCustom studentCustom) {
        this.studentCustom = studentCustom;
    }

    //其他查询条件,比如教师,课程,学校等等
}

在该pojo中不仅包括与学生相关的查询条件,还有与教师,课程,学校有关的查询条件。

接下来请看mapper.xml

<select id="findStudentList" parameterType="cn.com.StudentQueryVO" resultType="cn.com.StudentCustom">
        SELECT * FROM student WHERE gender=#{studentCustom.gender} and name like '%${studentCustom.name}%'
</select>

嗯哼,看到了吧:我们将自定义的包装类型的pojo作为输入参数设置给parameterType;然后取出输入参数StudentQueryVO中的studentCustom的gender和name作为条件查询。

再来瞅瞅mapper.java中的定义

public List<StudentCustom> findStudentList(StudentQueryVO studentQueryVO);

最后,请看测试代码:

@Test
    public void findStudentList() throws IOException {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        StudentQueryVO studentQueryVO=new StudentQueryVO();
        StudentCustom studentCustom=new StudentCustom();
        studentCustom.setGender("female");
        studentCustom.setName("木");
        studentQueryVO.setStudentCustom(studentCustom);
        List<StudentCustom> studentList = studentMapper.findStudentList(studentQueryVO);
        for (int i = 0; i <studentList.size(); i++) {
            StudentCustom sc = studentList.get(i);
            System.out.println(sc);
        }
        sqlSession.commit();
        sqlSession.close();
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值