SpringBoot中MyBatis模块的使用

  |   0 评论   |   1,218 浏览

背景

本文来调研一下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]

参考

评论

发表评论

validate