SpringBoot第 16 讲:SpringBoot+Maven父子项目

一、创建父工程boot-parent

创建一个SpringBoot父项目,只保留pom.xml,并导入工程中用到的所有依赖

	<dependencies>
		
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.1version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.20version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.32version>
        dependency>
    dependencies>

二、创建子工程

2.1、创建控制器子工程gaofeng-admin

创建一个admin子模块,用于添加配置文件、实现Controller层的作用

2.1.1、application.yml

在配置文件中管理整个工程的配置项

server:
  port: 8070
spring:
  datasource:
    type: com.zaxxer.hikari.HikariDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.01:3306/t_mybatis?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
    username: root
    password: Aa123123.
mybatis-plus:
  type-aliases-package: demo.entity
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      table-prefix: t_
      id-type: auto
  mapper-locations: classpath:mappers/*.xml

2.2、创建系统子工程gaofeng-system

创建一个system子工程,用于实现实体层、DAO层、Service层的开发

2.2.1、实体层User

package demo.entity;

import lombok.Data;

@Data
public class User {
    private String userName;
    private String passwd;
}

2.2.2、DAO层UserMapper

package demo.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import demo.entity.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper extends BaseMapper<User> {
}

2.2.3、Service层UserService

package demo.service;

import demo.entity.User;

import java.util.List;

public interface UserService {
    List<User> findAll();
}
package demo.service.imp;

import demo.entity.User;
import demo.mapper.UserMapper;
import demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        return userMapper.selectList(null);
    }
}

2.3、在控制器子工程中添加系统子工程的依赖

由于控制器子工程实现的是Controller层的功能,需要依赖系统子工程的Service层功能,所以需要在控制器子工程中通过dependency标签添加系统子工程的依赖

2.3.1、添加依赖

gaofeng-admin/pom.xml

	<dependencies>
        <dependency>
            <groupId>com.examplegroupId>
            <artifactId>gaofeng-systemartifactId>
            <version>0.0.1-SNAPSHOTversion>
        dependency>
    dependencies>

2.3.2、完成Controller层的功能

package demo.controller;

import demo.entity.User;
import demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public List<User> findAll(){
        return userService.findAll();
    }
}

三、附录

3.1、项目源代码

3.2、父工程完整pom.xml


<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.0modelVersion>
    <packaging>pompackaging>
    <modules>
        <module>gaofeng-adminmodule>
        <module>gaofeng-systemmodule>
    modules>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.7.9version>
        <relativePath/> 
    parent>
    <groupId>com.examplegroupId>
    <artifactId>boot-parentartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>boot-parentname>
    <description>Demo project for Spring Bootdescription>
    <properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.1version>
        dependency>
        
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>1.18.20version>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.32version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

你可能感兴趣的:(SpringBoot+,spring,boot,maven,mybatis)