Mybatis输入输出映射

目录

一、输入映射

1.基本数据类型

2.自定义类型

3.pojo的包装类型

二、输出映射

1.resultMap自定义映射


一、输入映射

通过parameterType指定输入参数的类型,类型可以是基本数据类型、自定义类型、pojo的包装类型。

1.基本数据类型
 
2.自定义类型


        update student set studentName=#{studentName},gender=#{gender},address=#{address},email=#{email},remark=#{remark} where id=#{id}

3.pojo的包装类型

        创建Student类

public class Student {
    private int id;
    private String studentName;
    private String gender;
    private String address;
    private String email;
    private String remark;
    private List listOrder;

    public Student() {
    }

    public Student(String studentName, String gender, String address,String remark) {
        this.studentName = studentName;
        this.gender = gender;
        this.address = address;
        this.remark = remark;
    }

    public List getListOrder() {
        return listOrder;
    }

    public void setListOrder(List listOrder) {
        this.listOrder = listOrder;
    }

    public int getId() {
        return id;
    }

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

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getGender() {
        return gender;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }
}

        定义StudentMapper接口

public interface StudentMapper {
    //通过id查询学生
    public Student findStudentById(int id);
}

        编写StudentMapper.xml映射文件




    

        测试

public class TestDemo {
    SqlSessionFactory ssf = null;
    @Before
    public void creatFactory(){
        InputStream input = null;
        try {
            input = Resources.getResourceAsStream("SqlMapConfig.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        ssf = new SqlSessionFactoryBuilder().build(input);
    } 
@Test
    public void testMapper(){
        SqlSession session =ssf.openSession();
        StudentMapper mapper = session.getMapper(StudentMapper.class);
        Student student = mapper.findStudentById(1);
        System.out.println(student.getStudentName());
        session.commit();
        session.close();
    }
二、输出映射

输出映射有两种,resultType和resultMap。resultType也有很多种对象类型,当我们使用resultType时,查询出来的列名必须和pojo中对应的属性名要一致才能正确的映射成功,否则,就会出现映射出错这种现象。如果属性名不一致时使用resultMap自定义映射

这里我主要介绍resultMap自定义映射

1.resultMap自定义映射

        创建Exams实例(其中的eid,examdate,uid属性与数据库中的字段名不一致)

public class Exams {
    private int eid;
    private Date examdate;
    private int uid;
    private String score;

    public int getEid() {
        return eid;
    }

    public void setEid(int eid) {
        this.eid = eid;
    }

    public Date getExamdate() {
        return examdate;
    }

    public void setExamdate(Date examdate) {
        this.examdate = examdate;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }
}

        创建ExamMapper接口

import com.batis.domain.Exams;

import java.util.List;

public interface ExamMapper {
    //查询所有信息
    public List findExamAll();
}

        创建ExamMapper.xml映射文件





    
        
        
        
        

    
    
    

        测试

@Test
    public void testMapper3(){
        SqlSession sqlSession = ssf.openSession();
        ExamMapper mapper = sqlSession.getMapper(ExamMapper.class);
        List examAll = mapper.findExamAll();
        for (Exams exam : examAll) {
            System.out.println(exam.getEid()+","+exam.getExamdate());
        }
    }

        运行结果

你可能感兴趣的:(sql,mybatis,数据库)