ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
**1. 在父工程下构建服务端模块:cloud-provider-dept-8001** ![](https://img.kancloud.cn/6a/12/6a1229901270e4eb564ae1cd6ada7038_1455x141.jpg) ![](https://img.kancloud.cn/2a/24/2a24be50d95876bfab1d27a1a4bd1412_1475x308.jpg) **2. 当前模块的`pom.xml`** ```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> <!-- 1. 子模块引入父工程进行统一版本控制 --> <artifactId>rest-cloud-parent</artifactId> <groupId>org.atguigu.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-dept-8001</artifactId> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <!-- 2. 当前子模块没有写版本时,则用父工程中指定的版本--> <dependencies> <!-- 3. 引入自定义的模块,便可以在当前模块中调用自定义模块的API了,就是引入了自己的JAR包 --> <dependency> <groupId>org.atguigu.springcloud</groupId> <artifactId>cloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-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> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>8</source> <target>8</target> </configuration> </plugin> </plugins> </build> </project> ``` **3. 你会在父工程的`pom.xml`中看到引入了当前的模块** ```xml <modules> <module>cloud-api</module> <module>cloud-provider-dept-8001</module> </modules> ``` **4. 当前模块用到的resources配置文件如下** (1)*`resources/application.yml`* ```yml server: port: 8001 # 服务访问端口为8001 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.atguigu.springcloud.api.entities # 实体类所在的包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件所在的路径 spring: application: name: ${project.artifactId} # 该名字非常重要,它是当前模块对外服务时暴露的名字 datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: com.mysql.jdbc.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/cloudDB01 # 数据库地址 username: root password: root dbcp2: min-idle: 5 # 数据库连接池的最小维持连接数 initial-size: 5 # 初始化连接数 max-total: 5 # 最大连接数 max-wait-millis: 200 # 等待连接获取的最大超时时间 ``` (2)*`resources/mybatis/mybatis.cfg.xml`* ```xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <!-- 其实mybatis整合到springboot后,mybatis.cfg.xml文件是可以不用的, mybatis的相关配置可以移到application.yml,或application.properties中配置。 --> <configuration> <settings> <setting name="cacheEnabled" value="true"/><!-- 二级缓存开启 --> </settings> </configuration> ``` **5. 在当前模块中创建Mybatis的映射文件** *`resources/mybatis/mapper/DeptMapper.xml`* ```xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.atguigu.springcloud.dept8001.dao.DeptDao"> <select id="findById" resultType="Dept" parameterType="Long"> select deptno,dname,db_source from dept where deptno=#{deptno}; </select> <select id="findAll" resultType="Dept"> select deptno,dname,db_source from dept; </select> <insert id="addDept" parameterType="Dept"> INSERT INTO dept(dname,db_source) VALUES(#{dname},DATABASE()); </insert> </mapper> ``` **6. 在当前模块中创建dao层、service层、controller** (1)dao层 ```java @Component public interface DeptDao { public boolean addDept(Dept dept); public Dept findById(Long id); public List<Dept> findAll(); } ``` (2)service层 ```java public interface DeptService { public boolean add(Dept dept); public Dept get(Long id); public List<Dept> list(); } ``` ```java @Service public class DeptServiceImpl implements DeptService { @Autowired private DeptDao dao; @Override public boolean add(Dept dept) { return dao.addDept(dept); } @Override public Dept get(Long id) { return dao.findById(id); } @Override public List<Dept> list() { return dao.findAll(); } } ``` (3)controller层 ```java @RestController @RequestMapping("/dept") public class DeptController { @Autowired private DeptService service; @RequestMapping(value = "/add", method = RequestMethod.POST) public boolean add(@RequestBody Dept dept) { return service.add(dept); } @RequestMapping(value = "/get/{id}", method = RequestMethod.GET) public Dept get(@PathVariable("id") Long id) { return service.get(id); } @RequestMapping(value = "/list", method = RequestMethod.GET) public List<Dept> list() { return service.list(); } } ``` **7. 当前模块的启动类** ```java @SpringBootApplication // 标注该类为启动类 @MapperScan("com.atguigu.springcloud.dept8001.dao") // 扫描dao层 public class DeptProvider8001App { public static void main(String[] args) { SpringApplication.run(DeptProvider8001App.class, args); } } ``` **8. 验证** 启动当前模块,访问 http://localhost:8001/dept/list 得出如下结果,则项目构建成功! ```json [{"deptno":1,"dname":"开发部","db_source":"clouddb01"},{"deptno":2,"dname":"人事部","db_source":"clouddb01"},{"deptno":3,"dname":"财务部","db_source":"clouddb01"},{"deptno":4,"dname":"市场部","db_source":"clouddb01"},{"deptno":5,"dname":"运维部","db_source":"clouddb01"}] ```