栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合mybatis(超详细)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合mybatis(超详细)

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

一、我们所需要的基本环境

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.6.3
         
    
    com.duing
    springboot-mybatis-demo
    0.0.1-SNAPSHOT
    springboot-mybatis-demo
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.2
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            mysql
            mysql-connector-java
            5.1.47
        
        
            com.alibaba
            druid
            1.2.8
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

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



这是我的目录结构

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文件





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

二,开始写代码了 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 selectAll(){
       return service.selectAll();
    }
    @GetMapping("/{fid}")
    public Fun selectFid(@PathVariable("fid") Integer fid){
        return service.selectFid(fid);
    }

    //通过json的格式返回层级关系
    @GetMapping("/Hierarchy")
    public List 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 selectAll(){
     return mapper.selectAll();
    }

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


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

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

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

    private List Hierarchy(Integer fid,List fun){
        List 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 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 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




    
        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}
    


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

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

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

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

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