SSM框架笔记05:MyBatis条件查询

MyBatis条件查询

1、提出需求
对学生表进行条件查询,涉及姓名、性别和年龄。

student表与Student实体类在上一讲里已经创建好了。

SSM框架笔记05:MyBatis条件查询_第1张图片

package net.hw.mybatis.bean;

/**
 * Created by howard on 2017/2/15.
 */
public class Student {
    private int id;
    private String name;
    private String gender;
    private int age;
    private Clazz clazz;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Clazz getClazz() {
        return clazz;
    }

    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                ", clazz=" + clazz.getName() +
                '}';
    }
}
2、创建学生映射文件StudentMapper.xml

SSM框架笔记05:MyBatis条件查询_第2张图片





    

    
        
        
        
        
        
    

    

3、 在mybatis-config.xml文件里注册StudentMapper.xml

SSM框架笔记05:MyBatis条件查询_第3张图片

4、定义学生映射接口StudentMapper

SSM框架笔记05:MyBatis条件查询_第4张图片

package net.hw.mybatis.mapper;

import net.hw.mybatis.bean.Student;

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

/**
 * Created by howard on 2017/2/15.
 */
public interface StudentMapper {
    List findByCondition(Map condition);
}
5、 创建测试程序TestStudentMapper

SSM框架笔记05:MyBatis条件查询_第5张图片

package net.hw.mybatis.mapper;

import net.hw.mybatis.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.Reader;
import java.util.HashMap;
import java.util.List;

/**
 * Created by howard on 2017/2/15.
 */
public class TestStudentMapper {
    private SqlSession session;
    private StudentMapper studentMapper;

    @Before
    public void init() {
        try {
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
            session = factory.openSession();
            studentMapper = session.getMapper(StudentMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testFindByCondition() {
        HashMap condition = new HashMap();
        condition.put("gender", "女");
        List students = studentMapper.findByCondition(condition);
        for (Student student : students) {
            System.out.println(student);
        }

    }

    @After
    public void destroy() {
        session.close();
    }
}
运行testFindByCondition()方法,结果如下:

SSM框架笔记05:MyBatis条件查询_第6张图片

修改查询条件,查询19岁的女生:

SSM框架笔记05:MyBatis条件查询_第7张图片

运行testFindByCondition()方法,结果如下:
SSM框架笔记05:MyBatis条件查询_第8张图片

你可能感兴趣的:(Java,Web框架)