SpringBoot 實戰 (九) | 整合 Mybatis

微信公衆號:一個優秀的廢人
如有問題或建議,請後臺留言,我會盡力解決你的問題。

前言

如題,今天介紹 SpringBoot 與 Mybatis 的整合以及 Mybatis 的使用,本文通過註解的形式實現。

什麼是 Mybatis

MyBatis 是支持定製化 SQL、存儲過程以及高級映射的優秀的持久層框架。MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數以及獲取結果集。MyBatis 可以對配置和原生 Map 使用簡單的 XML 或註解,將接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java 對象)映射成數據庫中的記錄。

優點:

  • 簡單易學:本身就很小且簡單。沒有任何第三方依賴,最簡單安裝只要兩個 jar 文件+配置幾個 sql 映射文件易於學習,易於使用,通過文檔和源代碼,可以比較完全的掌握它的設計思路和實現。
  • 靈活:mybatis 不會對應用程序或者數據庫的現有設計強加任何影響。 sql 寫在 xml 裏,便於統一管理和優化。通過 sql 基本上可以實現我們不使用數據訪問框架可以實現的所有功能,或許更多。
  • 解除 sql 與程序代碼的耦合:通過提供 DAL 層,將業務邏輯和數據訪問邏輯分離,使系統的設計更清晰,更易維護,更易單元測試。sql 和代碼的分離,提高了可維護性。
  • 提供映射標籤,支持對象與數據庫的 orm 字段關係映射
  • 提供對象關係映射標籤,支持對象關係組建維護
  • 提供xml標籤,支持編寫動態 sql。

缺點:

  • 編寫 SQL 語句時工作量很大,尤其是字段多、關聯表多時,更是如此。
  • SQL 語句依賴於數據庫,導致數據庫移植性差,不能更換數據庫。
  • 框架還是比較簡陋,功能尚有缺失,雖然簡化了數據綁定代碼,但是整個底層數據庫查詢實際還是要自己寫的,工作量也比較大,而且不太容易適應快速數據庫修改。
  • 二級緩存機制不佳

準備工作

  • IDEA
  • JDK1.8
  • SpringBoot 2.1.3

sql 語句,創建表,插入數據:

CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `age` double DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO `student` VALUES ('1', 'aaa', '21');
INSERT INTO `student` VALUES ('2', 'bbb', '22');
INSERT INTO `student` VALUES ('3', 'ccc', '23');

pom.xml 文件配置依賴

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.nasus</groupId>
    <artifactId>mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mybatis</name>
    <description>mybatis Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- 啓動 web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- mybatis 依賴 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!-- mysql 連接類 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- druid 數據庫連接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.9</version>
        </dependency>
        <!-- lombok 插件 用於簡化實體代碼 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!-- 單元測試 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
      
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yaml 配置文件

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

實體類

import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;

    private Integer age;

}

使用了 lombok 簡化了代碼。

dao 層

import com.nasus.mybatis.domain.Student;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface StudentMapper {

    @Insert("insert into student(name, age) values(#{name}, #{age})")
    int add(Student student);

    @Update("update student set name = #{name}, age = #{age} where id = #{id}")
    int update(@Param("name") String name, @Param("age") Integer age, @Param("id") Integer id);

    @Delete("delete from student where id = #{id}")
    int delete(int id);

    @Select("select id, name as name, age as age from student where id = #{id}")
    Student findStudentById(@Param("id") Integer id);

    @Select("select id, name as name, age as age from student")
    List<Student> findStudentList();
}

這裏有必要解釋一下,@Insert 、@Update、@Delete、@Select 這些註解中的每一個代表了執行的真實 SQL。 它們每一個都使用字符串數組 (或單獨的字符串)。如果傳遞的是字符串數組,它們由每個分隔它們的單獨空間串聯起來。這就當用 Java 代碼構建 SQL 時避免了“丟失空間”的問題。 然而,如果你喜歡,也歡迎你串聯單獨 的字符串。屬性:value,這是字符串 數組用來組成單獨的 SQL 語句。

@Param 如果你的映射方法的形參有多個,這個註解使用在映射方法的參數上就能爲它們取自定義名字。若不給出自定義名字,多參數(不包括 RowBounds 參數)則先以 "param" 作前綴,再加上它們的參數位置作爲參數別名。例如 #{param1},#{param2},這個是默認值。如果註解是 @Param("id"),那麼參數就會被命名爲 #{id}。

service 層

import com.nasus.mybatis.domain.Student;
import java.util.List;

public interface StudentService {

    int add(Student student);

    int update(String name, Integer age, Integer id);

    int delete(Integer id);

    Student findStudentById(Integer id);

    List<Student> findStudentList();

}

實現類:

import com.nasus.mybatis.dao.StudentMapper;
import com.nasus.mybatis.domain.Student;
import com.nasus.mybatis.service.StudentService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 添加 Student
     * @param name
     * @param age
     * @return
     */
    @Override
    public int add(Student student) {
        return studentMapper.add(student);
    }

    /**
     * 更新 Student
     * @param name
     * @param age
     * @param id
     * @return
     */
    @Override
    public int update(String name, Integer age, Integer id) {
        return studentMapper.update(name,age,id);
    }

    /**
     * 刪除 Student
     * @param id
     * @return
     */
    @Override
    public int delete(Integer id) {
        return studentMapper.delete(id);
    }

    /**
     * 根據 id 查詢 Student
     * @param id
     * @return
     */
    @Override
    public Student findStudentById(Integer id) {
        return studentMapper.findStudentById(id);
    }

    /**
     * 查詢所有的 Student
     * @return
     */
    @Override
    public List<Student> findStudentList() {
        return studentMapper.findStudentList();
    }
}

controller 層構建 restful API

import com.nasus.mybatis.domain.Student;
import com.nasus.mybatis.service.StudentService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/Student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping("")
    public int add(@RequestBody Student student){
        return studentService.add(student);
    }

    @PutMapping("/{id}")
    public int updateStudent(@PathVariable("id") Integer id, @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "age", required = true) Integer age){
        return studentService.update(name,age,id);
    }

    @DeleteMapping("/{id}")
    public void deleteStudent(@PathVariable("id") Integer id){
        studentService.delete(id);
    }

    @GetMapping("/{id}")
    public Student findStudentById(@PathVariable("id") Integer id){
        return studentService.findStudentById(id);
    }

    @GetMapping("/list")
    public List<Student> findStudentList(){
        return studentService.findStudentList();
    }
}

測試結果

查詢全部學生信息結果

其他接口已通過 postman 測試,無問題。

源碼下載:github 地址

後語

以上爲 SpringBoot 實戰 (九) | 整合 Mybatis 的教程,除了註解方式實現以外,Mybatis 還提供了 XML 方式實現。想了解更多用法請移步官方文檔

最後,對 Python 、Java 感興趣請長按二維碼關注一波,我會努力帶給你們價值,如果覺得本文對你哪怕有一丁點幫助,請幫忙點好看,讓更多人知道。

另外,關注之後在發送 1024 可領取免費學習資料。資料內容詳情請看這篇舊文:Python、C++、Java、Linux、Go、前端、算法資料分享

一個優秀的廢人

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章