注意:
#创建部门表
create table t_dept
(
dept_id int primary key auto_increment comment '部门编号',
dept_name varchar(20) comment '部门名字',
description varchar(200) comment '描述'
);
select * from t_dept;
insert into t_dept
(dept_name,description)
select '财务部','管钱的' union
select '后勤部','搞物业' union
select 'IT部门','编程序';
#创建员工表
create table t_emp
(
emp_id int primary key auto_increment comment '员工编号',
emp_name varchar(20) comment '员工姓名',
sex char(1) comment '性别',
phone varchar(11) comment '手机号',
-- hireDate date comment '入职日期',
-- leaveDate date comment '离职日期',
-- address varchar(200) comment '家庭住址',
-- username varchar(20) comment '员工账号',
-- password varchar(20) comment '员工密码',
dept_id int comment '所属部门'
);
select * from t_emp;
insert into t_emp
(emp_name,sex,phone,dept_id)
select '张三','女','1234567890',1 union
select '李四','男','1234567891',1 union
select '王五','女','1234567892',2 union
select '赵六','男','1234567893',3 ;
4.0.0
org.example
mybatis_0815_KTLX
1.0-SNAPSHOT
8
8
org.mybatis
mybatis
3.4.6
mysql
mysql-connector-java
8.0.11
org.projectlombok
lombok
1.18.22
junit
junit
4.13
compile
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/70813_db?useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf8&allowPublicKeyRetrieval=true
jdbc.username=root
jdbc.password=123456
参考官网:mybatis映射器mappers
https://mybatis.org/mybatis-3/zh/configuration.html#mappers
package com.yzh7.entity;
import lombok.Data;
import java.util.List;
@Data
public class Dept {
private Integer deptId;
private String deptName;
private String description;
//一个部门拥有多个员工
private List empList;
}
package com.yzh7.entity;
import lombok.Data;
@Data
public class Emp {
private Integer empId;
private String empName;
private String sex;
private String phone;
private Integer deptId;
//一个员工属于一个部门
private Dept dept;
}
package com.yzh7.mapper;
import com.yzh7.entity.Dept;
import java.util.List;
public interface DeptMapper {
//查询部门,一个部门关联多个员工
List listAll();
}
package com.yzh7.mapper;
import com.yzh7.entity.Emp;
import java.util.List;
public interface EmpMapper {
//查询所有员工,并且每个员工关联一个部门对象
List listAll();
}
package com.yzh7.test;
import com.yzh7.entity.Emp;
import com.yzh7.mapper.EmpMapper;
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.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class demo3 {
@Test
public void test1(){
EmpMapper empMapper=session.getMapper(EmpMapper.class);
List empList=empMapper.listAll();
for(Emp e:empList){
System.out.println(e);
}
}
private static SqlSessionFactory factory; //静态
private SqlSession session;
@BeforeClass
public static void befCla() throws IOException {
String resource = "mybatis_config.xml"; //mybatis_config.xml
InputStream inputStream = Resources.getResourceAsStream(resource);
factory = new SqlSessionFactoryBuilder().build(inputStream);
}
@Before
public void bf(){
session=factory.openSession();
}
@After
public void af(){
session.commit();
session.close();
}
}
// A code block
var foo = 'bar';
// A code block
var foo = 'bar';