Spring Boot中使用MyBatis详解

236 阅读4分钟

今天给大家介绍如何在Spring Boot中使用MyBatis,希望大家喜欢。

  1. 创建项目 关于如何创建Spring Boot的项目,大家可以参考我的《Hello Spring Boot》里面有详细介绍。

  2. 导入依赖 在Spring Boot中使用MyBatis主要用到了三个依赖:MyBatis和Spring Boot 整合依赖、MySQL驱动依赖、Druid依赖。基于本文还需要另外两个依赖:jsp解析器、lombok。下面展示一下我的pom文件:

org.springframework.boot spring-boot-starter-web
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
    </dependency>
    <!--MyBatis和Spring Bot整合框架-->
    <!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.1.2</version>
    </dependency>
    <!--mysql驱动依赖-->
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- Druid依赖-->
    <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.22</version>
    </dependency>

    <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>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

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

    <resources>
        <!--注册webapp资源目录-->
        <resource>
            <directory>src/main/webapp</directory>
            <targetPath>META-INF/resources</targetPath>
            <includes>
                <include>**/*.*</include>
            </includes>
        </resource>
        <!--注册dao包下mybatis映射文件为资源目录-->
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

org.mybatis.spring.boot 这个依赖的版本号是不能省去,因为这个依赖是MyBatis整合的Spring Boot,而不是Spring Boot整合的它,所以不能省掉。

resources中的两个配置分别是注册webapp为资源目录、注册MyBatis的资源目录。

看到这么多依赖肯定有小伙伴问,我是从哪里知道的,大家可以访问这个网址查找。

  1. 创建Jsp 如何在Spring Boot中使用Jsp大家可以参考 《Spring Boot 使用jsp》 创建两个jsp文件分别是:index.jsp、success.jsp他们用来提交表单和展示结果。

<%-- Created by IntelliJ IDEA. User: zhangxianwei Date: 2020/4/12 Time: 12:55 下午 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>

Title 诗句:

作者:

<%-- Created by IntelliJ IDEA. User: zhangxianwei Date: 2020/4/12 Time: 1:26 下午 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> Title
提交成功
4.创建数据库 使用Navicat创建了一个poetry的表:

示意图

5.创建实体类 在项目的包名下创建一个目录名为:bean,再创建一个PoetryBean的类:

package com.zxw.mybatis.bean;

import lombok.Data;

@Data public class PoetryBean { private Integer id; private String verse; private String author; } 6.创建dao层 在项目的包名下创建一个目录名为:dao,再创建一个接口PoetryDao,不要忘记加上@Mapper注解。

package com.zxw.mybatis.dao;

import com.zxw.mybatis.bean.PoetryBean; import org.apache.ibatis.annotations.Mapper;

@Mapper public interface PoetryDao {

void insertVerse(PoetryBean poetryBean);

} 然后在resources目录下创建与其对应的配置文件:PoetryDao.xml

INSERT INTO poetry(verse,author ) VALUES (#{verse},#{author}) 首先这里面的id要和上面的方法一一对应,然后再写SQL语句就好了,这里推荐一个MybatisX的插件(类似于eventbus3-intellij-plugin这个插件),装上这个插件就可以直接定位到xml防止写错,而且这个插件很强大,感兴趣的去它的官网。

7.创建service 在项目的包名下创建一个目录名为:service,再创建接口PoetryService:

package com.zxw.mybatis.service;

import com.zxw.mybatis.bean.PoetryBean;

public interface PoetryService {

void addVerse(PoetryBean poetryBean);

} 创建实现类PoetryServiceImpl:

package com.zxw.mybatis.service;

import com.zxw.mybatis.bean.PoetryBean; import com.zxw.mybatis.dao.PoetryDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;

@Service public class PoetryServiceImpl implements PoetryService {

@Autowired
PoetryDao poetryDao;

@Override
public void addVerse(PoetryBean poetryBean) {
    poetryDao.insertVerse(poetryBean);
}

} 这个类中不要忘记加上@Service这个注解,我当时就是忘记加而报错了。

8.创建Controller 在项目的包名下载创建一个目录名为:controller,再创建类PoetryController:

package com.zxw.mybatis.controller;

import com.zxw.mybatis.bean.PoetryBean; import com.zxw.mybatis.service.PoetryService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping;

@Controller public class PoetryController {

@Autowired
private PoetryService poetryService;

@PostMapping("/register")
private String registerVerse(PoetryBean poetryBean) {
    poetryService.addVerse(poetryBean);
    return "success";
}

} 这个段代码就是当诗词提交成功后,返回success这个jsp用来通知提交成功。

9.配置 在application.properties文件中做一下配置:

视图的前辍与后辍

spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsp

注册映射文件

mybatis.mapper-locations=classpath:PoetryDao.xml

注册实体类别名

mybatis.type-aliases-package=com.zxw.mybatis.bean

注册数据源类型

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

连接数据库

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql:///db1 spring.datasource.username=root spring.datasource.password=zxw12345 10.检查 运行项目添加诗句检查结果如图:

示意图

总结 以上就是在Spring Boot中使用MyBatis详细步骤,总结如下:

在pom文件找那个添加:MyBatis与Spring Boot整合依赖、MySQL驱动依赖,和Druid依赖。 在配置文件中添加:映射文件、实体类别名,及数据源。 在Dao接口上添加@Mapper注解。

原文 www.jianshu.com/p/d526107a3…