欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

mybatis Mapper的xml文件中resultType值的使用说明

程序员文章站 2022-06-24 19:30:08
目录mapper的xml文件中resulttype值②当返回类型是javabean③当返回是list类型④返回类型数map结构mybatis学习之resulttype解析2、list类型3、map类型...

mapper的xml文件中resulttype值

①返回一般数据类型的值

比如根据id或者字段条件查询获取表中的某一个字段值

user sel(int id); //根据id查询

sql映射文件

<select id="sel" resulttype="java.lang.string"> //注意这个写了类全名 
    select username from user_test where id = #{id}
</select>  

如果需要简写的话 需要定义resulttype的别名

java 的基本类型不需要别名的:

别名 映射的类型
_byte byte
_long long
_short short
_int int
_boolean boolean
integer integer
string string
date date
boolean boolean

②当返回类型是javabean

<select id="sel" resulttype="com.tx.springboottestdemo.entity.user">
    select * from user_test where id = #{id}
</select>
<typealiases>  
<!-- 针对单个别名定义 type:类型的路径 alias:别名 -->  
<typealias type="cn.itcast.mybatis.po.user" alias="user"/>  

③当返回是list类型

有时候我们需要模糊查询或者是全表查询,返回的数据是多条的, 那么可以把多条数据保存到list里面的。

mapper 接口

list<user> getusers();

sql映射文件:

<select id="getusers" resulttype="com.tx.entity.user">
select * from user
</select>

这里需要注意的是返回是list类型 但是resulttype依然是javabean, 有些人会困惑这里怎么不是集合类型呢?其实透过现象看本质, 还是javabean。

④返回类型数map结构

当我们在查询的时候返回一条数据的时候,我们可以把{字段名,字段值}封装成map结构。

map<string, object> finduserbyname(integer id);

sql 映射文件:

<select id="finduserbyname" resulttype="string">
select username from user where id=#{id};
</select>

⑤说一下关于mybatis里面mapper层中传入多个参数的方法

1.其实可以看成是多个参数的

public list<user> finduser( string name1,  string name2);

对应的sql映射文件:

<select id="finduser3"  resulttype="com.tx.springboottestdemo.entity.user">
    select * from user_test where username = #{0} and realname = #{1}
</select> 

其中里面#{0}, #{1}默认是按照mybatis传值的顺序位置索引的 但是在springboot2.1(集成mybatis框架)里面会报错,我看网上说是可以的 我尝试很多好像不行,下面贴出error:

~~org.mybatis.spring.mybatissystemexception: nested exception is org.apache.ibatis.binding.bindingexception: parameter '0' not found. available parameters are [arg1, arg0, param1, param2]~~

2.可以看做是加了注解

public list<user> finduser( @param("name1") string name1, @param("name2") string name2);

对应的sql文件:

<select id="finduser" resulttype="com.tx.springboottestdemo.entity.user">
    select * from user_test where username = #{name1} and realname = #{name2}
</select>

3.可以把参数封装到map里面

有些时候我们的业务数据查询没有定义对应的pojo,就进行参数的封装操作。

public list<user> finduser1(map<string, object> map);

对应的sql文件:

<select id="finduser1" parametertype="java.util.map" resulttype="com.tx.springboottestdemo.entity.user">
    select * from user_test where username = #{username} and realname = #{realname}
</select>

mybatis学习之resulttype解析

resulttype是sql映射文件中定义返回值类型,返回值有基本类型,对象类型,list类型,map类型等。现总结一下再解释

总结

resulttype:

1、基本类型:resulttype=基本类型

2、list类型:resulttype=list中元素的类型

3、map类型:

单条记录:resulttype =map

多条记录:resulttype =map中value的类型

1、对象类型

对于对象类型resulttype直接写对象的全类名就可以了

实例:

hotelmapper接口

package com.pjf.mybatis.dao;
import com.pjf.mybatis.po.hotel;
public interface hotelmapper {
    //返回值类型为hotel
    public hotel gethotel(integer i);
}

hotelmapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pjf.mybatis.dao.hotelmapper">
    <!--resulttype直接写对象的全类名 -->
    <select id="gethotel" resulttype="com.pjf.mybatis.po.hotel">
        select * from hotel
        where
        id=#{id}
    </select>
</mapper>

测试类:

package com.pjf.mybatis;
import java.io.ioexception;
import java.io.inputstream;import org.apache.ibatis.io.resources;
import org.apache.ibatis.session.sqlsession;
import org.apache.ibatis.session.sqlsessionfactory;
import org.apache.ibatis.session.sqlsessionfactorybuilder;
import org.junit.test;
import com.pjf.mybatis.dao.hotelmapper;
import com.pjf.mybatis.po.hotel;
public class testhotel {
    public sqlsessionfactory sqlsessionfactory() throws ioexception {
        // mybatis的配置文件
        string resource = "mybatis_config.xml";
        // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)testhotel.class.getclassloader()
        inputstream is = resources.getresourceasstream(resource);
        // 构建sqlsession的工厂
        sqlsessionfactory sessionfactory = new sqlsessionfactorybuilder().build(is);
        return sessionfactory;
    }
    // 查
    @test
    public void gethotel() throws ioexception {
        sqlsessionfactory sessionfactory = sqlsessionfactory();
        sqlsession session = sessionfactory.opensession();
        hotelmapper hotelmapper = session.getmapper(hotelmapper.class);
        system.out.println(hotelmapper.getclass());
        //直接返回hotel对象,打印出来        
        hotel hotel = hotelmapper.gethotel(1001);
        system.out.println(hotel);
        session.close();
    }
}

2、list类型

返回值为list类型,resulttype为list中对象的类型,如list<hotel>,resulttype为hotel

实例:

hotelmapper接口

package com.pjf.mybatis.dao;
import java.util.list;
import com.pjf.mybatis.po.hotel;
public interface hotelmapper {
    // 返回值为list
    public list<hotel> gethotel(integer i);
}

hotelmapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pjf.mybatis.dao.hotelmapper">
    <!-- 返回值为list,resulttype为list中元素的全类名 -->
    <select id="gethotel" resulttype="com.pjf.mybatis.po.hotel">
        select * from hotel
        where
        price>#{price}
    </select>
</mapper>

测试类:

package com.pjf.mybatis;
import java.io.ioexception;
import java.io.inputstream;
import org.apache.ibatis.io.resources;
import org.apache.ibatis.session.sqlsession;
import org.apache.ibatis.session.sqlsessionfactory;
import org.apache.ibatis.session.sqlsessionfactorybuilder;
import org.junit.test;
import com.pjf.mybatis.dao.hotelmapper;
import com.pjf.mybatis.po.hotel;
public class testhotel {
    public sqlsessionfactory sqlsessionfactory() throws ioexception {
        // mybatis的配置文件
        string resource = "mybatis_config.xml";
        // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)testhotel.class.getclassloader()
        inputstream is = resources.getresourceasstream(resource);
        // 构建sqlsession的工厂
        sqlsessionfactory sessionfactory = new sqlsessionfactorybuilder().build(is);
        return sessionfactory;
    }
    // 查
    @test
    public void gethotel() throws ioexception {
        sqlsessionfactory sessionfactory = sqlsessionfactory();
        sqlsession session = sessionfactory.opensession();
        hotelmapper hotelmapper = session.getmapper(hotelmapper.class);
        system.out.println(hotelmapper.getclass());
        // 返回值为list
        list<hotel> list = hotelmapper.gethotel(1000);
        for (hotel hotel : list) {
            system.out.println(hotel);
        }
        session.close();
    }
}

3、map类型

a、返回单条记录的map,key为属性,值为属性值。resulttype为map

hotelmapper接口

package com.pjf.mybatis.dao;
import java.util.map;
import com.pjf.mybatis.po.hotel;
public interface hotelmapper {
    // 返回值为map,key为属性名,value为属性值
    public map<string, object> gethotel(integer i);
}

hotelmapper.xml

