springboot引入其他项目jar包并实现对数据库的操作

概述

A作为被引用的项目,目录结构如下:

可以看到A目录中只有基础的service类以及mybatis操作数据库的相关文件,service类中包含查询数据库的方法。

Tips: 项目A也可以是一个包含启动类和配置文件的Springboot项目,一般情况下只会将公共的方法和类提取打包成公共jar供其他项目引用,而且这里启动类的配置对后续引用也会造成影响,不利于排查。所以不推荐使用,尽量删除在被引用的项目无用的配置文件和test文件,在主项目中进行必要的配置。

A项目作为公共项目打成jar供其他项目引用,注意被引入的项目不能使用默认的maven-plugin打包,否则引入此jar包的项目编译时会报找不到程序包的错误

需要替换成以下配置:

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>

	</build>

配置完成后使用maven工具进行打包

成功之后可以看到本地maven仓库中会出现上图左边标记出的jar,其他项目引用时,只需要在pom文件中使用上面的坐标引入依赖。

C项目引用A项目,目录结构如下:

项目C作为主程序,需要有启动类和配置文件,下面详细说明一下启动类的配置。

C项目本身只需要配置扫描当前项目的dao接口。因为@SpirngBootApplication默认包含了@ComponentScan注解并且处于其他类文件的根目录,所以可以自动扫描到同级下面的所有@Service、@Controller类。

如果要扫描到A项目的类和接口就要添加A的目录,写法如下:

@MapperScan同时扫描A的dao接口和C项目的dao接口

@ComponentScan同时注册A和C项目下需要被使用的类

这里如果MapperScan只扫描了C项目,那调用A的dao接口中的方法时,会初始化失败项目无法启动,

如果ComponentScan只扫描了A项目,那么访问C项目的Controller会报404错误。

Tips:如果被引入的jar和主项目两个项目的文件路径全都一致的话,就不需要额外再配置扫描被引入的项目的目录。

配置文件application.yml:

server:
  port: 8082
spring:
    datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://**.**.**.***:3306/database?usessl = false&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
        username: username
        password: password
        type: com.zaxxer.hikari.HikariDataSource
        hikari:
           minimum-idle: 5
           maximum-pool-size: 15
           auto-commit: true
           idle-timeout: 30000
           pool-name: DatebookHikariCP
           max-lifetime: 1800000
           connection-timeout: 30000
           connection-test-query: SELECT 1

mybatis:
  configuration:
    mapUnderscoreToCamelCase: true
    call-setters-on-nulls: true
#    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath*:mapper/*.xml

这里对服务的端口、数据库连接、mybatis进行了配置。

注意:如果引入的其它的jar并且要使用它对应的mapper/*.xml文件时,要修改mapper-locations配置,使用通配符扫描所有calsspath对应目录下的mapper文件

mapper-locations: classpath*:mapper/*.xml

测试:CccController.java

/**
 * @ClassName CController
 * @Description 测试调用其他jar中的方法
 * @Author Sue
 * @Create 2020/1/15 9:39
 **/
@RestController
@RequestMapping("/ccc")
public class CcController {

    @Autowired
    private CcService ccService;
    @Autowired
    private AbcService abcService;
    @Autowired
    private AbcMapper abcMapper;

    @GetMapping("/test01")
    public Map<String, Object> test01() {
        Map<String, Object> res = new HashMap<>();
        res.put("code", 200);
        //C项目service方法
        res.put("c_service", ccService.queryOne());
        //A项目的dao和service中的方法
        res.put("abc_mapper", abcMapper.queryOne());
        res.put("abc_service", abcService.queryOne());
        return res;
    }
}

我们在C项目的Controller中调用了C项目的查询数据的方法和A项目查询数据库的方法,访问接口成功返回数据,说明我们成功的在C项目中引用A项目并调用了其操作数据库的方法。

{"code":200,"c_service":9419,"abc_service":9419,"abc_mapper":9419}

EOF

原文地址:https://www.cnblogs.com/sueyyyy/p/12197922.html