在学习的过程中,大多时候项目与数据库都在本机上,使用hibernate或者mybatits加上简单的配置就能够打通程序到数据库路径,让程序能够访问到数据库。然而在一些情况下,我们不仅需要访问本机的数据库,还需要访问到另外一个数据库中的资源。本文书写的目的便在于解决访问多资源库的问题,更多的是为了交流与学习。学习,源码是很重要的。文末给出了我的Github中的源码链接,感兴趣的可以下载阅览。
一、项目创建与资源文件配置
建立一个初始的spring项目的方式在我的另一篇文章《springboot整合mybatis注解版与XML配置版》已经详细介绍了,在这里就不多赘述。当然,网上也有很多详细的教程,你也可以自行搜索阅览。新建好的项目的pom.xml文件中已经集成了必要的依赖包。本文所用到的pom.xml文件如下。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.40</version>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<!--部署热启动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</depend