接父子工程项目搭建,继续扩展项目
重新调整模块结构
1.删除子模块fast-demo-web
中所有无用内容
2.右键fast-demo-web
名称->新建(news)->模块(Module),创建新的子模块
3.选择新建Maven工程
4.新建模块fast-demo-controller
,注意:父项选择fast-demo-web
而不是fast-demo
5.重复操作创建fast-demo-service
、fast-demo-dao
模块
fast-demo-controller
:专注写控制层代码,负责提供接口给前端对接fast-demo-service
:专注写业务逻辑代码fast-demo-dao
:专注处理数据,负责与数据库交互
简单实现数据库操作
1.每个子模块都新建对应的目录,如控制层增加controller
目录存放控制层文件
2.添加MySQL和MyBatis-plus依赖
这里我们在fast-demo-web
模块中的pom.xml
里添加依赖而不是父工程
<!--引入mybatisPlus 包含了 jdbc-->
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId>
</dependency><!--mysql-->
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId>
</dependency>
子工程添加依赖不写版本,版本写在父工程的pom.xml
,统一管理
<dependencyManagement><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.2</version></dependency>
</dependencyManagement>
重新加载Maven后,三个子工程都会有MySQL和MyBatis-plus依赖包,但父工程没有
3.配置application.yml
在fast-demo-controller
模块中的resources
目录下新建application.yml
,并增加数据库连接和MyBatis配置
# 数据库连接配置,记得新建一个数据库
spring:datasource:url: jdbc:mysql://localhost:3306/my_demo?useUnicode=true&characterEncoding=UTF-8driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: 123456mybatis-plus:# 指定 Mapper XML 文件的位置,使用 classpath 通配符指定路径mapper-locations: classpath:/mapper/**/*.xml# 指定实体类的包路径,用于自动扫描并注册类型别名type-aliases-package: com.cyfy.springboot_one.pojoglobal-config:db-config:id-type: input# 驼峰下划线转换(将数据库字段的下划线命名规则转换为 Java 实体类属性的驼峰命名规则)db-column-underline: true# 刷新 mapperrefresh-mapper: trueconfiguration:# 将 Java 实体类属性的驼峰命名规则转换为数据库字段的下划线命名规则map-underscore-to-camel-case: true# 查询结果中包含空值的列,在映射的时候,不会映射这个字段call-setters-on-nulls: true# 开启 sql 日志log-impl: org.apache.ibatis.logging.stdout.StdOutImpl# 关闭 sql 日志# log-impl: org.apache.ibatis.logging.nologging.NoLoggingImpl
4.引用其他模块类
当我们需要引用其他模块的类时,需要在pom.xml
中引入其他模块依赖,如service需要调用dao模块的方法,则需要在pom.xml
中添加下如下代码
<dependencies><dependency><groupId>com.cyfy</groupId><artifactId>fast-demo-dao</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies>
添加后,我们就可以在service
模块中导入dao
模块的类
同理,controller
模块需要导入service
模块的类,所以controller
模块中的pom.xml
需要添加如下代码
<dependencies><dependency><groupId>com.cyfy</groupId><artifactId>fast-demo-service</artifactId><version>0.0.1-SNAPSHOT</version><scope>compile</scope></dependency>
</dependencies>
注:虽然controller
模块只引用了service
模块,但因为service
模块引用了dao
模块,所以controller
模块也能直接使用dao
模块的类
项目结构
fast-demo-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>fast-demo</artifactId><groupId>com.cyfy</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>fast-demo-web</artifactId><packaging>pom</packaging><modules><module>fast-demo-controller</module><module>fast-demo-service</module><module>fast-demo-dao</module></modules><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><!--引入mybatisPlus 包含了 jdbc--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!--mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency></dependencies></project>
fast-demo-controller
模块
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>fast-demo-web</artifactId><groupId>com.cyfy</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>fast-demo-controller</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>com.cyfy</groupId><artifactId>fast-demo-service</artifactId><version>0.0.1-SNAPSHOT</version><scope>compile</scope></dependency></dependencies>
</project>
UserController
@RestController
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/user")public List<User> user(){List<User> users = userService.getUserList();return users;}
}
FastDemoApplication
@SpringBootApplication
public class FastDemoApplication {public static void main(String[] args) {SpringApplication.run(FastDemoApplication.class,args);}
}
fast-demo-service
模块
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>fast-demo-web</artifactId><groupId>com.cyfy</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>fast-demo-service</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>com.cyfy</groupId><artifactId>fast-demo-dao</artifactId><version>0.0.1-SNAPSHOT</version><scope>compile</scope></dependency></dependencies></project>
UserService
public interface UserService {List<User> getUserList();
}
UserServiceImpl
@Service
public class UserServiceImpl implements UserService {@Autowiredprivate UserMapper userMapper;@Overridepublic List<User> getUserList() {return userMapper.selectList(null);}
}
fast-demo-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>fast-demo-web</artifactId><groupId>com.cyfy</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>fast-demo-dao</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>
UserMapper
@Mapper
public interface UserMapper extends BaseMapper<User> {}
父工程fast-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><groupId>com.cyfy</groupId><artifactId>fast-demo</artifactId><packaging>pom</packaging><version>0.0.1-SNAPSHOT</version><modules><module>fast-demo-web</module></modules><name>fast-demo</name><description>fast-demo</description><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.13</spring-boot.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><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></dependencies><dependencyManagement><dependencies><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><configuration><mainClass>com.cyfy.fastdemo.FastDemoApplication</mainClass><skip>true</skip></configuration><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>