今年跳槽后工作比较悠闲,而且本人对android开发确实觉得兴趣乏乏,刚好SpringBoot目前很火,而且可以和我android的kotlin母语配合食用,所以,开坑一个SpringBoot学习系列。
仿着饿了么来,除开支付模块(因为我没有账号),其他特效,自定义View之类的,估摸着自己学习,少用或者禁用第三方,尽量杜绝 面向百度编程
提供api给与app调用,尽量规范化,模块化。
使用Bootstrap布局,写类似jsp的页面模块,虽然不优雅,不好看。
后台的话,打算分成3个模块:
其中 Buyer 模块为app提供接口 Seller 模块提供ModelAndView,也就是管理后台 Service 模块提供Service,dao,数据操作
如果还不会新建项目,可以查看 如何新建SpringBoot项目
删除项目目录下src目录,即删除后,项目目录为:
分别新建 Buyer , Seller , Service 三个module,新建方法为:
修改根项目的pom.xml文件,去除掉springBoot的依赖(这些依赖我们放在Service Module里面) 这里只是我个人的看法,也许不对 其实这里我对这个项目的认识是:
没错,就是android狗最熟悉的Eclipse(其实我没用过,不过这个比较好解释) platform 也就是我们一开始新建的那个空壳项目,其实相当于Eelipse的workPlace,也就是我们的工作目录 Service 其实就是我们提供数据的Library,我们每个功能模块都会依赖它,在里面写Dao,Service Buyer 卖家模块,其实就是一个提供入口的功能模块,依赖了Service模块,调用里面的Service Seller 买家模块,解释同Buyer
好的,我知道还是解释得很烂,那我们看图。
其中根目录的pom.xml为:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <modules> <module>Buyer</module> <module>Service</module> <module>Seller</module> </modules> <groupId>com.ly</groupId> <artifactId>platform</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <name>platform</name> <description>Demo project for Spring Boot</description> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
注意这里根模块引用了3个module:
<module>Buyer</module> <module>Service</module> <module>Seller</module>
其中Service的pom.xml文件比较繁杂,因为主要Springboot依赖的文件,通用第三方都会写在这里
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>platform</artifactId> <groupId>com.ly</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service</artifactId> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--lombok插件--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--jpa--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.15</version> </dependency> <!--微信插件--> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>2.9.0</version> </dependency> <!--spring-boot-configuration:spring boot 配置处理器; --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!--swagger2--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.2.2</version> </dependency> <!--freemarker--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.3.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
而Buyer以及Seller的pom.xml高度类似:
Buyer
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>platform</artifactId> <groupId>com.ly</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Buyer</artifactId> <dependencies> <dependency> <groupId>com.ly</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <type>pom</type> <version>1.5.2.RELEASE</version> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <start-class>com.ly.platform.PlatformApplication</start-class> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.3.RELEASE</version> <configuration> <mainClass>${start-class}</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
Seller
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>platform</artifactId> <groupId>com.ly</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>Seller</artifactId> <dependencies> <dependency> <groupId>com.ly</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <type>pom</type> <version>1.5.2.RELEASE</version> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <start-class>com.ly.platform.PlatformApplication</start-class> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.5.3.RELEASE</version> <configuration> <mainClass>${start-class}</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
值得注意的是这边Buyer以及Service模块都引入了Service
<dependencies> <dependency> <groupId>com.ly</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
值得注意的是配置文件,我们一共有3个配置文件 (properties/yml) 分别位于:Buyer,Seller,Service
其中所负责的功能不同:
Service模块的配置文件
负责通用配置,例如配置常量,配置数据库,Redis等等
Buyer/Seller模块的配置文件负责配置各自的端口,日志文件目录等等不需要通用的配置
按去配置数据库连接,写dao,service 不表,我们写好了如下文件:
其中service提供的方法为:
/** * Banner */ public interface BannerService { List<Banner> findAll(); }
@RestController @RequestMapping("api/home") @Api(value = "首页模块", description = "管理Banner") public class BannerController { @Autowired private BannerService bannerService; @GetMapping("banner") @ApiOperation("查询所有的Banner") public List<Banner> getAllBanner() { return bannerService.findAll(); } }
server.port=8889
@RestController @RequestMapping("web/home") @Api(value = "首页模块", description = "管理Banner") public class BannerController { @Autowired private BannerService bannerService; @GetMapping("banner") @ApiOperation("查询所有的Banner") public List<Banner> getAllBanner() { return bannerService.findAll(); } }
Tips计划Seller是前后端不分离的,所以这里应该返回的ModelAndView,不过先写着测试吧