One 、 First, create two entity classes and configuration files

The configuration file

<?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 and 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;
} }

Two 、 Configuration files and interfaces

package com.dao;
import java.util.List;
import com.model.StudentInfo;
public interface StudentInfoMapper {
/**
* Check the information of all the students
* @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">
The first joint query method
<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>
The second joint query method
<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>

test

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 More related articles about joint query using interface

  1. MyBatis Multi table joint query , Solutions to duplicate fields

    MyBatis Multi table joint query , When fields in two tables are repeated , In the configuration file ,sql Statement union query using field alias ,resultMap Corresponding column Property uses the corresponding alias : <resultMap type=&q ...

  2. MyBatis Multi table joint query and optimization And custom return result set

    Let's talk about mybatis How to realize multi table joint query . First, let's look at the table relationship , Pictured : this in , I've set up the development environment , What is used is SpringMVC + Spring + MyBatis, Of course , in order to ...

  3. Mybatis Realize joint query ( 6、 ... and )

    1. doubt In the previous chapter, we explained how to use Mybatis Realize the query of checking , And in our actual needs , Most queries are not just simple queries for a single data table , So we're going to take a look at Mybatis How to implement joint query . 2. ...

  4. Mybatis The joint query ( One )

    Mybatis Simple union query operation : Entity class : Employee: package com.test.mybatis; public class Employee { private Integer i ...

  5. MyBatis Joint query and use association Step by step query

    Inquire about Emp At the same time , Find out emp The corresponding department Department Method 1: The joint query , Encapsulate the result set with cascading properties <!-- The joint query , Encapsulate the result set with cascading properties type: To customize the rules javaBean type i ...

  6. mybatis: Development environment construction -- Additions and deletions -- Multi table joint query ( For one more )

    What is? mybatisMyBatis Is to support the general SQL Inquire about , Excellent persistence layer framework for stored procedures and advanced mappings .MyBatis Eliminated almost all of them JDBC Manual setting of code and parameters and retrieval of result sets .MyBatis Easy to use XML or ...

  7. MyBatis A third : Multi table joint query

    In this article, we mainly explain how to mybatis It uses one-on-one . One to many . Multi table joint query ( Similar view ) Examples of operations . notes : Before reading this article, please take a look at the previous two articles . One . Table structure Class table class, Student list student, ...

  8. Mybatis.net And MVC Entry configuration and joint query dynamic SQL Splicing and simple transactions

    Study for the first time Mybatis.net, I also found a lot of information in the blog park , But after the successful configuration, we also encountered some problems , Especially in the dynamic SQl When you put it together , Here is a list of the problems I encountered and one I wrote Demo post , Hope to help the novice , There's something wrong with it ...

  9. Mybatis oracle The problem of duplicate paging data in multi table joint query

    Mybatis oracle The problem of duplicate paging data in multi table joint query There is a strange phenomenon when multi table joint query gets data by paging : The total number of data is correct , But there are more records , Some records are missing or even gone . I've been looking for this problem for a long time , It turned out that it was due to ...

Random recommendation

  1. git- introduction

    One . brief introduction Git It is the most advanced distributed version control system in the world ,Git Most of the operations in are to access local resources , There's no need for the Internet , There are three important concepts :1. working directory 2. staging area 3. Local repository . Just a quick explanation , working directory ...

  2. JS Medium bind Method learning

    EcmaScript5 to Function Extended a method :bind as everyone knows stay jQuery and prototype.js There's one in every frame bind jQuery The purpose of is to bind events to elements $("# ...

  3. HttpWebRequest and HttpWebResponse Summary of usage

    http://www.cnblogs.com/willpan/archive/2011/09/26/2176475.html http://www.cnblogs.com/lip0121/p/4539 ...

  4. c++ in volatile Detailed explanation

    1. Why volatile? C/C++ Medium volatile Key words and const Corresponding , Used to modify variables , Usually used to establish language level memory barrier. This is a BS stay "The ...

  5. java How to stop a running thread ?

    A little experience about thread // First import the required package improt java.util.Timer;import java.io.File;import java.util.TimerTask; // First you need to create ...

  6. Spring In the frame ModelAndView、Model、ModelMap difference

    Original address :http://www.cnblogs.com/google4y/p/3421017.html SPRING In the frame ModelAndView.Model.ModelMap difference   Be careful : If the method ...

  7. Struts What are the common tag libraries

    Struts  What are the common tag libraries 1.html Tag library 2.bean Tag library 3.logic Tag library

  8. Python object-oriented —— Basic inheritance

    1. Basic inheritance diagram 1.1 Instantiate a Contact Class object c 1.2 Instantiate a Supplier Class object s 1.3 Properties of the access object 1.4 Access object s Methods 1.5 Class variable details If we redefine it c.all_cont ...

  9. Python【 Third articles 】 File operations 、 Character encoding

    One . File operations File operation is divided into three steps : file open . Operation file . Close file , however , We can use with To manage file operations , So you don't have to close the file manually . Realization principle : import contextlib @context ...

  10. Baidu Maps API Real time drawing of dynamic trajectory ( A track ), The front of the car points in the direction of travel in real time , Set the angle offset

    Refer to the website :https://blog.csdn.net/skywqnan/article/details/79036262 Change the direction of the car :http://www.cnblogs.com/peixuanzh ...