天天看点

spring boot 多模块简单示例SpringBoot多模块项简单示例

最近花了两天看spring-boot.简单记录下springboot的多模块示例

项目有如下三个模块

dao

数据库操作

service

项目逻辑

web

web api,与前端交互

项目右键-> 新建module.

项目结构look like this

spring boot 多模块简单示例SpringBoot多模块项简单示例

spring-boot-mult-module项目下配置spring-boot的依赖。

可以有两种方式来引入spring-boot

直接添加parent,继承spring-boot的pom依赖

如果你项目中有其他的父类,就不能用method-1中的继承了,这样你可以这样来管理spring-boot的依赖

本例中用的是method-1,完整的pom.xml如下:

这里需要注意的,spring-boot已经添加了很多依赖的包,如果你想使用不同版本的库,你可以用来移除,然后添加自己版本的库

for example:

the version of jackson in spring-boot-1.5.4.release is

now, you want to use another version of jackson, you can add the follow dependency in you pom.xml

dao模块是用来操作数据库的,创建包

config

包下新建mybatisconfig.java

mapper

mybatis xml文件

entity

数据库实体类 user

创建接口

userdao

在main文件夹下创建resources文件夹,并添加文件

db.properties

数据库配置

mybatis-config.xml

mybatis配置

now, you dao module's structure may look like this:

spring boot 多模块简单示例SpringBoot多模块项简单示例

then, each file's content are follow:

finally, the structure of dao module may look like this:

spring boot 多模块简单示例SpringBoot多模块项简单示例

then run all the tests and check the database to see whether or not the insert test is successful.

if occur ==illegalstateexception: no supported datasource type found== error

make sure the dependency of commons-dbcp, or tomcat-jdbc or hikaricp in your pom.xml file

spring boot 多模块简单示例SpringBoot多模块项简单示例
spring boot 多模块简单示例SpringBoot多模块项简单示例

the default port of tomcat server is 8080,

if you want to use another port ,

you can create application.properties under resources in web module.

and add the follow property to change server port

spring boot 多模块简单示例SpringBoot多模块项简单示例

if you want to add some filter in you web application

define your filter

then configure your filter in your applicationconfig.java

run your application and you will see the filter is load.

spring boot 多模块简单示例SpringBoot多模块项简单示例

一个项目中用到了hive和hadoop,在用spring-boot搭个任务导入平台的时候,启动的时候报错了

这个是因为hadoop中的servlet-api的版本和spring-boot中的版本冲突了

hadoop中的是 2.5,而spring-boot中的是3.1,所以在pom.xml加入如下配置