5-输出结果

resultType

该标签表示执行sql语句返回值的类型,使用java类型的全限定名称或者MyBatis定义的别名。但是推荐使用全限定名称。

注意:如果返回的是集合,那么应该设置为集合包含的元素,而不是集合本身。

resultType和resultMap不能同时使用。

MyBatis框架内部会调用对应类的构造方法来创建对象,使用setXXX方法给属性赋值。

Map

Map数据类型可以作为sql查询的返回值,推荐使用Map<Object, Object>。但是注意:Map如果作为接口返回值,sql语句的查询结果只能有一条记录,多于一条记录就会出现错误。

其中,列名是Map的key,列值是Map的value。

例子:
DAO接口:

1
public HashMap<Object, Object> selectStudentById(Integer id);

映射文件:

1
2
3
<select id="selectStudentById" resultType="hashmap" >
select id, name, email, age from student where id = #{studentId}
</select>

测试代码:

1
2
3
4
5
6
7
8
@Test
public void testSelectStudentById() {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
HashMap<Object, Object> hashMap = studentDao.selectStudentById(1002);
System.out.println(hashMap);

}

运行结果:

image-20201203182849126

但是如果查询结果超过一条,就会报错。

resultMap

resultMap可以自定义sql的查询结果对应列值和java对象属性的映射关系。更灵活的把列值赋给指定属性。经常用在列名和Java对象属性名不一样的情况。

使用方法

  1. 首先定义resultMap,指定列名和属性的对应关系。

    语法格式为:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <resultMap id="" type="" >
    <!--主键字段使用id-->
    <id column="" property="" />
    <!--非主键使用result-->
    <result column="" property="" />
    <result column="" property="" />
    <result column="" property="" />
    ...
    </resultMap>

    其中resultMap的id属性是自定义的唯一值,在操作数据库标签的resultMap使用,,如

    type属性是期望转换成Java对象的全限定名称或别名。

    当列为主键使用id标签,非主键使用result字段,column是列名,property是属性名

  2. 在操作数据库如

例子:
DAO接口定义方法:

1
public List<Student> selectStudents();

映射文件内容:

1
2
3
4
5
6
7
8
9
10
<resultMap id="student" type="org.example.domain.Student" >
<id column="id" property="id" />
<result column="name" property="name" />
<result column="email" property="email" />
<result column="age" property="age" />
</resultMap>

<select id="selectStudents" resultMap="student" >
select id, name, email, age from student;
</select>

测试代码:

1
2
3
4
5
6
7
8
9
10
@Test
public void testSelectStudent() throws IOException {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao dao = sqlSession.getMapper(StudentDao.class);
List<Student> list = dao.selectStudents();
for(Student stu : list) {
System.out.println(stu);
}
sqlSession.close();
}

运行结果:

image-20201203184113423

列名和属性名不相同的处理方法

  1. 第一种方法就是使用之前讲的resultMap标签来定义。

  2. 使用select 列名 as 列别名,将列别名设为和属性名一致。

    例如:

    定义Java代码:

    1
    2
    3
    4
    private Integer stuId;
    private String name;
    private String email;
    private Integer age;

    映射文件中:

    1
    2
    3
    <select id="selectStudents"  resultType="org.example.domain.Student" >
    select id as StuId, name, email, age from student;
    </select>

模糊查询

如果要进行模糊查询,使用Like,有两种方式。跟MyBatis没有关系。就是sql语句,使用"%"进行查询。

一种是是在mapper文件中的sql语句中加入%,另一种就是调用方法传参数时加入"%"。

例子:
DAO接口方法:

1
public List<Student> selectStudentsByName(String name);

映射文件:

1
2
3
<select id="selectStudentsByName"  resultType="org.example.domain.Student" >
select id as StuId, name, email, age from student where name like #{name}
</select>

测试代码:

1
2
3
4
5
6
7
8
9
@Test
public void testSelectStudentByName() {
SqlSession sqlSession = MyBatisUtils.getSqlSession();
StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
List<Student> list = studentDao.selectStudentsByName("%赵%");
for(Student stu : list) {
System.out.println(stu);
}
}

运行结果:

image-20201203192134193

另一种方式就是在测试代码中去掉%,在映射文件中加入%,并使用双引号或者单引号括起来,就是:

1
2
3
<select id="selectStudentsByName"  resultType="org.example.domain.Student" >
select id as StuId, name, email, age from student where name like "%" #{name} "%"
</select>

5-输出结果
https://zhaoquaner.github.io/2022/05/11/MyBatis/5-输出结果/
更新于
2022年5月22日
许可协议