栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

【MyBatis】Spring Boot整合Mybatis实现数据库的读取

【MyBatis】Spring Boot整合Mybatis实现数据库的读取

目录
  • 一、从JDBC到MyBatis
    • MyBatis介绍
    • MyBatis的优点
  • 二、MyBatis实例
    • (1)创建项目
    • (2)配置相关文件
    • (3)项目编写
      • 1.定义数据库
      • 2.创建实体类实现业务流程
  • 三、运行测试
  • 四、总结
  • 五、参考

一、从JDBC到MyBatis MyBatis介绍

  MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Ordinary Java Objects,普通的 Java对象)映射成数据库中的记录。

MyBatis的优点
  • 与JDBC相比,减少了50%以上的代码量,消除了JDBC大量冗余的代码,不需要手动开关连接。
  • 很好的与各种数据库兼容,开发人员不需要考虑数据库的差异性。
  • MyBatis相当灵活,不会对应用程序或者数据库的现有设计强加任何影响,SQL写在XML里,从程序代码中彻底分离,解除sql与程序代码的耦合,便于统一管理和优化,并可重用。
  • 提供XML标签,支持编写动态SQL语句。
  • 提供映射标签,支持对象与数据库的ORM字段关系映射。
  • 提供对象关系映射标签,支持对象关系组建维护。
二、MyBatis实例 (1)创建项目

左侧选择Spring Initializr,选择JDK1.8以上,点击Next

此处可直接使用默认信息,本文将Java Version改为了8,Artifact改为了mybatis_test:

选择项目所需要的依赖,这个页面是选择你工程中需要用到的依赖,因为我们的目标是web项目使用MyBatis所以在web模块中勾选Spring Web。

在SQL中依次勾选Jdbc API、Mybatis framework、Mysql Driver。
然后Next直到finish即可。

项目创建成功。

(2)配置相关文件

查看目录下pom.xml文件
pom.xml:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.5.5
         
    
    com.example
    mybatis_test
    0.0.1-SNAPSHOT
    mybatis_test
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        

        
            mysql
            mysql-connector-java
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


  Spring Boot通过pom.xml中引入模块化的Stater,使得常规的开发场景可以很快把应用搭建起来。在使用Spring Boot的过程中,除了可以在pom.xml中配置一些内容外,一些项目相关的配置也可以在application.properties中通过配置来完成。

修改application.properties文件,application.properties是项目自带的配置文件,也可以建立其他的配置文件,可以在文件中添加端口、数据源、mybatis等相关数据。
本文添加以下内容
application.properties:

server.port=8080
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/runoob?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
#3306端口号后为数据库名
spring.datasource.username=root  //账户
spring.datasource.password=root  //密码
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
(3)项目编写 1.定义数据库

2.创建实体类实现业务流程

1.创建entity实体层:用于存放实体类,与数据库中属性值基本保持一致,实现set和get的方法。

创建Website类:

package com.example.mybatis_test.entity;
public class Website {
    private int id;
    private String name;
    private String url;
    private int alexa;
    private String country;

    public int getId(){
        return id;
    }
    public void setId(){
        this.id = id;
    }
    public String  getName(){
        return name;
    }
    public void setName(){
        this.name = name;
    }
    public String getUrl(){
        return url;
    }
    public void setUrl(){
        this.url = url;
    }
    public int getAlexa(){
        return alexa;
    }
    public void setAlexa(){
        this.alexa = alexa;
    }
    public String getCountry(){
        return country;
    }
    public void setCountry(){
        this.country = country;
    }
    @Override
    public String toString() {
        return "WebSite{" +
                "name=" + name +
                ", url='" + url + ''' +
                ", Alexa='" + alexa + ''' +
                ",country=" + country + ''' +
                '}';
    }
}

2.创建mapper映射层:用于对数据库进行数据持久化操作,他的方法语句是直接针对数据库操作的,主要实现一些增删改查操作,在mybatis中方法主要与*Mapper.xml内相互一一映射。

WebsiteMapper接口:

package com.example.mybatis_test.mapper;
import com.example.mybatis_test.entity.Website;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface WebsiteMapper {
    public List findAllWebsite();
    public List findWebsiteById(int id);
}

3.创建Mapper映射对应的WebsiteMapper.xml文件

注意该文件放在resources目录下的mapper包中,具体包名位置namespace要和上边的映射类对应。
WebsiteMapper.xml:




    
        
        
        
        
        
    

    
        select  * from websites;
    

    

4.创建service业务层:用于给controller层的类提供接口进行调用。

WebsiteService:

package com.example.mybatis_test.service;
import com.example.mybatis_test.entity.Website;
import com.example.mybatis_test.mapper.WebsiteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class WebsiteService {
    @Autowired(required=false)
    public WebsiteMapper websiteMapper;

    public List findAllWebsite(){
        return websiteMapper.findAllWebsite();
    }
    public List findWebsiteById(int id){
        return websiteMapper.findWebsiteById(id);
    }
}

5.创建controller控制层:用于负责具体模块的业务流程控制,需要调用service逻辑设计层的接口来控制业务流程。

WebsiteController:

package com.example.mybatis_test.service;
import com.example.mybatis_test.entity.Website;
import com.example.mybatis_test.mapper.WebsiteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WebsiteService {
    @Autowired(required=false)
    public WebsiteMapper websiteMapper;

    public List findAllWebsite(){
        return websiteMapper.findAllWebsite();
    }

    public List findWebsiteById(int id){
        return websiteMapper.findWebsiteById(id);
    }
}
三、运行测试

通过点击IDEA右上角的启动按钮来启动项目

  • 在网址栏输入:http://localhost:8080/website/getAllWebsite,显示如下

  • 在网址栏输入:http://localhost:8080/website//getWebsiteById/1,显示如下


通过测试的数据显示,项目测试成功。

四、总结

   MyBatis 是一个优秀的基于java 的持久层框架,内部封装了JDBC,我们只需要关注sql语句本身,而不需要处理加载驱动、创建连接、创建 statement、关闭连接,资源等繁杂的过程。
  本文用spring boot整合MyBatis实现Website数据库的读取。

五、参考

IDEA2019开发Spring Boot整合Mybatis实现User的CRUD(增读更删)

【Mybatis】从JDBC到Mybatis的改进

IDEA2019创建spring boot项目整合mybatis超级详细适合刚入门的小白

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/336330.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号