创建一个SpringBoot父项目,只保留pom.xml,并导入工程中用到的所有依赖
org.springframework.boot spring-boot-starter-web com.baomidou mybatis-plus-boot-starter 3.5.1 org.projectlombok lombok 1.18.20 mysql mysql-connector-java 8.0.32
创建一个admin子模块,用于添加配置文件、实现Controller层的作用
在配置文件中管理整个工程的配置项
server:port: 8070
spring:datasource:type: com.zaxxer.hikari.HikariDataSourcedriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.01:3306/t_mybatis?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=trueusername: rootpassword: Aa123123.
mybatis-plus:type-aliases-package: demo.entityconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:table-prefix: t_id-type: automapper-locations: classpath:mappers/*.xml
创建一个system子工程,用于实现实体层、DAO层、Service层的开发
package demo.entity;import lombok.Data;@Data
public class User {private String userName;private String passwd;
}
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 {
}
package demo.service;import demo.entity.User;import java.util.List;public interface UserService {List 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 {@Autowiredprivate UserMapper userMapper;@Overridepublic List findAll() {return userMapper.selectList(null);}
}
由于控制器子工程实现的是Controller层的功能,需要依赖系统子工程的Service层功能,所以需要在控制器子工程中通过dependency
标签添加系统子工程的依赖
gaofeng-admin/pom.xml
com.example gaofeng-system 0.0.1-SNAPSHOT
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 {@Autowiredprivate UserService userService;@GetMapping("/list")public List findAll(){return userService.findAll();}
}
4.0.0 pom gaofeng-admin gaofeng-system org.springframework.boot spring-boot-starter-parent 2.7.9 com.example boot-parent 0.0.1-SNAPSHOT boot-parent Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web com.baomidou mybatis-plus-boot-starter 3.5.1 org.projectlombok lombok 1.18.20 mysql mysql-connector-java 8.0.32 org.springframework.boot spring-boot-maven-plugin