<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
dependencies>
emp_id int 主键自增
emp_name varchar(40)
dept_id int
dept部门表
dept_id int 主键自增
dept_name varchar(40)
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class Emp implements Serializable {
private Integer empId;
private String empName;
//关联关系 1: 一个员工对应一个部门
private Dept dept;
}
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class Dept implements Serializable {
private Integer deptId;
private String deptName;
//一个部门对应多个员工
private List<Emp> emps;
}
秘诀: 从一头出发,看向另一头. 所在位置不同,得到的结果不同.
表关系: 一个员工对应一个部门
Mybatis中映射规则: 结果集不允许出现重名字段.
Sql:
/*内连接的另外的一种表现形式.*/
SELECT e.emp_id,e.emp_name,
d.dept_id,d.dept_name
FROM emp e,dept d
WHERE e.dept_id = d.dept_id
package com.jt;
import com.jt.mapper.EmpMapper;
import com.jt.pojo.Emp;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
class SpringbootDemo3ApplicationTests {
/**
* SqlSessionFactory
*/
private SqlSessionFactory sqlSessionFactory;
@BeforeEach
public void init() throws IOException {
String resource = "mybatis/mybatis-config.xml";
InputStream inputStream =
Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);
}
/**
* 进行一对一查询 一个员工对应一个部门
*/
@Test
void oneToOne() {
SqlSession sqlSession = sqlSessionFactory.openSession();
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
List<Emp> list = empMapper.findAll();
System.out.println(list);
sqlSession.close();
}
}
public interface EmpMapper {
List<Emp> findAll();
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jt.mapper.EmpMapper">
<select id="findAll" resultMap="empRM">
select e.emp_id,e.emp_name,
d.dept_id,d.dept_name
from emp e,dept d
where e.dept_id = d.dept_id
select>
<resultMap id="empRM" type="Emp">
<id property="empId" column="emp_id">id>
<result property="empName" column="emp_name">result>
<association property="dept" javaType="Dept">
<id property="deptId" column="dept_id"/>
<result property="deptName" column="dept_name"/>
association>
resultMap>
mapper>
/*内连接的另外的一种表现形式.*/
SELECT d.dept_id,d.dept_name,e.emp_id,e.emp_name
FROM dept d,emp e
WHERE e.dept_id = d.dept_id
//一个部门下有多个员工 主表: dept
@Test
public void testOneToMore(){
SqlSession sqlSession = sqlSessionFactory.openSession();
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
List<Dept> list = deptMapper.findAll();
System.out.println(list);
sqlSession.close();
}
public interface DeptMapper {
List<Dept> findAll();
}
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jt.mapper.DeptMapper">
<select id="findAll" resultMap="deptRM">
select d.dept_id,d.dept_name,e.emp_id,e.emp_name
from dept d,emp e
where e.dept_id = d.dept_id
select>
<resultMap id="deptRM" type="Dept">
<id column="dept_id" property="deptId"/>
<result column="dept_name" property="deptName"/>
<collection property="emps" ofType="Emp">
<id column="emp_id" property="empId"/>
<result column="emp_name" property="empName"/>
collection>
resultMap>
mapper>
矛盾点:
案例说明: 需求 简化Sql, 那么映射文件复杂.
/**
* 利用Mybatis的子查询实现数据封装
*/
@Test
public void testOneToMore2(){
SqlSession sqlSession = sqlSessionFactory.openSession();
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
List<Dept> list = deptMapper.selectChildren();
System.out.println(list);
sqlSession.close();
}
<resultMap id="cRM" type="Dept">
<id column="dept_id" property="deptId"/>
<result column="dept_name" property="deptName"/>
<collection property="emps" ofType="Emp" select="findEmp" column="dept_id">collection>
resultMap>
<select id="findEmp" resultMap="empRM">
select * from emp where dept_id = #{dept_id}
select>
<resultMap id="empRM" type="Emp">
<id column="emp_id" property="empId"/>
<result column="emp_name" property="empName"/>
resultMap>