<?xml version="1.0" encoding="utf-8" ?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pjf.mybatis.dao.hotelmapper">
    <!-- 返回值为map,resulttype为map -->
    <select id="gethotel" resulttype="map">
        select * from hotel
        where
        id=#{id}
    </select>
</mapper>

测试类:返回id=1001的酒店

package com.pjf.mybatis;
import java.io.ioexception;
import java.io.inputstream;
import java.util.map;
import org.apache.ibatis.io.resources;
import org.apache.ibatis.session.sqlsession;
import org.apache.ibatis.session.sqlsessionfactory;
import org.apache.ibatis.session.sqlsessionfactorybuilder;
import org.junit.test;
import com.pjf.mybatis.dao.hotelmapper;
import com.pjf.mybatis.po.hotel;
public class testhotel {
    public sqlsessionfactory sqlsessionfactory() throws ioexception {
        // mybatis的配置文件
        string resource = "mybatis_config.xml";
        // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)testhotel.class.getclassloader()
        inputstream is = resources.getresourceasstream(resource);
        // 构建sqlsession的工厂
        sqlsessionfactory sessionfactory = new sqlsessionfactorybuilder().build(is);
        return sessionfactory;
    }
    // 查
    @test
    public void gethotel() throws ioexception {
        sqlsessionfactory sessionfactory = sqlsessionfactory();
        sqlsession session = sessionfactory.opensession();
        hotelmapper hotelmapper = session.getmapper(hotelmapper.class);
        system.out.println(hotelmapper.getclass());
        // 返回值为map
        map<string, object> map = hotelmapper.gethotel(1001);
        system.out.println(map);
        session.close();
    }
}

b、返回多条记录的map,key为任意一属性,值为对象类型。如map<string,hotel>,resulttype为hotel

返回多条记录的map时,key为任意一属性,值为对象类型,不过key需要通过@mapkey("hotelname")指定对象中一个属性名为key

实例:

hotelmapper接口

package com.pjf.mybatis.dao;
import java.util.map;
import org.apache.ibatis.annotations.mapkey;
import com.pjf.mybatis.po.hotel;
public interface hotelmapper {
    // 返回值为map,key需要通过@mapkey("属性名")来指定javabean中的一个属性名为key,value为对象
    @mapkey("hotelname")
    public map<string, hotel> gethotel(integer i);
}

hotelmapper.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.pjf.mybatis.dao.hotelmapper">
    <!-- 返回值为map,resulttype为对象的全类名 -->
    <select id="gethotel" resulttype="com.pjf.mybatis.po.hotel">
        select * from hotel
        where
        price>#{price}
    </select>
</mapper>

测试类:返回价格>1000以上的酒店

package com.pjf.mybatis;
import java.io.ioexception;
import java.io.inputstream;
import java.util.map;
import org.apache.ibatis.io.resources;
import org.apache.ibatis.session.sqlsession;
import org.apache.ibatis.session.sqlsessionfactory;
import org.apache.ibatis.session.sqlsessionfactorybuilder;
import org.junit.test;
import com.pjf.mybatis.dao.hotelmapper;
import com.pjf.mybatis.po.hotel;
public class testhotel {
    public sqlsessionfactory sqlsessionfactory() throws ioexception {
        // mybatis的配置文件
        string resource = "mybatis_config.xml";
        // 使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)testhotel.class.getclassloader()
        inputstream is = resources.getresourceasstream(resource);
        // 构建sqlsession的工厂
        sqlsessionfactory sessionfactory = new sqlsessionfactorybuilder().build(is);
        return sessionfactory;
    }
    // 查
    @test
    public void gethotel() throws ioexception {
        sqlsessionfactory sessionfactory = sqlsessionfactory();
        sqlsession session = sessionfactory.opensession();
        hotelmapper hotelmapper = session.getmapper(hotelmapper.class);
        system.out.println(hotelmapper.getclass());
        // 返回值为map
        map<string, hotel> map = hotelmapper.gethotel(1000);
        system.out.println(map);
        session.close();
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。