整合mybatis和springBoot

本文介绍了如何在SpringBoot项目中整合MyBatis,包括在接口上使用@Mapper注解自动实现映射、通过@MapperScan注解扫描Mapper接口、以及处理一对一和一对多关联查询的XML配置。
摘要由CSDN通过智能技术生成

一.整合mybatisspringBoot

步骤:

创建模块

配置application.xml

不用在写mybatis-config.xml文件,将数据的配置信息写在application.xml文件里面。

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/manager
    username: root
    password: root

mybatis:
//允许使用别名
  type-aliases-package: com.lzh.mybatis_springboot.model
  configuration:
  //开启驼峰命名
    map-underscore-to-camel-case: true
  //	 映射文件路径
  mapper-locations: classpath:mapper/*.xml
		

实体类

@Data
public class Dept implements Serializable {
    private Integer deptNo;

    private String dName;

    private String loc;
    private List<Emp> empList;

    private static final long serialVersionUID = 1L;
}

映射接口

package com.lzh.mybatis_springboot.mapper;

import com.lzh.mybatis_springboot.model.Dept;
import com.lzh.mybatis_springboot.model.Emp;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface DeptMapper {

    int deleteByPrimaryKey(Long id);

    int insert(Dept record);

    int insertSelective(Dept record);

    Dept selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(Dept record);

    int updateByPrimaryKey(Dept record);

    List<Dept> selectAll2();
}

方法一:
在接口上加上@Mapper注解

(1).标识接口:通过接口上使用@Mapper注解,告诉Mybatis这个接口是一个Mapper 接口,它定义了与数据库表交互的方法。

(2).自动生成实现:Mybatis会自动生成接口方法的实现,这些实现包括了与数据库的交互逻辑,如SQL语句的生成,参数的传递,结果的映射等。这样,就不需要手动编写Mapper接口的实现。

(3).当使用SpringBoot时,@Mapper注解也会告诉Spring容器将Mapper 接口注册为Springbean,使其可以通过依赖注入(@Autowired)来在应用程序中使用。

方法二:
在启动类上添加@MapperScan注解
@SpringBootApplication
@MapperScan("com.kfm.mybatis_springboot.mapper")
public class MybatisSpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisSpringBootApplication.class, args);
}
}

@MapperScan:

(1).扫描指定包:通过@MapperScan注解中指定要扫描的包名,Spring会自动查找该包下的所有Mapper接口.

(2).注册Mapper接口:当找到Mapper接口时,Spring会将注册为Springbean,以便可以在应用程序的其他组件中通过依赖注入(@Autowires)来使用。

(3).避免手动配置:使用 @MapperScan 注解可以避免手动在Spring的配置类中一个一个地注册Mapper接口。它能够减少配置的繁琐性,提高开发效率。

service

在service层加上注解@Component@Autowired

@Component:Springboot启动就可以创建该注解标注的类的对象

@Autowired:将自动创建的对象赋值给标注的属性值

import com.lzh.mybatis_springboot.mapper.DeptMapper;
import com.lzh.mybatis_springboot.model.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class DeptService {
    @Autowired
    DeptMapper deptMapper;
    public List<Dept> findAll2(){
        return deptMapper.selectAll2();
    }
}

测试类

加上注解@SpringBootTest@Autowired

二.关联查询

1.一对一查询

设置.xml文件

 <resultMap id="BaseResultMap" type="com.lzh.mybatis_springboot.model.Emp">
        <result property="empNo" column="emp_no" jdbcType="INTEGER"/>
        <result property="eName" column="e_name" jdbcType="VARCHAR"/>
        <result property="job" column="job" jdbcType="VARCHAR"/>
        <result property="mgr" column="mgr" jdbcType="INTEGER"/>
        <result property="hirdate" column="hirdate" jdbcType="TIMESTAMP"/>
        <result property="sal" column="sal" jdbcType="DOUBLE"/>
        <result property="comm" column="comm" jdbcType="DOUBLE"/>
        <result property="deptNo" column="dept_no" jdbcType="INTEGER"/>
        <association property="dept" javaType="dept">
            <result property="deptNo" column="dept_no" jdbcType="INTEGER"/>
            <result property="dName" column="d_name" jdbcType="VARCHAR"/>
            <result property="loc" column="loc" jdbcType="VARCHAR"/>
        </association>
    </resultMap>

例:一个员工对应一个部门(emp表除了员工信息外,还有部门的相关信息(dept表中对应的列))

association标签表示关联查询

association标签的property属性表示resultMap标签中type指定的类的属性名称。

association标签的javaType属性表示属性中存储的类型

要在emp表中创建dept表实体类的对象

2.一对多查询

设置.xml文件

 <resultMap id="BaseResultMap" type="com.lzh.mybatis_springboot.model.Dept">
            <result property="deptNo" column="dept_no" jdbcType="INTEGER"/>
            <result property="dName" column="d_name" jdbcType="VARCHAR"/>
            <result property="loc" column="loc" jdbcType="VARCHAR"/>
        <collection property="empList" ofType="emp">
            <result property="empNo" column="emp_no" jdbcType="INTEGER"/>
            <result property="eName" column="e_name" jdbcType="VARCHAR"/>
            <result property="job" column="job" jdbcType="VARCHAR"/>
            <result property="mgr" column="mgr" jdbcType="INTEGER"/>
            <result property="hirdate" column="hirdate" jdbcType="TIMESTAMP"/>
            <result property="sal" column="sal" jdbcType="DOUBLE"/>
            <result property="comm" column="comm" jdbcType="DOUBLE"/>
            <result property="deptNo" column="dept_no" jdbcType="INTEGER"/>
        </collection>
    </resultMap>

collectionn标签用于建立一对多中集合属性的对应关系,加载关联的集合对象

ofType属性用于指定集合元素的数据类型

property属性指定属性名

例:一个部门有多个员工

dept实体类中创建emp集合,把部门里面的员工封装成集合

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值