Mybatis关联查询
实体
package
com.smbms.entity;
import
javax.persistence.*;
import
java.util.ArrayList;
import
java.util.List;
public
class
StudentinfoEntity {
private
int
stuid;
private
String studentname;
private
String sex;
private
String address;
private
String birthday;
//多的一方的教师
private
List<TeacherEntity> teachers=
new
ArrayList<>();
public
int
getStuid() {
return
stuid;
}
public
void
setStuid(
int
stuid) {
this
.stuid = stuid;
}
public
String getStudentname() {
return
studentname;
}
public
void
setStudentname(String studentname) {
this
.studentname = studentname;
}
public
String getSex() {
return
sex;
}
public
void
setSex(String sex) {
this
.sex = sex;
}
public
String getAddress() {
return
address;
}
public
void
setAddress(String address) {
this
.address = address;
}
public
String getBirthday() {
return
birthday;
}
public
void
setBirthday(String birthday) {
this
.birthday = birthday;
}
public
List<TeacherEntity> getTeachers() {
return
teachers;
}
public
void
setTeachers(List<TeacherEntity> teachers) {
this
.teachers = teachers;
}
}
package
com.smbms.entity;
import
javax.persistence.*;
import
java.util.ArrayList;
import
java.util.List;
public
class
TeacherEntity {
private
int
tid;
private
String tname;
//多的一方的学生实体
private
List<StudentinfoEntity> students=
new
ArrayList<>();
public
int
getTid() {
return
tid;
}
public
void
setTid(
int
tid) {
this
.tid = tid;
}
public
String getTname() {
return
tname;
}
public
void
setTname(String tname) {
this
.tname = tname;
}
public
List<StudentinfoEntity> getStudents() {
return
students;
}
public
void
setStudents(List<StudentinfoEntity> students) {
this
.students = students;
}
}
接口:
package
com.smbms.dao;
import
com.smbms.entity.TeacherEntity;
import
java.util.List;
//关联查询多对多
public
interface
TeacherMapper {
//查询所以教师(包括所教学员)
List<TeacherEntity> getAllTeacher();
}
配置:
<?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"
>
<!-- namespace属性代表该Mapper文件的唯一标识,通常习惯设置mapper接口名-->
<mapper namespace=
"com.smbms.dao.TeacherMapper"
>
<resultMap id=
"teacherMap"
type=
"TeacherEntity"
>
<id column=
"tid"
property=
"tid"
></id>
<result column=
"tname"
property=
"tname"
></result>
<!--为students集合设置关联属性-->
<collection property=
"students"
ofType=
"StudentinfoEntity"
>
<id column=
"stuid"
property=
"stuid"
></id>
<result column=
"studentname"
property=
"studentname"
></result>
<result column=
"sex"
property=
"sex"
></result>
<result column=
"address"
property=
"address"
></result>
<result column=
"birthday"
property=
"birthday"
></result>
</collection>
</resultMap>
<select id=
"getAllTeacher"
resultMap=
"teacherMap"
>
SELECT s.*,t.* FROM teacher t,studentinfo s,teastu ts
WHERE t.`tid`=ts.`tid` AND s.`stuid`=ts.`stuid`
</select>
</mapper>
测试:
package
com.smbms.test;
import
com.smbms.dao.TeacherMapper;
import
com.smbms.entity.TeacherEntity;
import
com.smbms.util.MybatisUtil;
import
org.apache.ibatis.session.SqlSession;
import
org.junit.After;
import
org.junit.Before;
import
org.junit.Test;
import
java.util.List;
public
class
Demo05 {
SqlSession session=
null
;
TeacherMapper mapper=
null
;
@Before
public
void
before(){
session= MybatisUtil.getSqlSession();
mapper=session.getMapper(TeacherMapper.
class
);
}
@Test
public
void
test01(){
List<TeacherEntity> teachers = mapper.getAllTeacher();
//重写Teacher类和Student类中的toString方法 直接打印
for
(TeacherEntity item:teachers){
System.out.println(item);
}
}
@After
public
void
after() {
session.commit();
//事务提交
session.close();
//释放se
}
}
自关联
|
package com.smbms.entity; import javax.persistence.*; import java.util.ArrayList; import java.util.List; public class ProductCategoryEntity { private int id; private String name; private int type; private List<ProductCategoryEntity> lists= new ArrayList<>(); public int getId() { return id; } public void setId( int id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public int getType() { return type; } public void setType( int type) { this .type = type; } public List<ProductCategoryEntity> getLists() { return lists; } public void setLists(List<ProductCategoryEntity> lists) { this .lists = lists; } } |
接口:
|
package com.smbms.dao; import com.smbms.entity.ProductCategoryEntity; //自关联 public interface CategoryMapper { //查询一级分类(包括其下的子分类) ProductCategoryEntity getOneById( int categoryid); } |
测试:
package
com.smbms.test;
import
com.smbms.dao.CategoryMapper;
import
com.smbms.entity.ProductCategoryEntity;
import
com.smbms.util.MybatisUtil;
import
org.apache.ibatis.session.SqlSession;
import
org.junit.After;
import
org.junit.Before;
import
org.junit.Test;
public
class
Demo06 {
SqlSession session=
null
;
CategoryMapper mapper=
null
;
@Before
public
void
before(){
session= MybatisUtil.getSqlSession();
mapper=session.getMapper(CategoryMapper.
class
);
}
@Test
public
void
test01(){
ProductCategoryEntity cate = mapper.getOneById(
548
);
//重写Category类中的toString方法 直接打印
System.out.println(cate);
}
@After
public
void
after(){
session.commit();
//事务提交
session.close();
//释放session
}
}