锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

springboot整合mybatis(超详细)

时间:2023-11-21 16:37:02 yl1雨量传感器

文章目录

  • 一、我们需要的基本环境
    • 1.2、配置application.yml
    • 1.3、在mappers添加一个新的文件mapper.xml文件
  • 二、开始写代码
    • FunController
    • FunService
    • FunMapper
    • Fun
    • tfunMapper.xml

一、我们需要的基本环境

pom.xml

 <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>     <parent>         <groupId>org.springframework.bootgroupId>         <artifactId>spring-boot-starter-parentartifactId>         <version>2.6.3version>         <relativePath/>      parent>     <groupId>com.duinggroupId>     <artifactId>springboot-mybatis-demoartifactId>     <version>0.0.1-SNAPSHOTversion>     <name>springboot-mybatis-demoname>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.2.2version>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.47version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druidartifactId>
            <version>1.2.8version>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombokgroupId>
                            <artifactId>lombokartifactId>
                        exclude>
                    excludes>
                configuration>
            plugin>
        plugins>
    build>

project>

这是我的目录结构
在这里插入图片描述

1.2、配置application.yml

server:
  port: 8080
spring:
  application:
    name: mybadis-demo-8080
  datasource: #这是MySQL所使用的配置
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/his09?useSSL=true&useUnicode=true&characterEncoding=utf8
    username: root
    password: 123456

#这是mybatis所使用的配置
mybatis:
  type-aliases-package: com.duing.entity
  mapper-locations: classpath*:mappers/*.xml
  #使用mybatis有两个重要的配置:1:mapper-locations:告诉mybatisSQL的映射文件在这里
  #2:type-aliases-package:告诉mybatis对应的实体类位置

1.3、在mappers文件加下新增一个mapper.xml文件


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="这里写数据操作层dao/mapper">

mapper>

到这里我们的准备工作就做好了,下面我们就可以编写代码了

二,开始写代码了

FunController

package com.duing.controller;

import com.duing.entity.Fun;
import com.duing.service.FunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/fun")
public class FunController { 
        

    @Autowired
    private FunService service;

    @GetMapping("/")
   public List<Fun> selectAll(){ 
        
       return service.selectAll();
    }
    @GetMapping("/{fid}")
    public Fun selectFid(@PathVariable("fid") Integer fid){ 
        
        return service.selectFid(fid);
    }

    //通过json的格式返回层级关系
    @GetMapping("/Hierarchy")
    public List<Fun> selectHierarchy(){ 
        
        return service.selectHierarchy();
    }
}

FunService

package com.duing.service;

import com.duing.entity.Fun;
import com.duing.mapper.FunMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FunService { 
        

    @Autowired
    private FunMapper mapper;

   public List<Fun> selectAll(){ 
        
     return mapper.selectAll();
    }

    public Fun selectFid(Integer fid){ 
        
     return mapper.selectFid(fid);
    }


    public List<Fun> selectHierarchy(){ 
        
        List<Fun> funBos =new ArrayList<>();

        List<Fun> fun = mapper.selectAll();
        for (Fun fun1 : fun) { 
        
            if (fun1.getPid()==-1){ 
        
                List<Fun> hierarchy = Hierarchy(fun1.getFid(), fun);//子类的属性

                fun1.setFunList(hierarchy);
                funBos.add(fun1);
            }
        }
        return funBos;
    }

    private List<Fun> Hierarchy(Integer fid,List<Fun> fun){ 
        
        List<Fun> funBos =new ArrayList<>();
        for (Fun fun1 : fun) { 
        
            if (fun1.getPid()==fid){ 
        
             funBos.add(fun1);
            }
        }

        return funBos;
    }

}

FunMapper

package com.duing.mapper;

import com.duing.entity.Fun;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface FunMapper { 
        

    List<Fun> selectAll();//查询全部

    Fun selectFid(Integer fid);//通过id查询

}

Fun

package com.duing.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.List;

//权限类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Fun { 
        

    private Integer fid;
    private String fname;
    private Integer ftype;
    private String furl;
    private Integer pid;
    private String auth_flag;
    private Date  create_time;
    private Integer create_user;
    private Date update_time;
    private Integer update_user;
    private Integer delete_flag;

    private String yl1;
    private String yl2;

    private List<Fun> funList;

    public Fun(Integer fid, String fname, Integer ftype, String furl, Integer pid, String auth_flag, Date create_time, Integer create_user, Date update_time, Integer update_user, Integer delete_flag, String yl1, String yl2) { 
        
        this.fid = fid;
        this.fname = fname;
        this.ftype = ftype;
        this.furl = furl;
        this.pid = pid;
        this.auth_flag = auth_flag;
        this.create_time = create_time;
        this.create_user = create_user;
        this.update_time = update_time;
        this.update_user = update_user;
        this.delete_flag = delete_flag;
        this.yl1 = yl1;
        this.yl2 = yl2;
    }
}

tfunMapper.xml


DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.duing.mapper.FunMapper">

    <select id="selectAll" resultType="com.duing.entity.Fun">
        SELECT `fid`,`fname`,`ftype`,`furl`,`pid`,`auth_flag`,`create_time`,
               `create_user`,`update_time`,`update_user`,`delete_flag`,
               `yl1`,`yl2`FROM `t_fun`
    select>

    <select id="selectFid" resultType="com.duing.entity.Fun">
        SELECT `fid`,`fname`,`ftype`,`furl`,`pid`,`auth_flag`,`create_time`,
               `create_user`,`update_time`,`update_user`,`delete_flag`,`yl1`,`yl2`
        FROM `t_fun`
        WHERE `fid`=#{fid}
    select>

mapper>

写完之后的目录结构是这样的

这是我在gitee上的代码可以下载下来看一看
boot-mybatis

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章