赞
踩
中文网:https://www.springcloud.cc/
承接着我们的springmvc+ mybatis+ mysql初级高级课程,以Dept部门模块做一个微服务通用案例Consumer消费者(Client),通过REST调用Provider提供者(Server)提供的服务
MicroServiceCloud父工程(Project)下初次带着3个子模块(Module)
microservicecloud-api:封装的整体entity/接口/公共配置等
microservicecloud-provider-dept-8001:微服务落地的服务提供者
microservicecloud-consumer-dept-80:微服务调用的客户端使用
作用:主要是定义POM文件,将后续各个子模块公用的jar包等统一提出来,类似一个抽象父类
- pom文件
<?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"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>microservicecloud</artifactId> <version>1.0</version> <modules> <module>microservicecloud-api</module> <module>microservicecloud-provider-dept-8001</module> <module>microservicecloud-consumer-dept-80</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <junit.version>4.12</junit.version> <log4j.version>1.2.17</log4j.version> <lombok.version>1.16.18</lombok.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.9.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.4</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.31</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> <!--日志--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.2.3</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> </dependencies> </dependencyManagement> </project>
- 架构图
- 步骤
- pom文件
<?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>microservicecloud</artifactId> <groupId>org.example</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservicecloud-api</artifactId> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
- 实体类(Dept)
package com.mk.demo.entity; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.experimental.Accessors; import java.io.Serializable; @Data @AllArgsConstructor @NoArgsConstructor @Accessors(chain = true)//开启链式访问 //实体类-序列化接口 public class Dept implements Serializable { // Dept(Entity) orm mysql->Dept(table) 类表关系映射 private Long deptno;//主键 private String dname;//部门名称 private String db_source;//来自那个数据库,因为微服务架构可以一个服务对应一个数据库,同一个信息被存储到不懂数据库 public static void main(String[] args) { Dept dept = new Dept(); dept.setDeptno(11l).setDname("zhan").setDb_source("adkfl");//链式写法 } }
- 架构图
- pom文件
<?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>microservicecloud</artifactId> <groupId>org.example</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>microservicecloud-provider-dept-8001</artifactId> <!-- 引入自己定义的api通用包,可以使用Dept部门Entity--> <dependencies> <dependency> <artifactId>microservicecloud-api</artifactId> <groupId>org.example</groupId> <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-jetty</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>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <!--修改后立即生效,热部署--> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
- 配置文件(application.yml)
server: port: 8001 # 端口号 mybatis: config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径 type-aliases-package: com.mk.demo.entity # 所有Entity别名类所在的包 mapper-locations: - classpath:mybatis/mapper/**/*.xml # mapper映射文件 spring: application: name: microservicecloud-dept datasource: type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型 driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包 url: jdbc:mysql://localhost:3306/aa_mk_001 # 数据库名称 username: root password: 123456 dbcp2: min-idle: 5 # 数据库连接池的最小维持连接数 initial-size: 5 # 初始化连接数 max-total: 5 # 最大连接数 max-wait-millis: 200 # 等待连接获取的最大超时时间
- 编写接口(DeptDao)
package com.mk.demo.dao; import com.mk.demo.entity.Dept; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface DeptDao { public boolean addDept(Dept dept); public Dept findById(Long id); public List<Dept> findAll(); }
- 编写映射文件(DeptMapper)
<?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.mk.demo.dao.DeptDao"> <!--public Dept findById(Long id);--> <select id="findById" resultType="dept" parameterType="Long"> select deptno , dname , db_source from dept where deptno = #{id}; </select> <!--public List<Dept> findAll();--> <select id="findAll" resultType="dept"> select deptno , dname , db_source from dept </select> <!--public boolean addDept(Dept dept);--> <insert id="addDept" parameterType="dept"> insert into dept(dname,db_source) values (#{dname},DATABASE()); </insert> </mapper>
- 编写service接口(DeptService)
package com.mk.demo.service;
import com.mk.demo.entity.Dept;
import java.util.List;
public interface DeptService {
public boolean add(Dept dept);
public Dept get(Long id);
public Lis
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。