一、先建立两个实体类和配置文件

配置文件

<?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>
<properties resource="orc_db.properties"></properties>
<typeAliases>
<package name="com.model"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"> </transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.dao"/>
</mappers>
</configuration>

student 和 studentinfo

package com.model;

import java.util.Date;

public class Student {

    private Integer sno;
private String sname;
private String ssex;
private Date sbirthday;
private Integer sclass; public Student(Integer sno, String sname, String ssex, Date sbirthday, Integer sclass) {
super();
this.sno = sno;
this.sname = sname;
this.ssex = ssex;
this.sbirthday = sbirthday;
this.sclass = sclass;
} public Student() {
super();
} @Override
public String toString() {
return "Student [sno=" + sno + ", sname=" + sname + ", ssex=" + ssex + ", sbirthday=" + sbirthday + ", sclass="
+ sclass + "]";
} public Integer getSno() {
return sno;
} public void setSno(Integer 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 Date getSbirthday() {
return sbirthday;
} public void setSbirthday(Date sbirthday) {
this.sbirthday = sbirthday;
} public Integer getSclass() {
return sclass;
} public void setSclass(Integer sclass) {
this.sclass = sclass;
} }
package com.model;

import java.util.Date;

public class Student {

    private Integer sno;
private String sname;
private String ssex;
private Date sbirthday;
private Integer sclass; public Student(Integer sno, String sname, String ssex, Date sbirthday, Integer sclass) {
super();
this.sno = sno;
this.sname = sname;
this.ssex = ssex;
this.sbirthday = sbirthday;
this.sclass = sclass;
} public Student() {
super();
} @Override
public String toString() {
return "Student [sno=" + sno + ", sname=" + sname + ", ssex=" + ssex + ", sbirthday=" + sbirthday + ", sclass="
+ sclass + "]";
} public Integer getSno() {
return sno;
} public void setSno(Integer 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 Date getSbirthday() {
return sbirthday;
} public void setSbirthday(Date sbirthday) {
this.sbirthday = sbirthday;
} public Integer getSclass() {
return sclass;
} public void setSclass(Integer sclass) {
this.sclass = sclass;
} }

二、配置文件和接口

package com.dao;

import java.util.List;

import com.model.StudentInfo;

public interface StudentInfoMapper {
/**
* 查询全部学生的信息
* @return
*/
public List<StudentInfo> selectAll();
}
package com.dao;

import java.util.List;
import java.util.Map;
import com.model.Student; public interface StudentMapper {
// public Integer addStu(Student student);
// public Integer delStu(Integer sno);
// public Integer updStu(Student student);
public List<Student> getStuByMap(Map<String,Object> map);
}
<?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.dao.StudentInfoMapper">
<resultMap type="studentInfo" id="Info">
<id property="id" column="id"/>
<result property="student.sno" column="sno"/>
<result property="student.sname" column="sname"/>
<result property="student.ssex" column="ssex"/>
<result property="student.sbirthday" column="sbirthday"/>
<result property="student.sclass" column="sclass"/> <result property="saddress" column="saddress"/>
</resultMap>
第二种联合查询方法
<resultMap type="studentInfo" id="Info1">
<association property="student" column="sno" select="com.dao.StudentMapper.getStuBySno"></association>
</resultMap>
<select id="selectAll" resultMap="Info1">
select * from studentinfo si left join student s on si.sno = s.sno
</select>
</mapper>
<?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.dao.StudentMapper">
<select id="getStuBySno" resultType="student">
select * from student s where s.sno=#{sno}
</select>
</mapper>

测试

package com.test;

import static org.junit.Assert.*;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.dao.StudentInfoMapper;
import com.model.StudentInfo;
import com.util.MybatisSqlFactory; public class Test2 { private SqlSession ss;
private StudentInfoMapper sm;
@Before
public void setUp() throws Exception {
ss = MybatisSqlFactory.getSqlSession();
sm =ss.getMapper(StudentInfoMapper.class);
} @After
public void tearDown() throws Exception {
ss.commit();
ss.close();
} @Test
public void test() {
List<StudentInfo> list = sm.selectAll();
for(StudentInfo data : list){
System.out.println(data);
}
} }

mybatis使用接口联合查询的更多相关文章

  1. MyBatis 多表联合查询,字段重复的解决方法

