您的位置 首页 java

09.mybatis返回值类型详解

返回值类型作为结果的返回,可以实现对数据成果的自动包装;

一、 表示简单类型

 /**
 * 根据学生姓名查询学生数量
 * @param name 学生姓名
 * @return
 */public  Integer selectCountByStuName(String name);  
 <!--    根据学生姓名查询学生数量-->
<select id="selectCountByStuName" resultType=" java .lang.Integer">
    select count(1)
    from student
    where name = #{name}
</select>  
 @Test
public  void  selectCountByStuName(){
    //        读取配置文件到数据流
    InputStream stream = Student.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
    //        创建SqlSessionFactory
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
    //创建 sql Session
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //获取StudentMapper
    StudentMapper  map per = sqlSession.getMapper(StudentMapper.class);
    //执行查询语句
     Integer  count =  Map per.selectCountByStuName("小明");
    //打印结果
    System.out.println("count = " + count);
}  

二、对象类型

resultType属性:在执行select时使用,作为标签的属性出现的。作为执行sql得到ResultSet转换的类型,使用类型的完全限定名或别名。注意如果返回的是集合,那应该设置为集合包含的类型(比如List<Student> 集合,应该使用Student的全类名),而不是集合本身。

它的值有两种类型,全限定名称和自定义别名:

(1) 全限定名称

使用java类型的全限定名称。表示的意思是 mybatis 执行sql,把ResultSet中的数据转为限定的类型的对象。

 <!--使用param占位符,注意不要写错 参数位置从1开始,这个需要注意 stu_name=#{param1}这个名称和方法名称可以不一样,使用的是 占位符 -->
<select id="selectStuByNameAndAge" result type ="student">
    select *
    from student
    where name = #{name}
      and age = #{age}
</select>

<!--    根据map查询学生信息-->
<select id="selectStuByMap" resultType="com.jiaguohui.mybatis.domain.Student">
    select *
    from student
    where name = #{name}
      and age = #{age}
</select>  

(2) 自定义别名

mybatis提供的对java类型定义简短,好记的名称。自定义别名有两种方式:分别为typeAlias和package;

① 方法一:使用typeAlias自定义别名:

 <!--    设置包的别名-->
<typeAliases>
    <!-- 指点单独的类起别名
        1.给具体的类起别名;
        2.type:制定具体的类全路径;
        3.alias:是起的别名,可以省略,在没有注解的情况下,会使用类的首字母小写的非限定类名来作为它的别名,(实测是不区分大小写的)              若有注解,则别名为其注解值;
     -->
    <typeAlias type="com.jiaguohui.mybatis.domain.Student" alias="student"/>

</typeAliases>  

优点: 别名可以自定义;

缺点: 每个类型必须单独定义;

方法二: package 自定义别名

主配置文件中配置报名,在mapper.xml中直接使用,这一种方式resultType中不区分大小写,所以全用小写同样也可以;

 <!--    设置包的别名-->
<typeAliases>
    <!--        
        1.给所有的实体类包起别名
        2.默认别名是这个类名字的首字母小写(实测是不区分大小写的)
    -->
    <package name="com.jiaguohui.mybatis.domain"/>
</typeAliases>  

优点: 使用方便,一次给多个类定义别名;

缺点: 别名不能随便定义,必须是类名;

这两种自定义别名都有优点和缺点,所以建议是不适用别名,就使用全限定名称,这样清晰明了。

三、返回结果是Map

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

查询结果如果是一个对象,可以封装为一个对象,如果是大于一个对象的字段,可以把查询结果封装到map里面,这样返回到前段,直接对应 json ;

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

 /**
 * 返回map类型查询结果
  * @param id 
 * @return
 */  
@MapKey("id")
public Map<String,String> getMapById(@Param("id") String id);  
 <select id="getMapById" resultType="java.util.Map">
    select * from student where id = #{id}
</select>  
 @Test
public void getMapById() {
    //        读取配置文件到数据流
    InputStream stream = Student.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
    //        创建SqlSessionFactory
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
    //创建SqlSession
    SqlSession sqlSession = sqlSessionFactory.openSession();
    //获取StudentMapper
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //执行查询语句
    Map<String, String> studentMap = mapper.getMapById("6");
    System.out.println(studentMap.get("id"));
}  

文章来源:智云一二三科技

文章标题:09.mybatis返回值类型详解

文章地址:https://www.zhihuclub.com/185184.shtml

关于作者: 智云科技

热门文章

网站地图