mybatis遇到的一个问题

cumt_TTR cumt_TTR 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
public class Employee
{
private int id;
private String name;
private int age;
private int gender;

//get、set方法。。。。
}

public class Department
{
private int id;
private String name;
private int accounts;
private List<Employee> employees;

//get、set方法。。。。
}

===============语句映射文件================
<mapper namespace="inter.InterOperation">
<select id="getDepartment" resultMap="resultDepartment" >
select d.did,
d.name,
d.accounts,
e.eid,
e.age,
e.name1,
e.gender
from
department d,employee e
where e.employee_department_id=d.did and d.did=#{id}
</select>

<resultMap type="Department" id="resultDepartment">
<id property="did" column="id"/>
<result property="name" column="name"/>
<result property="accounts" column="accounts"/>
<collection property="employees" ofType="Employee">
<id property="eid" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="gender" column="gender"/>
</collection>
</resultMap>
</mapper>

=============测试代码=====================

InterOperation interOperation = sqlSession
.getMapper(InterOperation.class);
Department d = interOperation.getDepartment(1);
System.out.println("d.getName:"+d.getName());
assertEquals("dep1", d.getName());
System.out.println(d.getEmployees().size());
for(Employee e:d.getEmployees())
System.out.println("e.getName:"+e.getName());
//在这里打印出的本应该是Employee所对应的name,发现打印出的都是Department的name
发现解决办法是把数据库里的Employee的name变成name1

mybatis这么锉???还是我用的不对??
还有,各位有好的mybatis的教程推荐么,官方的教程好像太简略了
}
...全文

相关推荐

mybatis创建一个或多个新用户 insert 字段和表名不确定时动态添加问题

在MyBatis中,有时我们需要处理动态SQL,特别是在插入数据时,字段和表名可能根据不同的情况变化。本文将深入探讨如何在MyBatis中解决插入一个或多个新用户时,字段和表名不确定的问题。

weixin_38711149

搭建一个简单的maven+mybatis项目

本文介绍了如何搭建一个简单的maven+mybatis项目。首先,通过新建maven项目并配置pom.xml引入mybatis和mysql依赖。接着,创建实体类、mybatis配置文件和映射文件,并编写测试类进行验证。在遇到编译错误时,调整了IDE的编译器设置以解决问题。最后,项目成功运行。

kzmzzz

springboot+mybatis+vue实现一个简单的注册登录功能

该博客详细介绍了如何使用SpringBoot、Mybatis和Vue.js构建一个简单的注册登录功能。从mybatis和vue的基本概念介绍,到数据库设计、SpringBoot工程的创建,包括实体类、映射类、服务层和控制层的编写,再到前端页面的实现,最后进行了结果测试。涉及的数据库表包括用户名和密码字段,解决了字段类型匹配问题。

HachiLin

Mybatis狂神视频学习详细笔记

该博客是MyBatis学习笔记,涵盖基础知识、第一个MyBatis程序、CRUD操作、配置解析等内容。介绍了搭建环境、编写代码、测试的步骤,讲解了使用Map传递参数、解决属性名和字段名不一致问题的方法,还提及日志、分页、注解开发、缓存等方面的知识。

xiangminlu

Mybatis实体类和表映射问题(推荐)

在这种情况下,我们需要通过特定的方式来解决字段名与属性名不匹配的问题,确保数据的正确读写。以下将详细介绍如何在Mybatis中处理实体类和表映射的差异。首先,创建一个符合业务需求的实体类。

weixin_38741075
评论 5
收藏 已收藏
分享
返回顶部
cumt_TTR
Java EE 67,535 成员 225,843 内容