12-Mybatis封装输出结果之一 -- resultType (含typeAliases)

本文详细介绍了Mybatis中ResultType的三种常见类型:简单类型返回整数,对象类型返回封装的VO对象,以及Map类型用于一对一结果。通过实例演示了每种类型的使用方法和测试结果,以帮助开发者理解并选择合适的返回类型。
摘要由CSDN通过智能技术生成

前一篇: 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值