java若依框架开发api接口(添加新模块)

在java的spingboot框架中比较好的开源后台那必须是若依了!为了方便管理自己开发的api接口,添加自己的模块,将api接口统一管理在该模块中!

记得三连哦~

1、下载若依admin,链接:RuoYi 若依官方网站 |后台管理系统|权限管理系统|快速开发框架|企业管理系统|开源框架|微服务框架|前后端分离框架|开源后台系统|RuoYi|RuoYi-Vue|RuoYi-Cloud|RuoYi框架|RuoYi开源|RuoYi视频|若依视频|RuoYi开发文档|若依开发文档|Java开源框架|Java|SpringBoot|SrpingBoot2.0|SrpingCloud|Alibaba|MyBatis|Shiro|OAuth2.0|Thymeleaf|BootStrap|Vue|Element-UI||www.ruoyi.vip

2、添加模块

 

 

 

3、配置模块 

            <!-- 前端接口 -->
            <dependency>
                <groupId>com.ruoyi</groupId>
                <artifactId>v2</artifactId>
                <version>${ruoyi.version}</version>
            </dependency>
        <module>v2</module>

        <!-- 前端接口-->
        <dependency>
            <groupId>com.ruoyi</groupId>
            <artifactId>v2</artifactId>
        </dependency>

 

    #前端接口
    com.v2: debug

    # 搜索指定包别名
    typeAliasesPackage: com.ruoyi.**.domain,com.v2.domain
    # 配置mapper的扫描,找到所有的mapper.xml映射文件
    mapperLocations: classpath*:mapper/**/*Mapper.xml,classpath*:mapper/*Mapper.xml

    <dependencies>
        <!-- 通用工具-->
        <dependency>
            <groupId>com.ruoyi</groupId>
            <artifactId>ruoyi-common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
        </dependency>
    </dependencies>

 

package com.ruoyi.framework.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * 程序注解配置
 *
 * @author ruoyi
 */
@Configuration
// 表示通过aop框架暴露该代理对象,AopContext能够访问
@EnableAspectJAutoProxy(exposeProxy = true)
// 指定要扫描的Mapper类的包的路径
@MapperScan({"com.ruoyi.**.mapper","com.v2.mapper"})
public class ApplicationConfig
{

}

 ​​​​​​​

package com.ruoyi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;

/**
 * 启动程序
 * 
 * @author ruoyi
 */
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
//扫描我们的模块
@ComponentScan({ "com.ruoyi.*", "com.v2.*"})
public class RuoYiApplication
{
    public static void main(String[] args)
    {
        // System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(RuoYiApplication.class, args);
        System.out.println("(♥◠‿◠)ノ゙  若依启动成功   ლ(´ڡ`ლ)゙  \n" +
                " .-------.       ____     __        \n" +
                " |  _ _   \\      \\   \\   /  /    \n" +
                " | ( ' )  |       \\  _. /  '       \n" +
                " |(_ o _) /        _( )_ .'         \n" +
                " | (_,_).' __  ___(_ o _)'          \n" +
                " |  |\\ \\  |  ||   |(_,_)'         \n" +
                " |  | \\ `'   /|   `-'  /           \n" +
                " |  |  \\    /  \\      /           \n" +
                " ''-'   `'-'    `-..-'              ");
    }
}

 4、添加的模块代码

BannerController
package com.v2.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.v2.domain.Banner;
import com.v2.service.BannerService;

import java.util.List;


@RestController
@RequestMapping("/v2")
public class BannerController{
    @Autowired
    private BannerService bannerService;
    @RequestMapping("/list")
    public AjaxResult bannerList(){
        List<Banner> banners = bannerService.BannerList();
        return AjaxResult.success("查询成功", banners);
    }
}
Banner
package com.v2.domain;

import java.util.Objects;

public class Banner {
    private Integer id;
    private String img;
    private Integer sort;
    private Integer createtime;
    private Integer updatetime;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public Integer getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Integer createtime) {
        this.createtime = createtime;
    }

    public Integer getUpdatetime() {
        return updatetime;
    }

    public void setUpdatetime(Integer updatetime) {
        this.updatetime = updatetime;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Banner banner = (Banner) o;
        return Objects.equals(id, banner.id) && Objects.equals(img, banner.img) && Objects.equals(sort, banner.sort) && Objects.equals(createtime, banner.createtime) && Objects.equals(updatetime, banner.updatetime);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, img, sort, createtime, updatetime);
    }

    @Override
    public String toString() {
        return "Banner{" +
                "id=" + id +
                ", img='" + img + '\'' +
                ", sort=" + sort +
                ", createtime=" + createtime +
                ", updatetime=" + updatetime +
                '}';
    }
}
BannerMapper
package com.v2.mapper;

import org.apache.ibatis.annotations.Mapper;
import com.v2.domain.Banner;

import java.util.List;

@Mapper
public interface BannerMapper {
    public List<Banner> selectBanner();
}
BannerService
package com.v2.service;


import com.v2.domain.Banner;

import java.util.List;

public interface BannerService {
    public List<Banner> BannerList();
}
BannerServiceImpl
package com.v2.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.v2.domain.Banner;
import com.v2.mapper.BannerMapper;
import com.v2.service.BannerService;

import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerMapper bannerMapper;

    public List<Banner> BannerList(){
        List<Banner> banners = bannerMapper.selectBanner();
        return banners;
    }
}

BannerMapper

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.v2.mapper.BannerMapper">
    <select id="selectBanner" resultType="com.v2.domain.Banner">
        select *
        from sys_banner;
    </select>

</mapper>

5、过拦截访问

        //前端接口
        filterChainDefinitionMap.put("/v2/**", "anon");

 6、启动框架测试api接口

 

 这样会不会更好的管理自己开发的接口呢~

 7、注意事项

多个模块时,千万千万千万别有同名的类哦!~bean会报错的!~

记得三连~

海蜘蛛:只要我足够的废物,就没有天敌!

猜你喜欢

转载自blog.csdn.net/weixin_47723549/article/details/124666705