ssm合集---05 mybatis resultType和resultMap

mybatis的输出结果

resultType(A、简单类型),(B、 对象类型),(C、 Map)

resultMap

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

1.resultType(resultType: 执行 sql 得到 ResultSet 转换的类型,使用类型的完全限定名或别名。 注意如果返回的是集

合,那应该设置为集合包含的类型,而不是集合本身。 resultType resultMap ,不能同时使用。

1)简单类型

接口方法:
int countStudent();
mapper 文件:
 <!--sql执行后返回一行一列-->

    <!--resultType可以是别名或者全限定名称-->
<!--<select id="countStudent" resultType="int">-->
<select id="countStudent" resultType="java.lang.Integer">
    select count(*) from student
</select>
测试方法:

    @Test
    public void countStudent(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        int countStudent = dao.countStudent();
        System.out.println("一共有"+countStudent+"列");
    }
2) 对象类型
接口方法:
public Student selectStudentById(Integer id);
mapper 文件:
<select id="selectStudentById" resultType="com.zsz.domain.Student">
    select id,name,email,age from student where id=#{id}
</select>
测试方法:
  @Test
    public void testSelectStudentById(){

        SqlSession sqlSession= MybatisUtils.getSqlSession();
        StudentDao dao=sqlSession.getMapper(StudentDao.class);

        //调用dao的方法来执行数据库的操作
         Student student= dao.selectStudentById(1001);
        System.out.println("student="+student);


    }

3) Map

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

注意: Map 作为接口返回值, sql 语句的查询结果最多只能有一条记录。大于一条记录是错误。
接口方法:
 //定义方法返回map
    Map<Object,Object> selestStudentById(Integer id);
mapper 文件:
<!--返回map
        1)列名是map的key,列值是map的value
        2)返回map的时候只能返回一行数据,大于一行会报错  TooManyResultsException
    -->
<select id="selestStudentById" resultType="java.util.HashMap">
    select  id,name,email,age from student where id=#{id}
</select>
测试方法:
@Test
    public void testSelestStudentById(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);
        Map<Object, Object> objectObjectMap = dao.selestStudentById(1003);
        System.out.println("objectObjectMap==="+objectObjectMap);
    }
2.resultMap(resultMap 可以自定义 sql 的结果和 java 对象属性的映射关系。更灵活的把列值赋值给指定属性。
常用在列名和 java 对象属性名不一样的情况。
使用方式:
1. 先定义 resultMap, 指定列名和属性的对应关系。
2. <select> 中把 resultType 替换为 resultMap
接口方法:
/**
     * 使用resultMap定义映射关系
     *
     */

    List<Student> selectAllStudnets();
mapper 文件:
<!--使用resultMap
        1)先定义resultMap
        2)在select标签中,使用resultMap来引用已定义的
    -->


    <!--定义resultMap
        id:自定义名称,表示你自定义的这个resultMap
        type:java类型的全限定名称
    -->
    <resultMap id="studentMap" type="com.zsz.domain.Student">
        <!--列名和java属性的关系-->
        <!--主键列,使用id标签
            column:列名
            property:java类型的属性名
        -->
        <id column="id" property="id"></id>

        <!--非主键列使用result标签-->
        <result column="name" property="name"></result>
        <result column="email" property="email"/>
        <result column="age" property="age"/>



    </resultMap>
    <select id="selectAllStudnets" resultMap="studentMap">
        select id,name,email,age from student
    </select>
测试方法:
  @Test
    public void testSelectAllStudents(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);

       List<Student> resultMap=dao.selectAllStudnets();
        for (Student student:resultMap) {
            System.out.println("学生="+student);
        }
        sqlSession.close();

    }

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

1) 使用列别名和<resultType>

创建新的实体类(MyStudent)

private Integer stuId;
private String stuName;
private String stuEmail;
private Integer stuAge;

接口方法:
    List<MyStudent> selectDiffColProperty();
mapper 文件:
<!--列名和属性名不一样:resultType解决方式
      resultType的默认原则是 数据库中同名的列赋值给java代码中实体类中同名的属性(同名的列赋值给同名的属性),使用列别名(java对象的属性名)

    -->

    <select id="selectDiffColProperty" resultType="com.zsz.domain.MyStudent">
           select id as stuId,name as stuName,email as stuEmail,age as stuAge from student
    </select>
测试方法:
@Test
    public void testSelectDiffColProperty(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);

        List<MyStudent> resultMap=dao.selectDiffColProperty();
        for (MyStudent student:resultMap) {
            System.out.println("学生="+student);
        }
        sqlSession.close();

    }
2) 使用<resultMap>
接口方法:
    List<MyStudent> selectAllMyStudnet();
mapper 文件:
 <resultMap id="myStudentMap" type="com.zsz.domain.MyStudent">

        <id column="id" property="stuId"></id>

        <!--非主键列使用result标签-->
        <result column="name" property="stuName"></result>
        <result column="email" property="stuEmail"/>
        <result column="age" property="stuAge"/>
    </resultMap>
    <!--列名和属性名不一样:第一种解决方式-->
    <select id="selectAllMyStudnet" resultMap="myStudentMap">
        select id,name,email,age from student
    </select>
测试方法:
@Test
    public void testSelectAllMyStudent(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao dao = sqlSession.getMapper(StudentDao.class);

        List<MyStudent> resultMap=dao.selectAllMyStudnet();
        for (MyStudent student:resultMap) {
            System.out.println("学生="+student);
        }
        sqlSession.close();

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM框架中的MyBatis升级到MyBatis-Plus是可行的,可以实现共存。SSM框架由Spring、Spring MVC和MyBatis组成,而MyBatis-Plus是对MyBatis的增强扩展。下面将介绍如何将它们共存。 首先,需要将MyBatis升级到MyBatis-Plus。可以将MyBatis-Plus的依赖项添加到项目的pom.xml文件中,替换原有的MyBatis依赖。然后,需要对原有的MyBatis配置文件进行修改。MyBatis-Plus提供了一些方便的功能和特性,如自动填充、逻辑删除等,可以根据项目需求选择开启或关闭。 在SSM框架中,MyBatis-Plus可以与原有的Spring框架和Spring MVC框架完美共存。Spring框架负责管理和配置各种Bean,MyBatis-Plus可以与Spring框架一起使用,将其作为DAO层的组件进行管理。在Spring的配置文件中,可以将MyBatis-Plus的配置文件加入到配置中。 在Spring MVC框架中,可以继续使用原有的控制器、服务和视图解析器等组件。MyBatis-Plus可以与Spring MVC框架无缝集成,通过Spring MVC接收请求,然后调用MyBatis-Plus进行数据访问和处理。 在具体开发过程中,可以利用MyBatis-Plus提供的一些特性简化开发工作。例如,可以使用MyBatis-Plus的代码生成器来自动生成DAO、实体类和Mapper等代码,减少手动编写的工作量。 总结来说,将SSM框架中的MyBatis升级到MyBatis-Plus是完全可以实现的,它们可以共存并完美集成。通过使用MyBatis-Plus,我们可以更加便捷地开发和管理数据库操作,提高开发效率和代码质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值