mybatis(输入、输出参数、ResultMap)

1、输入参数(POJO包装类):

(1)POJO包装类:

public class QueryVo implements Serializable {
    private static final long serialVersionUID = 1L;
    private Student student;
    public Student getStudent() {
        return student;
    }

    public void setStudent(Student student) {
        this.student = student;
    }
}

需要实现序列化接口,用于实现序列化(对象转化为二进制)与反序列化(二进制转换为对象)。

(2)配置mapper.xml配置文件:

<select id="findStudentByQueryVo" parameterType="QueryVo" resultType="pers.zhb.pojo.Student">
        select * from student where sname like "%"#{student.sname}"%"
</select>

(3)创建接口:

public interface StudentMapper {
     Student findStudentById(Integer studentno);
     List<Student> findStudentByQueryVo(QueryVo vo);
}

(4)测试:

 public void findStudentByQueryVo() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSEssion帮我生成一个实现类  (给接口)
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        QueryVo queryVo=new QueryVo();
        Student student=new Student();
        student.setSname("z");
        queryVo.setStudent(student);
        List<Student> s = studentMapper.findStudentByQueryVo(queryVo);
        for(Student ss:s){
            System.out.println(s);
        }
    }

2、输出参数

(1)接口:

public interface StudentMapper {
     Integer countStudent();
}

(2)配置文件:

    <select id="countStudent" resultType="Integer">
        select count(*) from student
    </select>

(3)测试:

 public void countStudent() throws Exception {
            //加载核心配置文件
            String resource = "sqlMapConfig.xml";
            InputStream in = Resources.getResourceAsStream(resource);
            //创建SqlSessionFactory
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
            //创建SqlSession
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //SqlSEssion帮我生成一个实现类  (给接口)
            StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
            Integer countStudent = studentMapper.countStudent();
            System.out.println(countStudent);
    }

3、输出类型:ResultMap

(1)书写POJO:

public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
    private String snumber;
    private String sname;
    private String sex;
    private String birthday;
    private String classno;
    private String point;
    private String phone;
    private String email;
    public String getSnumber() {
        return snumber;
    }
    public void setSnumber(String snumber) {
        this.snumber = snumber;
    }
    public String getSname() {
        return sname;
    }
    public void setSname(String sname) {
        this.sname = sname;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getBirthday() {
        return birthday;
    }
    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }
    @Override
    public String toString() {
        return "Student [snumber=" + snumber + ", sname=" + sname + ", sex="
                + sex + ", birthday=" + birthday + ", classno=" + classno
                + ", point=" + point + ", phone=" + phone + ", email=" + email
                + "]";
    }
    public String getClassno() {
        return classno;
    }
    public void setClassno(String classno) {
        this.classno = classno;
    }
    public String getPoint() {
        return point;
    }
    public void setPoint(String point) {
        this.point = point;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
}

(2)接口:

public interface StudentMapper {
     List<Student> selectAllStudent();
}

(3)配置文件:

    <select id="selectAllStudent" resultType="pers.zhb.pojo.Student">
        select *  from student
    </select>

(4)书写测试类:

 public void selectAllStudent() throws Exception {
        //加载核心配置文件
        String resource = "sqlMapConfig.xml";
        InputStream in = Resources.getResourceAsStream(resource);
        //创建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
        //创建SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //SqlSEssion帮我生成一个实现类  (给接口)
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        List<Student> students = studentMapper.selectAllStudent();
        for(Student student:students){
            System.out.println(student);
        }
    }

(5)输出结果:

DEBUG [main] - Find JAR URL: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/mapper/StudentMapper.xml
DEBUG [main] - Not a JAR: file:/D:/IdeaProjects/MybatisDemo/web/WEB-INF/classes/pers/zhb/mapper/StudentMapper.xml
DEBUG [main] - Reader entry: <?xml version="1.0" encoding="UTF-8" ?>
DEBUG [main] - Checking to see if class pers.zhb.mapper.StudentMapper matches criteria [is assignable to Object]
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 517210187.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@1ed4004b]
DEBUG [main] - ==>  Preparing: select * from student 
DEBUG [main] - ==> Parameters: 
DEBUG [main] - <==      Total: 8
Student [snumber=null, sname=zhai, sex=男, birthday=1998-11-11, classno=tx171, point=890, phone=1234567890, email=null]
Student [snumber=null, sname=zhai2, sex=男, birthday=1998-11-11, classno=tx171, point=893, phone=19837372533, email=null]
Student [snumber=null, sname=zhai3, sex=男, birthday=1998-11-11, classno=tx171, point=892, phone=19837372534, email=null]
Student [snumber=null, sname=zhai3, sex=男, birthday=1998-11-11, classno=tx101, point=892, phone=19837372534, email=null]
Student [snumber=null, sname=qwerr, sex=男, birthday=1998-11-11, classno=tx101, point=892, phone=19837372534, email=null]
Student [snumber=null, sname=jiayou, sex=男, birthday=1998-11-11, classno=tx101, point=892, phone=19837372534, email=null]
Student [snumber=null, sname=null, sex=null, birthday=null, classno=2, point=null, phone=null, email=null]
Student [snumber=null, sname=null, sex=null, birthday=null, classno=2, point=null, phone=null, email=null]

可以看到snumber列的数据都为空,只是因为POJO中的列名与数据库中的列名不一致导致的(在sql类型处理器不能根据属性名查找数据库中的数据)。因为resultType可以指定将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。

(6)resultMap

结果集映射,映射为Student:

<id property="studentno" column="studentno"></id>

在<resultMap>标签中将数据库的字段与java类的属性进行一一映射(也可以只对不一样的字段和属性进行映射)

 <resultMap id="result" type="Student">
        <result column="studentno" property="snumber"></result>
    </resultMap>
    <select id="selectAllStudent" resultMap="result">
        select *  from student
    </select>

可以采用resultMap的方式,即使POJO中的属性与数据库的字段不一致,依旧能够查询成功。因为resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。

原文地址:https://www.cnblogs.com/zhai1997/p/12537090.html