emp表:
dept表:
二者之间通过dept_id来联系。
先上代码结构:
先在pom里面导入依赖:
pom.xml
文件
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.7version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.2version>
dependency>
dependencies>
然后在application.yml
文件里面配置数据库,配置mybatis-plus是为了在控制台看到日志信息,所以不需要去配置mybatis的log4j.xml文件啥啥啥的了。
application.yml
:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_table?useSSl=true&useUnicode=true&characterEncoding=Utf-8&serverTimezone=Asia/Shanghai
username: root
password: 54jacksparrow
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
对应数据库表,新建两个实体类
Emp.java:
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@TableName("emp")
public class Emp extends Model<Emp> {
@TableId(value = "emp_id",type = IdType.AUTO)
private Integer empId;
@TableField("emp_name")
private String empName;
@TableField("age")
private Integer age;
@TableField("gender")
private String gender;
private Dept dept;
}
Dept.java:
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@TableName("dept")
public class Dept extends Model<Dept> {
@TableId(value = "dept_id",type = IdType.AUTO)
private Integer deptId;
@TableField("dept_name")
private String deptName;
private Collection<Emp> emp;
}
再建立mapper层:
EmpMapper.java:
@Mapper
public interface EmpMapper extends BaseMapper<Emp> {
List<Emp> getAllEmp();
}
DeptMapper.java:
@Mapper
public interface DeptMapper extends BaseMapper<Dept> {
List<Dept> getAllDept();
}
再建立对应的xml文件进行映射,这里有个小细节,如果你的两个xml文件是建立在mapper
包下,那就没啥事;如果是建立在自己命名的mappers
包下,那么就要去application.yml文件里面去配置路径,各位自行百度,免麻烦就把包命名为mapper
。不要加s。
dept与emp是一对多的关系,所以用到了
标签。
DeptMapper.xml
文件:
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatis_table.mapper.DeptMapper">
<resultMap id="baseDeptMapper" type="com.example.mybatis_table.entity.Dept">
<id property="deptId" column="dept_id"/>
<result property="deptName" column="dept_name"/>
<collection property="emp" ofType="com.example.mybatis_table.entity.Emp">
<id property="empId" column="emp_id"/>
<result property="empName" column="emp_name"/>
<result property="age" column="age"/>
<result property="gender" column="gender"/>
collection>
resultMap>
<select id="getAllDept" resultType="com.example.mybatis_table.entity.Dept">
select emp_id,emp_name,age,gender from dept join emp on dept.dept_id = emp.dept_id
select>
mapper>
empMapper.xml:
emp和dept是多对一的关系,注意观察两个xml文件,仔细区分,这里容易搞错。
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatis_table.mapper.EmpMapper">
<resultMap id="baseEmpMapper" type="com.example.mybatis_table.entity.Emp">
<id property="empId" column="emp_id"/>
<result property="age" column="age"/>
<result property="empName" column="emp_name"/>
<result property="dept.deptId" column="dept_id"/>
<result property="dept.deptName" column="dept_name"/>
resultMap>
<select id="getAllEmp" resultType="com.example.mybatis_table.entity.Emp">
select emp_id,emp_name,age,gender,dept_name from emp left join dept on emp.dept_id = dept.dept_id
select>
mapper>
这里很多细节容易出错,要注意,要是有看不懂的地方,建议先去好好复习一下mybatis和mybatis-plus的知识
最后写个测试类测试一下:
先对emp表进行测试:
MybatisTableApplicationTests.java
文件
@SpringBootTest
class MybatisTableApplicationTests {
@Resource
private EmpMapper empMapper;
@Resource
private DeptMapper deptMapper;
@Test
public void testDemo(){
empMapper.getAllEmp();
}
}
控制台输出结果:
这是通过emp表查看部分dept表的信息。结果与我不一样的去Mapper的xml文件里面去检查一下sql语句:
接下来检测dept表:
MybatisTableApplicationTests.java
文件
@SpringBootTest
class MybatisTableApplicationTests {
@Resource
private EmpMapper empMapper;
@Resource
private DeptMapper deptMapper;
@Test
public void testDemo(){
// empMapper.getAllEmp();
deptMapper.getAllDept();
}
}