前言
-
Spring Boot数据库交互之Spring Data JPA
经过之前文章(见上面链接)的学习,我们学会了用Spring Data JPA方式与数据库交互,JPA有其优点,但也有其缺点,比如:
- 系统中的SQL在一定程度上被JPA的API接管了,久而久之在某种程度上会弱化开发人员编写SQL的能力;
- JPA在处理简单SQL时未发现明显问题,然而处理复杂SQL时就显得没那么容易了;
- JPA方式会多一层Entity层,需要一定的维护工作;
- ...
上述1、2 可以通过在Repository侧的nativeQuery=true来规避,但JPA的设计初衷并不是用来使用nativeQuery的,而是一种“对象/关联映射” 思想,nativeQuery更像是JPA方式的一个“彩蛋”!
经过多方了解,发现大家用得更多的还是Mybatis,接下来,我们就一起来学学Mybatis在Spring Boot框架下的使用!
建立项目
按照之前的文章介绍,建立项目:
项目结构如下:
项目开发
1. 配置pom.xml;
#pom.xml整体样子:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.sample</groupId>
<artifactId>spring-boot-mybatis</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-boot-mybatis</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
<repositories>
<repository>
#此处可以使用国内Maven镜像,如aliyun镜像、清华大学镜像等
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc8</artifactId>
<version>12.2.0.1.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
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,以便查看,如:
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);
}
}
启动项目,见证奇迹
-
启动项目:
-
调用API:
http://127.0.0.1:8080/getLead/10XXXX46
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方式与数据库交互的实现!是不是很简单呀!
码字不容易,点赞需积极
谢谢!!!