前一篇: 11-Mybatis中mapper文件中#和$的区别https://blog.csdn.net/fsjwin/article/details/109672707
ResultType结果的类型有三种
1. 简单累心
2. 对象类型
3. map类型
1.简单对象
就是返回一个值,比如数据库的数量:
1.1 StudentDao
/**
* ResultType返回结果类型 简单类型
*
*/
public Integer selectStudentResultTypeInteger();
1.2 StudentDao.xml
<!-- ResultType返回结果类型-简单类型-->
<select id="selectStudentResultTypeInteger" resultType="java.lang.Integer">
select count(1) from student
</select>
1.3 测试
@Test
public void test12() {
SqlSession sqlsession = MybatisUtil.getSqlsession();
StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
//可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
Map map = new HashMap();
map.put("name", "yuhl");
map.put("age", 11);
Integer count = studentDao.selectStudentResultTypeInteger();
System.out.println(count);
}
1.4 测试结果
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 1920467934.
==> Preparing: select count(1) from student
==> Parameters:
<== Columns: count(1)
<== Row: 5
<== Total: 1
5
2.对象类型
2.1 StudentDao
/**
* ResultType返回结果类型Object
*
*/
public List<StudentVo> selectStudentResultType(Integer id);
2.2 StudentDao.xml
<!-- ResultType返回结果类型-->
<select id="selectStudentResultType" resultType="com.yuhl.vo.StudentVo">
select id,name,email,age from student where id=#{id}
</select>
2.3 测试
@Test
public void test11() {
SqlSession sqlsession = MybatisUtil.getSqlsession();
StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
//可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
Map map = new HashMap();
map.put("name", "yuhl");
map.put("age", 11);
List<StudentVo> studentList = studentDao.selectStudentResultType(1001);
studentList.forEach(stu -> System.out.println(stu));
}
2.4 测试结果
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 668210649.
==> Preparing: select id,name,email,age from student where id=?
==> Parameters: 1001(Integer)
<== Columns: id, name, email, age
<== Row: 1001, 张三, zhangsan@qq.com, 20
<== Total: 1
StudentVo{name='张三', age=20}
2.5 自定义别名
定义后在mapper中就可以写不用写那么长了。
2.5.1 自定义别名typeAlias mybatis.xml
<typeAliases>
<typeAlias type="com.yuhl.domain.Student" alias="sdfsafdsaf"/>
</typeAliases>
2.5.1 自定义别名package mybatis.xml
<typeAliases>
<package name="com.yuhl.domain"/>
<package name="com.yuhl.vo"/>
</typeAliases>
3.Map
只能返回一条数据哦!!!!
3.1 StudentDao
/**
* ResultType返回结果类型 简单类型
*
*/
public Map<Object,Object> selectStudentResultTypeMap(Integer id);
3.2 StudentDao.xml
<!-- ResultType返回结果类型-map类型-->
<select id="selectStudentResultTypeMap" resultType="hashmap">
select id,name from student where id = #{id}
</select>
3.3 测试
@Test
public void test13() {
SqlSession sqlsession = MybatisUtil.getSqlsession();
StudentDao studentDao = sqlsession.getMapper(StudentDao.class);
//可以吧查询的条件放进去,也可以使用一个单独的类ParaObject类封装就可以了。
Map<Object, Object> objectObjectMap = studentDao.selectStudentResultTypeMap(1001);
System.out.println(objectObjectMap);
}
3.4 测试结果
Checking to see if class com.yuhl.vo.StudentVo matches criteria [is assignable to Object]
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 838411509.
==> Preparing: select id,name from student where id = ?
==> Parameters: 1001(Integer)
<== Columns: id, name
<== Row: 1001, 张三
<== Total: 1
{name=张三, id=1001}
4.总结
ResultType结果的类型有三种
1. 简单累心
2. 对象类型
3. map类型
第三种不用,第二种用的最多,第一种次之~!
下一篇:13-Mybatis封装输出结果之二 – resultMaphttps://blog.csdn.net/fsjwin/article/details/109674185