第六个MyBatis练习,查询返回Map,resultMap再使用

项目结构

1.创建Student实体类

package com.it.domain;

public class Student {
    private int sno;
    private String sname;
    private  String ssex;
    private int sage;
    private String sdept;

    public int getSno() {
        return sno;
    }

    public void setSno(int sno) {
        this.sno = sno;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSsex() {
        return ssex;
    }

    public void setSsex(String ssex) {
        this.ssex = ssex;
    }

    public int getSage() {
        return sage;
    }

    public void setSage(int sage) {
        this.sage = sage;
    }

    public String getSdept() {
        return sdept;
    }

    public void setSdept(String sdept) {
        this.sdept = sdept;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sno=" + sno +
                ", sname='" + sname + '\'' +
                ", ssex='" + ssex + '\'' +
                ", sage=" + sage +
                ", sdept='" + sdept + '\'' +
                '}';
    }
}

2.创建StudentDao接口类

package com.it.dao;

import com.it.domain.Student;

import java.util.List;
import java.util.Map;

public interface StudentDao {

    //返回类型是map
    Map<Object,Object> selectMapById(int id);

    //使用 resultMap定义映射关系
    List<Student> selectAllStudents();
}

3.创建StudentDao接口类的映射文件StudentDao.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.it.dao.StudentDao">

<!-- 返回Map,resultType可以等于它的别名map,也可以等于它的全限定名称,java.util.HashMap
1.列名是map的key值,列值是map的value;
2.返回map时,最多只能返回一行记录,多一行都是错误的。
注意:返回map用的比较少,推荐使用返回对象
-->
    <select id="selectMapById" resultType="map">
select sno,sname,sage,ssex,sdept from student where sno=#{renyi}
    </select>



<!--  使用resultMap
1.先定义resultMap
2.在result标签,使用resultMap来引用1步骤定义的
-->

<!--    定义resultMap
id:自定义的一个名称,来表示你定义的这个resultMap,
type:java类型的全限定名称
-->
    <resultMap id="studentMap" type="com.it.domain.Student">
<!--  主键列使用id标签,
column:数据库列名
property:java类型的属性名
-->
        <id column="sno" property="sno"/>
<!--     非主键使用result-->
        <result column="sname" property="sname"/>
        <result column="ssex" property="ssex"/>
        <result column="sage" property="sage"/>
        <result column="sdept" property="sdept"/>

    </resultMap>
    
    <select id="selectAllStudents" resultMap="studentMap">
        select sno,sname,sage,ssex,sdept from student
    </select>


</mapper>


4.创建MyBatis.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>

    <environments default="mydev">
        <environment id="mydev">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/it/dao/StudentDao.xml"/>
    </mappers>
</configuration>

5.创建MyBatisUntils工具类

package com.it.utils;

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 java.io.IOException;
import java.io.InputStream;

public class MyBatisUtils {
//    private static SqlSessionFactory factory=null;
//
//    static {
//        String mybatis="mybatis.xml";
//        InputStream resourceAsStream=null;
//        try {
//            resourceAsStream = Resources.class.getResourceAsStream(mybatis);
//            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//            factory= builder.build(resourceAsStream);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//
//    }
//
//    public static SqlSession getSqlSession(){
//        SqlSession sqlSession=null;
//        if (factory!=null){
//            sqlSession = factory.openSession(true);
//        }
//        return  sqlSession;
//    }

    private static SqlSessionFactory factory = null;

    static {
        String config="MyBatis.xml";
        InputStream resourceAsStream=null;
        try {
            resourceAsStream = Resources.getResourceAsStream(config);
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            factory = builder.build(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static SqlSession getSqlSession(){
        SqlSession sqlSession=null;
        if (factory!=null){
            sqlSession = factory.openSession(true);
        }
        return sqlSession;
    }


}

6.创建测试类

package com.it;

import static org.junit.Assert.assertTrue;

import com.it.dao.StudentDao;
import com.it.domain.Student;
import com.it.utils.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;
import java.util.Map;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
@Test
    public void selectMapByIdTest(){
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    StudentDao mapper = sqlSession.getMapper(StudentDao.class);
    Map<Object, Object> objectObjectMap = mapper.selectMapById(1);
    System.out.println(objectObjectMap);
}


@Test
    public void selectAllStudentsTest(){
    SqlSession sqlSession = MyBatisUtils.getSqlSession();
    StudentDao mapper = sqlSession.getMapper(StudentDao.class);
    List<Student> studentList = mapper.selectAllStudents();
    for (Student stu:studentList){
        System.out.println(stu);
    }
}


}

数据库数据

结果展示

1.返回值是Map类型

2. resultMap:结果映射, 指定列名和java对象的属性对应关系。resultMap和resultType不要一起用,二选一。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

做一道光

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值