MyBatis中通过Map进行查询时

  1. 单条查询时:

首先我们需要确定我们Student对应的属性,并且给对应的属性get和set方法,无参有参构造方法,并且重写他的toString方法

package com.cmj.entity;

public class Student {
	private int id;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	private String name;
	private int age;
	private String no;
	private String userClass;
	public String getUserClass() {
		return userClass;
	}
	public void setUserClass(String userClass) {
		this.userClass = userClass;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", no=" + no + ", userClass=" + userClass
				+ "]";
	}
}

在StudentMapper中进行对应的配置

public Map<String, Object> selectByIdMap(@Param("no") String no);

StudentMappe.java中对应的keyvalue,我们可以确认我们的key一般为String类型,如果说value无法确认类型,可以通过object来定义;就是后续可以为任何的类型

StudentMappe.xml中的配置:

<select id="selectByIdMap" resultType="map">
		select * from `student` where no = #{no}
		</select>

他的结果类型是map类型,resultType="map"

在对应的测试类中,怎么实现:

将都需要用到封装成一个方法进行调用:

private SqlSessionFactory getFactory() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream is = Resources.getResourceAsStream(resource);
		// 初始化mybatis,创建SqlSessionFactory类实例
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
		return sqlSessionFactory;
	}
测试
@Test
	public void testSelectByIdMap() throws IOException {
		SqlSession session=getFactory().openSession();
		StudentMapper mapper =session.getMapper(StudentMapper.class);

		Map<String, Object> map=mapper.selectByIdMap("3");
		System.out.println(map);
		
	}

就可以将ID3的这个内容通过map的方式全部都查询出来

  1. 多条数据查询时:

对应Student类与之前是一致的,但是StudentMapper中进行对应的配置需要注意

@MapKey("name")
public Map<String, Student> selectByNameMap(@Param("name") String name);

这边的@MapKey("name")

比如你查出来五个student放在mapvalue里面,那么你得指定一下用哪个字段作为mapkey,比如这个是通过name作为key,然后他的类型是String类型

StudentMappe.xml中的配置:

<select id="selectByNameMap" resultType="student">
		select * from `student` where name like #{name}
</select>

这块一般相对比较简单

测试类中我们进行对应的编写

@Test
	public void selectByNameMap() throws IOException {
		SqlSession session=getFactory().openSession();
		StudentMapper mapper =session.getMapper(StudentMapper.class);		
		Map<String, Student> selectByNameMap = mapper.selectByNameMap("%小%");
		System.out.println(selectByNameMap);
	}

这边插到我们的SQL语句中,就是将名字中含有小字的名单全部筛选出来,并且通过名字作为主键输出

  1. 表中出现联表内容的时,需要进行联表查询时

在学生类中,我们需要定义一个dept的类,获得这个类的属性

private String name;
	private int age;
	private String no;
	private String userClass;
	private Dept dept;

在dept中定义dept相关的属性值

public class Dept {
	private int id;
	private String name

并且生成对应的get和set方法、无参与有参的构造方法、重写他的toString的方法

在StudentMapper中写他的接口:

public Student selectByReturnMap (@Param("no") String no);

在对应的XML文档中进行接口的映射编写

<resultMap type="student" id="depMap">
	<id column="id" property="id"/>
	<result column="no" property="no"/>
	<result column="name" property="name"/>
	<result column="age" property="age"/>
	<result column="user_class" property="userClass"/>
	<result column="dept_id" property="dept.id"/>
	<result column="dname" property="dept.name"/>
	</resultMap>
	
	<select id="selectByReturnMap" resultMap="depMap">
	SELECT s.*,d.id did,d.`name` dname FROM student s,dept d where s.dept_id=d.id AND s.`no`=#{no}
	</select>

下方的resultMap与上方的id需要相匹配,映射至上方;type为本次输出的类型,为student这个类

在这前半部分为对应student表中的列名内容所以无需做太多的处理

后半部分这些,是studentdept表中相关联部分,

我们需要生成时,把对应的表列名进行重新定义为dept_id,而这个id是通过dept.id获得(就是dept这张表中的id字段)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值