springboot项目之间相互引用,提取工具类和相同查询等公用代码

本文介绍了如何在SpringBoot项目中创建并引用公共模块,用于提取工具类、公共查询和JS代码。通过在主项目中配置Maven依赖、属性配置、包扫描以及调整打包顺序,确保主项目能正确调用和打包公共模块。同时,需要注意避免重复打包和修改公共模块文件时的压缩问题。

流程:主项目的maven依赖添加公共项目,配置文件中添加公共项目中需要的配置属性,包扫描添加公共项目包,主项目就可以直接调用公共项目中的代码和js了,打包时先打包公共项目再打包主项目,且公共项目不要打包为可执行jar包,否则主项目打包时会重复打包导致报错,打包后配置文件以主项目为准,修改jar中的文件时,如果是主项目文件则正常修改覆盖,如果是公共项目jar包中文件则需要用winrar的不压缩文件修改,否则会重复压缩导致启动报错,使用winrar修改可见:https://blog.csdn.net/qq_23337175/article/details/84300778

示例:准备两个项目 - 1.web-sql:公共项目-包含工具类,公共查询,工具js等;2.web-demo:主项目,调用web-sql

1.web-sql:公共项目

公共查询:

@Service
public class EmpService {

    @Resource
    EmpMapper mapper;

    public Emp getById(int id){
        return mapper.getEmpById1(id);
    }
}

工具类:

public class TestSqlUtil {
    public static String getSql(String str){
        return "sql为:"+str;
    }
}

工具js,路径为static/sql.js:

function getStr(str){
    return "getStr ------------------ "+str;
}

配置文件:

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.example.websql.bean
#logging.level.com.example.websql.dao=debug
#延时加载
mybatis.configuration.lazy-loading-enabled=true

打包配置,注释掉spring-boot-maven-plugin,防止主项目打包时重复打包

<!--            <plugin>-->
<!--                <groupId>org.springframework.boot</groupId>-->
<!--                <artifactId>spring-boot-maven-plugin</artifactId>-->
<!--            </plugin>-->

2.web-demo:主项目,调用web-sql

添加公共项目依赖

        <dependency>
            <groupId>com.example</groupId>
            <artifactId>web-sql</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

配置文件添加公共项目需要的属性,打包后配置文件以主项目为准

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*Mapper.xml
mybatis.type-aliases-package=com.example.websql.bean
#logging.level.com.example.websql.dao=debug
#延时加载
mybatis.configuration.lazy-loading-enabled=true

添加包扫描,如果不扫描公共项目启动后不能自动注入,而且需要把主项目路径也添加上,否则不会扫描自己

@SpringBootApplication(scanBasePackages = {"com.example.websql","com.example.webdemo"})
public class WebDemoApplication {

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

}

调用公共项目

package com.example.webdemo.controller;

import com.alibaba.fastjson.JSON;
import com.example.websql.bean.Emp;
import com.example.websql.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MavenController {

    @Autowired
    EmpService empService;

    @RequestMapping("/mtest/{sql}")
    public Object EUSTest(@PathVariable String sql) {
        System.out.println(sql);
        Emp e=empService.getById(1);
        System.out.println(JSON.toJSONString(e));
        return e;
    }

}

引用公共项目js

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>webdemo页面</title>
    <script src="sql.js"></script>
</head>
<body>
<h1 onclick=alert(getStr("demo"))>web-demo</h1>
</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值