创建maven项目
父项目
导入父项目依赖(这里做的是依赖的管理,可以统一管理控制子项目依赖的版本)
<!--打包方式 pom--> <packaging>pom</packaging> <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-alibaba-dependencies</artifactId> <version>0.2.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--springCloud的依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Greenwich.SR1</version> <type>pom</type> <scope>import</scope> </dependency> <!--SpringBoot--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.1.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <!--数据库--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!--SpringBoot 启动器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</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> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies> </dependencyManagement>
子项目 api(实体类,提供者提供的接口)
在子项目中可以引用父项目的依赖(不需要写版本号)
<dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies>
在数据库db01下创建表dept插入数据
创建实体类(必须要实现序列化implements Serializable)
//创建get,set方法 @Data //链式写法 @Accessors(chain = true) public class Dept implements Serializable { private Long deptno; //主键 private String dname; //这个数据数存在哪个数据库的字段 // 微服务一个服务对应一个数据库,同一个信息可能存在不同的数据库 private String db_source; public Dept(String dname) { this.dname = dname; } public Dept() { } }
链式写法: 就是支持这样赋值。
子项目 提供者
创建使用8001端口的提供者项目
导入依赖(这里会用到api项目中的pojo,需要依赖之前创建的项目springcloud-api)
<dependencies> <!-- 需要在别的项目中的pojo,引入springcloud-api --> <dependency> <artifactId>springcloud-api</artifactId> <groupId>com.easyjava</groupId> <version>1.0-SNAPSHOT</version> </dependency> <!--数据库--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> </dependency> <!--SpringBoot 启动器--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!--日志测试~--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> </dependencies>
其中的pojo需要根据创的文件夹路径填写
server: port: 8001 #mybatis配置 mybatis: type-aliases-package: com.kuang.springcloud.pojo config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml #spring的配置 spring: application: name: springcloud-provider-dept datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/db01?useUnicode=true&characterEncoding=utf-8 username: root password: 1234
<?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"> <configuration> </configuration>
编写mybatis映射,dao层和service层(DeptMapper.xml和DeptDao相互映射,需要修改<mapper namespace="com.demo.dao.DeptDao">中的namespace)
@Mapper @Repository public interface DeptDao { public boolean addDept(Dept dept); public Dept queryById(Long id); public List<Dept> queryAll(); }
<?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.demo.dao.DeptDao"> <insert id="addDept" parameterType="Dept"> insert into dept(dname, db_source) values (#{dname},DATABASE()); </insert> <select id="queryById" resultType="Dept" parameterType="Long"> select * from dept where deptno = #{deptno}; </select> <select id="queryAll" resultType="Dept"> select * from dept; </select> </mapper>
public interface DeptService { public boolean addDept(Dept dept); public Dept queryById(Long id); public List<Dept> queryAll(); }
public class DeptServiceImpl implements DeptService { @Autowired private DeptDao deptDao; @Override public boolean addDept(Dept dept) { return deptDao.addDept(dept); } @Override public Dept queryById(Long id) { return deptDao.queryById(id); } @Override public List<Dept> queryAll() { return deptDao.queryAll(); } }
@RestController public class DeptController { @Autowired DeptService deptService; @PostMapping("/dept/add") public boolean addDept(@RequestBody Dept dept) { return deptService.addDept(dept); } @GetMapping("/dept/get/{id}") public Dept get(@PathVariable("id") Long id) { return deptService.queryById(id); } @GetMapping("/dept/list") public List<Dept> queryAll() { return deptService.queryAll(); } }
@SpringBootApplication public class DeptProvider_8001 { public static void main(String[] args) { SpringApplication.run(DeptProvider_8001.class,args); } }
子项目 消费者
创建maven子项目作为消费者
<!-- 需要在别的项目中的pojo,引入springcloud-api --> <dependency> <artifactId>springcloud-api</artifactId> <groupId>com.easyjava</groupId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
消费者不该有service层
在Configuration中添加RestTemplate的bean,使其能够通过@Autowired自动注入
@Configuration public class ConfigBean { @Bean public RestTemplate getRestTemplate(){ return new RestTemplate(); } }
controller层使用RestTemplate进行对restful风格的hhtp请求访问(即使用网络链接http://....请求数据)
@Controller public class DeptConsumerController { //理解:消费者,不应该有service层 //RestTempLate ....供我们直接调用就可以了!注册到Spring中 // (请求网络路径urL, 实体: Map ,响应类型cLass<T> responseType) @Autowired private RestTemplate restTemplate; private static final String REST_URL_PREFIX = "http://localhost:8001"; @RequestMapping("/consumer/dept/add") public boolean add(Dept dept) { return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class); } @RequestMapping("/consumer/dept/ get/{id}") public Dept get(@PathVariable("id") Long id) { return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class); } @RequestMapping("/consumer/dept/list") public List<Dept> list() { return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class); } }