天天看點

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加入如下配置