Spring Boot数据库交互之Mybatis

前言

  • Spring Boot数据库交互之Spring Data JPA
    经过之前文章(见上面链接)的学习,我们学会了用Spring Data JPA方式与数据库交互,JPA有其优点,但也有其缺点,比如:
  1. 系统中的SQL在一定程度上被JPA的API接管了,久而久之在某种程度上会弱化开发人员编写SQL的能力;
  2. JPA在处理简单SQL时未发现明显问题,然而处理复杂SQL时就显得没那么容易了;
  3. JPA方式会多一层Entity层,需要一定的维护工作;
  4. ...

上述1、2 可以通过在Repository侧的nativeQuery=true来规避,但JPA的设计初衷并不是用来使用nativeQuery的,而是一种“对象/关联映射” 思想,nativeQuery更像是JPA方式的一个“彩蛋”!
经过多方了解,发现大家用得更多的还是Mybatis,接下来,我们就一起来学学Mybatis在Spring Boot框架下的使用!

建立项目

按照之前的文章介绍,建立项目:

  • 5分钟入手Spring Boot

项目结构如下:

Spring Boot数据库交互之Mybatis_第1张图片
项目结构

项目开发

1. 配置pom.xml;

#pom.xml整体样子:

  4.0.0
  com.mycompany.sample
  spring-boot-mybatis
  jar
  1.0-SNAPSHOT
  spring-boot-mybatis
  http://maven.apache.org

  
    org.springframework.boot
    spring-boot-starter-parent
    2.3.1.RELEASE
    
  

  
    
     #此处可以使用国内Maven镜像,如aliyun镜像、清华大学镜像等
    
  

  
    
      junit
      junit
      3.8.1
      test
    
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      2.1.3
    
    
      com.oracle
      ojdbc8
      12.2.0.1.0
    
    
      org.projectlombok
      lombok
      1.18.12
      provided
    
  

2. 建立Java类;

Spring Boot数据库交互之Mybatis_第2张图片
建立Java类

3. 处理项目入口类App.java;

package com.mycompany.sample;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author : dylanz
 * @since : 07/07/2020
 **/
@SpringBootApplication
@MapperScan(basePackages = "com.mycompany.sample.dao")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
//注意关键注解:
//@MapperScan(basePackages = "com.mycompany.sample.dao")
//该注解用于告诉mybatis去哪里扫描mapper,即扫描SQL;
//com.mycompany.sample.dao为DAO包路径;

4. 配置application.properties;

#configuration for oracle
spring.datasource.url=jdbc:oracle:thin:@//xxdb-scan:xxxx/XXXX
spring.datasource.username=xxxx
spring.datasource.password=xxxxxxxx
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
logging.level.com.mycompany.sample.dao=debug

最后一行是为了在运行窗口打印出执行的SQL,以便查看,如:

Spring Boot数据库交互之Mybatis_第3张图片
打印SQL

5. 编写domain;

package com.mycompany.sample.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.io.Serializable;

/**
 * @author : dylanz
 * @since : 07/07/2020
 **/
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Lead implements Serializable {
    private static final long serialVersionUID = 1L;

    public Long leadId;
    public String email;
}

6. 编写DAO;

package com.mycompany.sample.dao;

import com.mycompany.sample.domain.Lead;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

/**
 * @author : dylanz
 * @since : 07/07/2020
 **/
public interface LeadDAO {
    @Select("select lead_id,email from lead where lead_id = #{leadId}")
    @Results(value = {
            @Result(property = "leadId", column = "LEAD_ID"),
            @Result(property = "email", column = "EMAIL")
    })
    Lead getLeadByLeadId(@Param("leadId") Long leadId);
}

7. 编写Service;

package com.mycompany.sample.service;

import com.mycompany.sample.dao.LeadDAO;
import com.mycompany.sample.domain.Lead;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author : dylanz
 * @since : 07/07/2020
 **/
@Service
public class LeadService {
    @Autowired
    private LeadDAO leadDAO;

    public Lead getLeadByLeadId(Long leadId) {
        return leadDAO.getLeadByLeadId(leadId);
    }
}

8. 编写Controller;

package com.mycompany.sample.controller;

import com.mycompany.sample.domain.Lead;
import com.mycompany.sample.service.LeadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : dylanz
 * @since : 07/07/2020
 **/
@RestController
public class LeadController {
    @Autowired
    private LeadService leadService;

    @GetMapping("/getLead/{leadId}")
    @ResponseBody
    public Lead getLead(@PathVariable Long leadId) {
        return leadService.getLeadByLeadId(leadId);
    }
}

启动项目,见证奇迹

  1. 启动项目:
    Spring Boot数据库交互之Mybatis_第4张图片
    启动项目
  2. 调用API:
    http://127.0.0.1:8080/getLead/10XXXX46
    Spring Boot数据库交互之Mybatis_第5张图片
    调用API

Mybatis增删改查;

  • 增:DAO中使用@Insert注解;
  • 删:DAO中使用@Delete注解;
  • 改:DAO中使用@Update注解;
  • 查:DAO中使用@Select注解;

Mybatis另外一种管理SQL的方式;

  • 采用.xml文件的形式管理SQL,比如在mapper包内管理;

此时application.properties需要配置:
mybatis.mapper-locations=classpath:mapper/*.xml
(这种方式,我们本文不做研究)。

到此为止,我们已经完成了Spring Boot项目中采用Mybatis方式与数据库交互的实现!是不是很简单呀!

码字不容易,点赞需积极

谢谢!!!

你可能感兴趣的:(Spring Boot数据库交互之Mybatis)