    MyBatis 多表联合查询,两张表中字段重复时,在配置文件中,sql语句联合查询时使用字段别名,resultMap中对应的column属性使用相应的别名: <resultMap type=&q ...

  2. MyBatis 多表联合查询及优化 以及自定义返回结果集

    下面就来说一下 mybatis 是通过什么来实现多表联合查询的.首先看一下表关系,如图: 这 里,我已经搭好了开发的环境,用到的是 SpringMVC + Spring + MyBatis,当然,为了 ...

  3. Mybatis实现联合查询(六)

    1. 疑问 在之前的章节中我们阐述了如何用Mybatis实现检查的查询,而我们实际的需求中,绝大部分查询都不只是针对单张数据表的简单查询,所以我们接下来要看一下Mybatis如何实现联合查询. 2. ...

  4. Mybatis联合查询(一)

    Mybatis的简单联合查询操作: 实体类: Employee: package com.test.mybatis; public class Employee { private Integer i ...

  5. MyBatis联合查询和使用association 进行分步式查询

    查询Emp的同时,查出emp对应的部门Department 方法1:联合查询,使用级联属性封装结果集 <!-- 联合查询,使用级联属性封装结果集 type:要自定义规则的javaBean类型 i ...

  6. mybatis:开发环境搭建--增删改查--多表联合查询(多对一)

    什么是mybatisMyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis使用简单的XML或 ...

  7. Mybatis中@select注解联合查询

    前言 在项目中经常会使用到一些简单的联合查询获取对应的数据信息,我们常规都是会根据对应的mapper接口写对应的mapper.xml的来通过对应的业务方法来调用获取,针对这一点本人感觉有点繁琐,就对@ ...

  8. MyBatis之三:多表联合查询

    在这篇文章里面主要讲解如何在mybatis里面使用一对一.一对多.多表联合查询(类似视图)操作的例子. 注:阅读本文前请先大概看一下之前两篇文章. 一.表结构 班级表class,学生表student, ...

  9. Mybatis.net与MVC入门配置及联合查询动态SQL拼接和简单事务

    第一次学习Mybatis.net,在博客园也找到好多资料,但是在配置成功之后也遇到了一些问题,尤其是在动态SQl拼接时候,这里把遇到的问题还有自己写的一个Demo贴出来,希望能帮到新手,有不适合的地方 ...

随机推荐

  1. postgres跨平台开发坑之空值

    ngx_lua架构下查询linux版postgres时,如果目标字段的值返回空,则返回结果为 ngx.null,同样的代码如果查询windows版postgres时,如果目标字段的值返回空,则返回结果 ...

  2. UML类图新手入门级介绍(转)

    首先,看动物矩形框,它代表一个类(Class).类图分三层,第一层显示类的名称,如果是抽象类,则就用斜体显示.第二层是类的特性,通常就是字段和属性.第三层是类的操作,通常是方法或行为.前面的符号,+ ...

  3. linux常用英文单词记录

    1.skip 跳过忽略 2.next 下一步3.hostname 主机名4.password 密码5.complete 完成6.network 网络7.conf config configuratio ...

  4. 记一次JAVAWEB项目部署

    需求 原本服务器上tomcat部署了一个javaweb项目在80端口,这次要部署另一个javaweb项目在8090端口,或者同时部署在同一端口不同目录下. 解决方法 不同端口部署 不同端口部署我们需要 ...

  5. 算法(第四版)C# 习题题解——1.4

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 这一节内容可能会用到的库文件有 ...

  6. JS版剑指offer

    介绍 用JavaScript刷完了剑指offer,故总结下每道题的难度.解决关键点,详细题解代码可以点链接进去细看. 关于JS刷题的技巧可以看我之前的这篇:JS刷题总结. 剑指offer的题目在牛客网 ...

  7. cv2.getRotationMatrix2D函数

  8. [转载]linux下网卡漂移导致网络不可用

    转自:https://blog.csdn.net/hyatsz/article/details/47690993 linux下网卡漂移导致网络不可用 2015年08月16日 00:48:50 hyat ...

  9. 1、docker容器技术基础入门

    Docker和传统虚拟机的区别               参考文章: https://lwn.net/Articles/531114/    操作中的命名空间详解 https://blog.yadu ...

  10. web 页面间传值 js 封装方法

    用法 var id = getParam("id"); function getParam(strKey) { var url=document.URL; //var url=&q ...