mybatis使用接口联合查询
一、先建立两个实体类和配置文件
配置文件
<?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使用接口联合查询的更多相关文章
- MyBatis 多表联合查询,字段重复的解决方法
MyBatis 多表联合查询,两张表中字段重复时,在配置文件中,sql语句联合查询时使用字段别名,resultMap中对应的column属性使用相应的别名: <resultMap type=&q ...
- MyBatis 多表联合查询及优化 以及自定义返回结果集
下面就来说一下 mybatis 是通过什么来实现多表联合查询的.首先看一下表关系,如图: 这 里,我已经搭好了开发的环境,用到的是 SpringMVC + Spring + MyBatis,当然,为了 ...
- Mybatis实现联合查询(六)
1. 疑问 在之前的章节中我们阐述了如何用Mybatis实现检查的查询,而我们实际的需求中,绝大部分查询都不只是针对单张数据表的简单查询,所以我们接下来要看一下Mybatis如何实现联合查询. 2. ...
- Mybatis联合查询(一)
Mybatis的简单联合查询操作: 实体类: Employee: package com.test.mybatis; public class Employee { private Integer i ...
- MyBatis联合查询和使用association 进行分步式查询
查询Emp的同时,查出emp对应的部门Department 方法1:联合查询,使用级联属性封装结果集 <!-- 联合查询,使用级联属性封装结果集 type:要自定义规则的javaBean类型 i ...
- mybatis:开发环境搭建--增删改查--多表联合查询(多对一)
什么是mybatisMyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis使用简单的XML或 ...
- Mybatis中@select注解联合查询
前言 在项目中经常会使用到一些简单的联合查询获取对应的数据信息,我们常规都是会根据对应的mapper接口写对应的mapper.xml的来通过对应的业务方法来调用获取,针对这一点本人感觉有点繁琐,就对@ ...
- MyBatis之三:多表联合查询
在这篇文章里面主要讲解如何在mybatis里面使用一对一.一对多.多表联合查询(类似视图)操作的例子. 注:阅读本文前请先大概看一下之前两篇文章. 一.表结构 班级表class,学生表student, ...
- Mybatis.net与MVC入门配置及联合查询动态SQL拼接和简单事务
第一次学习Mybatis.net,在博客园也找到好多资料,但是在配置成功之后也遇到了一些问题,尤其是在动态SQl拼接时候,这里把遇到的问题还有自己写的一个Demo贴出来,希望能帮到新手,有不适合的地方 ...
随机推荐
- 自制操作系统Antz(14)——实现内置编程语言(词法解析器)
AntzScript
- Linux 设置系统时间和时区1.Centos
- orm 扩展
"""ORM小练习 如何在一个Python脚本或文件中 加载Django项目的配置和变量信息""" import os if __name_ ...
- 编码原则 之 Once and Only Once
原文 The Once and Only Once principle can be thought of as a subset of the Don’t Repeat Yourself princ ...
- Eclipse使用Maven,创建项目出现:Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resour
使用maven创建简单的项目时候经常会遇到 Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resource ...
- IP包设计
IP包 IP核(Intellectual Property core)就是知识产权核或知识产权模块的意思,用于配置FPGA或其它硅芯片上的逻辑资源. 引用链接https://blog.csdn.net ...
- Python简单实现决策树
__author__ = '糖衣豆豆' #决策树 import pandas as pda fname="~/coding/python/data/lesson.csv" data ...
- 根据不同浏览器判断OCX插件是否安装
最近项目进入到了验收阶段,需要兼容不同的浏览器,海康的Demo写了一个判断插件是否成功安装的函数,但是经过测试,只在IE浏览器下有效果,在其他的浏览器下面会出现Bug,现在需要写一个通用的方法,在不同 ...
- 为wordpress后台登陆添加算术验证码
对于新建站(个人博客-柠檬https://ninmong.com)的站长来说提高后台的安全性,是一件非常重要的事,添加验证可以起到很好的效果,废话少说,贴代码 //后台登陆数学验证码 function ...
- y
switch(update_state) { : switch(num){ : window.progressn=num $('#h_progress_bar .ui-progress').anima ...