SpringBoot项目创建图解
项目名称为:demo
点击next后根据所需自行填写相应信息,这里不做过多介绍

删除多余的文件

子模块创建图解



直接点击下方Finish,完成子模块创建
依次创建dao/biz/web子模块

demo父项目
demo父项目主要用于版本控制
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>demo-dao</module>
<module>demo-biz</module>
<module>demo-web</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<demo.version>0.0.1-SNAPSHOT</demo.version>
<springboot.version>2.2.2.RELEASE</springboot.version>
<mybatis.version>2.0.0</mybatis.version>
<mysql-connector.version>5.1.32</mysql-connector.version>
<lombok.version>1.16.18</lombok.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-dao</artifactId>
<version>${demo.version}</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-biz</artifactId>
<version>${demo.version}</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-web</artifactId>
<version>${demo.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${springboot.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
dao子模块

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">
<parent>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo-dao</artifactId>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
这里properties命名方式是为了方便多配置文件管理
application-dao.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/goods?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:mybatis/*.xml
mybatis.type-aliases-package=com.demo.dao.entity
可通过Mybatis逆向工程生成相应的实体,接口,配置文件这里不做介绍
详情请参考–>http://mybatis.org/generator/quickstart.html
biz子模块

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">
<parent>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo-biz</artifactId>
<dependencies>
<!-- biz模块依赖dao模块 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-dao</artifactId>
</dependency>
</dependencies>
</project>
DemoService 接口
package com.demo.biz.service;
public interface DemoService {
String test();
}
DemoService 接口实现类
package com.demo.biz.service.impl;
import com.demo.biz.service.DemoService;
import com.demo.dao.entity.Good;
import com.demo.dao.mapper.GoodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class DemoServiceImpl implements DemoService {
@Autowired
private GoodMapper goodMapper;
@Override
public String test() {
Good good = goodMapper.selectByPrimaryKey("1");
return good.toString();
}
}
web子模块

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">
<parent>
<artifactId>demo</artifactId>
<groupId>com.example</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>demo-web</artifactId>
<dependencies>
<!-- web依赖biz -->
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-biz</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
application.properties配置文件
使用spring.profiles.active区分不同环境下的配置文件(多文件配置)
spring.profiles.active=dao
server.port=80
启动类:DemoWebApplication.java
扫描com.demo路径
@SpringBootApplication(scanBasePackages = “com.demo”)
扫描mapper接口
@MapperScan(“com.demo.dao.mapper”)
package com.demo.web;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "com.demo")
@MapperScan("com.demo.dao.mapper")
public class DemoWebApplication {
public static void main(String[] args) {
SpringApplication.run(DemoWebApplication.class, args);
}
}
WEB访问
package com.demo.web.controll;
import com.demo.biz.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("demo")
public class DemoController {
@Autowired
private DemoService demoService;
@GetMapping("test")
public String test() {
return demoService.test();
}
}
运行


本文详细介绍了如何使用SpringBoot搭建父子工程,包括项目创建、子模块配置、依赖管理及运行流程。通过具体示例,展示了dao、biz和web子模块的搭建过程,以及如何进行多配置文件管理和接口调用。
21万+

被折叠的 条评论
为什么被折叠?



