SpringBoot 中 MyBatis 模块的使用

本贴最后更新于 2092 天前,其中的信息可能已经时移俗易

背景

本文来调研一下 SpringBoot 中是如何集成进来第三方模块的。这里以 MyBatis 模块为例。

这里选取 MyBatis 的原因为,之前写过一个独立版本的 MyBatis 应用,见 MyBatis 3 初体验

环境搭建

工程搭建

Idea 中,使用 Spring Initializr,模块选中 SQL 中的 MyBatis 和 MySQL 两项。

看 pom 文件中引入了 MyBatis 和 MySQL 两项

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

继续翻看 mybatis-spring-boot-starter 包中的 spring.provides,发现实际上引入了 mybatis-spring-boot-autoconfigure,mybatis,mybatis-spring 三项。

因为还没有配置数据库,所以直接运行的话,会报错:

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class


Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

编写配置文件

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
spring.datasource.username=note
spring.datasource.password=abeffect
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

准备 MySQL 数据库

MySQL 数据准备过程在 MyBatis 3 初体验中已写,这里方便阅读重新记录一下。

CREATE DATABASE test;

CREATE TABLE blog (
	id INT NOT NULL,
	author_id INT NOT NULL,
	title VARCHAR(255),
	PRIMARY KEY (id)
);


INSERT INTO blog (id,author_id,title) VALUES (1,101,'Jim Business');
INSERT INTO blog (id,author_id,title) VALUES (2,102,'Bally Slog');

创建映射对象

同样在 MyBatis 3 初体验中已写,这里方便阅读重新记录一下。

Blog

package com.example.demo005;

import java.io.Serializable;

public class Blog implements Serializable {

   private Integer id;

   private Integer authorId;

   private String title;

   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public Integer getAuthorId() {
      return authorId;
   }

   public void setAuthorId(Integer authorId) {
      this.authorId = authorId;
   }

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   @Override
  public String toString() {
      return "Blog [id=" + id + ", authorId=" + authorId + ", title=" + title + "]";
   }
}

BlogMapper

注意,这里加了一个 @Mapper 注解和一个 @Component 注解。

package com.example.demo005;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
@Component
public interface BlogMapper {
    @Select("SELECT * FROM blog WHERE id = #{id}")
    Blog selectBlog(Integer id);
}

创建测试单元

package com.example.demo005;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Demo005ApplicationTests {

    @Autowired
    private BlogMapper blogMapper;

    @Test
    public void contextLoads() {
    }

    @Test
    public void test() {
        Blog blog = blogMapper.selectBlog(1);
        System.out.println(blog);
    }
}

执行结果

2018-07-17 15:53:39.895  INFO 25459 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
Blog [id=1, authorId=null, title=Jim Business]

参考

  • Java

    Java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由 Sun Microsystems 公司于 1995 年 5 月推出的。Java 技术具有卓越的通用性、高效性、平台移植性和安全性。

    3167 引用 • 8207 回帖 • 1 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...