接着上一篇博客OnToOne继续探讨mybatis多表查询
OneToMany,在t_group表中一个组对应多个人员信息,根据一个组去查询该组下的所有人员信息
1、修改sql映射文件GroupPersonUserMapper.xml
<select id="findGroupAndPersonByGID" resultMap="GroupAndPerson"> select g.id,g.group_name,g.description, p.gid,p.id pid,p.name,p.telphone,p.address,p.email from t_group g left join t_person p on g.id = p.gid where g.id = #{id} </select> <resultMap type="com.zlt.mybatis.model.Group" id="group"> <id property="id" column="id"/> <result property="groupName" column="group_name"/> <result property="description" column="description" /> </resultMap> <resultMap type="com.zlt.mybatis.model.Group" id="GroupAndPerson" extends="group"> <collection property="persons" column="gid" ofType="com.zlt.mybatis.model.Person"> <id property="id" column="pid"/> <result property="name" column="name"/> <result property="telphone" column="telphone"/> <result property="address" column="address"/> <result property="email" column="email"/> <result property="gid" column="gid"/> </collection> </resultMap>
2、单元测试用例
public void testOneToMany(){ SqlSession session = null; try{ session = sqlSessionFactory.openSession(); String statement = "com.zlt.mybatis.model.Group.findGroupAndPersonByGID"; //List<Group> groups = session.selectList(statement, 1); Group group = session.selectOne(statement, 1); if(null != group) { StringBuffer sb = new StringBuffer(); sb.append("id:" + group.getId() + "\t") .append("groupName:" + group.getGroupName() +"\t") .append("description:" + group.getDescription() +"\t"); String len = sb.toString(); List<Person> persons = group.getPersons(); for (Person person : persons) { sb.append("id:" + person.getId() + "\t") .append("name:" + person.getName() + "\t") .append("telphone:" + person.getTelphone() + "\t") .append("address:" +person.getAddress() + "\t") .append("email:" + person.getEmail() + "\t") .append("gid:" + person.getGid() + "\t"); System.out.println(sb.toString()); sb.delete(len.length(), sb.length()); } } }finally{ if(null != session) session.close(); } }
3、测试结果
id:1 groupName:Java开发部 description:Java项目组开发 id:1 name:张三 telphone:10086 address:浦东新区广兰路 email:zhangsan@163.com gid:1 id:1 groupName:Java开发部 description:Java项目组开发 id:2 name:李四 telphone:10000 address:浦东新区广兰路 email:lisi@163.com gid:1
ManyToOne
1、修改SQL映射文件
<select id="findGroupAndPersonByGID2" resultMap="GroupAndPerson2"> select p.id id,p.name,p.telphone,p.address,p.email,p.gid, g.id gid,g.group_name,g.description from t_person p left join t_group g on p.gid = g.id where p.id = #{id} </select> <resultMap type="com.zlt.mybatis.model.Person" id="GroupAndPerson2"> <id property="id" column="id"/> <result property="name" column="name"/> <result property="telphone" column="telphone"/> <result property="address" column="address"/> <result property="email" column="email"/> <result property="gid" column="gid"/> <association property="group" javaType="com.zlt.mybatis.model.Group"> <id property="id" column="gid"/> <result property="groupName" column="group_name"/> <result property="description" column="description" /> </association> </resultMap>
2、单元测试
public void testManyToOne(){ SqlSession session = null; try{ session = sqlSessionFactory.openSession(); String statement = "com.zlt.mybatis.model.Group.findGroupAndPersonByGID2"; Person person = session.selectOne(statement, 2); if(null != person) { StringBuffer sb = new StringBuffer(); sb.append("id:" + person.getId() + "\t") .append("name:" + person.getName() + "\t") .append("telphone:" + person.getTelphone() + "\t") .append("address:" +person.getAddress() + "\t") .append("email:" + person.getEmail() + "\t") .append("gid:" + person.getGid() + "\t"); Group group = person.getGroup(); sb.append("group-id:" + group.getId() + "\t") .append("groupName:" + group.getGroupName() +"\t") .append("description:" + group.getDescription() +"\t"); System.out.println(sb.toString()); } }finally{ if(null != session) session.close(); } }
3、测试结果
id:2 name:李四 telphone:10000 address:浦东新区广兰路 email:lisi@163.com gid:1 group-id:1 groupName:Java开发部 description:Java项目组开发
相关推荐
主要介绍了Mybatis基于注解实现多表查询功能,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
mybatis一对多的查询方法详解! mybatis一对多的查询方法详解! mybatis一对多的查询方法详解! mybatis一对多的查询方法详解!
springboot整合mybatis-plus实现多表分页查询,assocication和collection一对一,一对多关联。简单易懂,容易上手!
springboot和mybatis整合(单多表),以注册登录为案例,并且根据文章查询作者关联
Mybatis框架(多表查询典型案例)
MyBatis Plus 的多表联接、分页查询实现方法 http://localhost/common/getAllQuestionByPage/1/10 多表关联、分页查询接口 http://localhost/common/getAllQuestionWithStudentByPage/1/10 多表关联、分页带参数查询...
主要介绍了Java的MyBatis框架中实现多表连接查询和查询结果分页,借助MyBatis框架中带有的动态SQL查询功能可以比普通SQL查询做到更多,需要的朋友可以参考下
mybatis多表查询.zip
mybatis之多对多
文档中主要说明:(1)多表联合查询步骤,快捷便利;(2)数据批量插入,以及相关的案例,节省运行资源。
MyBatis的学习代码MyBatis的学习代码MyBatis的学习代码MyBatis的学习代码MyBatis的学习代码MyBatis的学习代码MyBatis的学习代码MyBatis的学习代码MyBatis的学习代码MyBatis的学习代码MyBatis的学习代码MyBatis的学习...
Mybatis多参数查询与列表查询不同方式实现,效果看博文 http://blog.csdn.net/evankaka/article/details/45671473
mybatis学习
mybatis实战教程mybatis in action之四实现关联数据的查询 mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action...
主要介绍了MyBatis-Plus多表联合查询并且分页(3表联合),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
MyBatis - CRUD + 多表联合查询 一个简单的demo
支持连表查询的mybatis-plus,mybatis-plus风格的连表操作提供 wrapper.leftJoin() wrapper.rightJoin()等操作
主要介绍了Spring boot2基于Mybatis实现多表关联查询,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
"Spring 多数据源处理_Mybatis 实现跨库查询" 本文主要讨论了 Spring 框架中多数据源的处理方法,特别是使用 Mybatis 实现跨库查询。在大型应用中,为了提高系统的水平伸缩性,需要对数据进行切分,并且采用多个...