0
点赞
收藏
分享

微信扫一扫

关于MyBatis 输出结果的封装

回溯 2022-03-30 阅读 89


封装 MyBatis 输出结果

resultType

resultType: 执行sql 得到 ResultSet 转换的类型,使用类型的完全限定名或别名。 注意如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身。resultType  和 resu ltMap,不能同时使用。

关于MyBatis 输出结果的封装_后端

A、 简单类型

接口方法:

int countStudent();

mapper 文件:

< select id="countStudent"

resultType="int"> select count(*) from

student

< /select>

测试方法:

@Test

public void testRetunInt(){

int count = studentDao.countStudent();

System.out.println("学生总人数:"+ count);

}

B、对象类型

接口方法:

​Student selectById(int id);​

mapper 文件:

<select id="selectById" 

resultType="com.bjpowernode.domain.Student"> select

id,name,email,age from student where id=#{studentId}

</select>

框架的处理: 使用构造方法创建对象。调用setXXX 给属性赋值。

​Student student = new Student();​

关于MyBatis 输出结果的封装_代码测试_02

注意:Dao 接口方法返回是集合类型,需要指定集合中的类型,不是集合本身。

关于MyBatis 输出结果的封装_java_03

C、Map

sql 的查询结果作为 Map 的key 和value。推荐使用Map<Object,Object>。

注意:Map 作为接口返回值,sql 语句的查询结果最多只能有一条记录。大于一条记录是错误。

接口方法:

​Map<Object,Object> selectReturnMap(int id);​

mapper 文件:

<select id="selectReturnMap" resultType="java.util.HashMap"> 

select name,email from student where id = #{studentId}

</select>

测试方法:

@Test

public void testReturnMap(){

Map<Object,Object> retMap = studentDao.selectReturnMap(1002);

System.out.println("查询结果是 Map:"+retMap);

}

resultMap

resultMap 可以自定义sql 的结果和 java 对象属性的映射关系。更灵活的把列值赋值给指定属性。

常用在列名和java 对象属性名不一样的情况。

使用方式:

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

2.在< select>中把 resultType 替换为resultMap。

接口方法:

List< Student> selectUseResultMap(QueryParam param);

mapper 文件:

关于MyBatis 输出结果的封装_MyBatis_04

测试方法:

关于MyBatis 输出结果的封装_后端_05

实体类属性名和列名不同的处理方式

(1) 使用列别名和< resultType>

步骤:

1. 创建新的实体类 PrimaryStudent

关于MyBatis 输出结果的封装_java_06

2. 接口方法

​List< PrimaryStudent> selectUseFieldAlias(QueryParam param);​

3. mapper 文件:

关于MyBatis 输出结果的封装_java_07

4. 测试方法

关于MyBatis 输出结果的封装_java_08

(2) 使用< resultMap>

步骤:

1. 接口方法

​List<PrimaryStudent> selectUseDiffResultMap(QueryParam param);​

2. mapper 文件:

关于MyBatis 输出结果的封装_java_09

3. 测试方法

关于MyBatis 输出结果的封装_sql_10

模糊 like

模糊查询的实现有两种方式, 一是java 代码中给查询数据加上“%” ; 二是在mapper 文件sql 语句的条件位置加上“%”

需求:查询姓名有“力”的

例 1: java 代码中提供要查询的 “%力%”

接口方法:

​List<Student> selectLikeFirst(String name);​

mapper 文件:

<select id="selectLikeFirst" 

resultType="com.bjpowernode.domain.Student"> select

id,name,email,age from student

where name like #{studentName}

</select>

测试方法:

@Test

public void

testSelectLikeOne(){ String

name="%力%";

List<Student> stuList = studentDao.selectLikeFirst(name);

stuList.forEach( stu -> System.out.println(stu));

}

例 2:mapper 文件中使用 like name "%" #{xxx} "%"

接口方法:

​List<Student> selectLikeSecond(String name);​

mapper 文件:

<select id="selectLikeSecond"

resultType="com.bjpowernode.domain.Student"> select id,name,email,age

from student

where name like "%" #{studentName} "%"

</select>

测试方法:

@Test

public void

testSelectLikeSecond(){ String

name="力";

List<Student> stuList = studentDao.selectLikeSecond(name);

stuList.forEach( stu -> System.out.println(stu));

}
@Testpublic void         testSelectLikeSecond(){ String         name="力";        List<Student> stuList = studentDao.selectLikeSecond(name);         stuList.forEach( stu -> System.out.println(stu));}复制代码


举报

相关推荐

0 条评论