7、SpringBoot-mybatis-plus引入


系列导航

未完待续

SpringBoot连接数据库引入mybatis-plus

1、 数据准备(oracle数据库)

Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('1001', 'RabbitMQ');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('1002', 'ZeroMQ');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('1003', 'ActiveMQ');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('1004', 'RocketMQ');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('1005', 'Apollo');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('1', '后端开发');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('2', '前端开发');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('3', '前端框架');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('4', '后端框架');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('5', '数据库');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('6', 'NoSql');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('7', '对象存储');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('8', '大数据');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('9', '操作系统');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('10', '消息队列');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('100', 'Python');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('101', 'Java');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('102', 'PHP');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('103', 'C');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('104', 'C++');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('105', 'C#');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('106', 'PHP');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('107', 'go');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('108', 'Visual Basic');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('201', 'JavaScript');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('202', 'css');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('203', 'swift');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('204', 'html5');
Insert into XY_DIC_BLOCK_T   (BLOCK_ID, BLOCK_NAME) Values   ('300', 'Vue');
commit;

2、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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>
    <groupId>com.examplegroupId>
    <artifactId>demoartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>demoname>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <spring-boot.version>2.1.17.RELEASEspring-boot.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>


        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.2.0version>
        dependency>

        
        <dependency>
            <groupId>com.oraclegroupId>
            <artifactId>ojdbc6artifactId>
            <version>11.2.0.3version>
        dependency>


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintagegroupId>
                    <artifactId>junit-vintage-engineartifactId>
                exclusion>
            exclusions>
        dependency>
    dependencies>



project>

3、application.properties配置

为了简化配置这里就没有引入druid,需要的朋友可以根据前面的博客自行添加

# 应用名称
spring.application.name=demo
# 应用服务 WEB 访问端口
server.port=8080

# 数据库设置
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@192.168.1.100:1521:orcl
spring.datasource.username=zy
spring.datasource.password=1
 
#mybatis-plus控制台打印sql
mybatis-plus.configuration.log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

4、工程目录

  5、代码部分

启动类

package com.example.demo;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
@MapperScan("com.example.demo.mapper")   //这里要注意扫描mapper文件
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

实体类

package com.example.demo.domain;


import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

 
@TableName(value = "XY_DIC_BLOCK_T")
public class Block {
    private static final long serialVersionUID = 1L;

    @TableId
    private String blockId;
   
    private String blockName;

    public String getBlockId() {
        return blockId;
    }

    public void setBlockId(String blockId) {
        this.blockId = blockId;
    }

    public String getBlockName() {
        return blockName;
    }

    public void setBlockName(String blockName) {
        this.blockName = blockName;
    }

    @Override
    public String toString() {
        return "XyDicBlockT{" +
                "blockId='" + blockId + '\'' +
                ", blockName='" + blockName + '\'' +
                '}';
    }
}

mapper类

package com.example.demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.domain.Block;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface BlockMapper extends BaseMapper {

}

控制器类

package com.example.demo.controller;


import com.example.demo.domain.Block;
import com.example.demo.mapper.BlockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

 
import java.util.List;

@RestController
@RequestMapping("/hello")
public class HelloController {

    @Autowired
    private BlockMapper blockMapper;


    @GetMapping("/list")
    @ResponseBody
    public String index() {

        //直接调用BaseMapper封装好的CRUD方法,就可实现无条件查询数据
        List list = blockMapper.selectList(null);

        //循环获取用户数据
        for (Block block:list){
            //获取用户名称
            System.out.println(block.getBlockName());
        }


        return "sucess";
    }
}

  6、启动项目访问项目

很简单,使用mybatis-plus 大部分的操作都不用写xml文件了,省事好多

注:因为是get请求可以用浏览器来调用,如果是post的请求就只能用postman等工具来调用接口了。

 后端控制台输出