基本结构:父类工程。common工具类。provider提供者。消费者consumer (一般映射地址报错)
1...父类工程:需要配置pom.xml文件。
手动指定pom <packaging>pom</packaging>,
1 <?xml version="1.0" encoding="UTF-8"?>
2 <project xmlns="http://maven.apache.org/POM/4.0.0"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5 <modelVersion>4.0.0</modelVersion>
6
7 <groupId>com.wsc</groupId>
8 <artifactId>spring-cloud-parent</artifactId>
9 <version>1.0-SNAPSHOT</version>
10 <modules>
11 <module>../../common</module>
12 <module>../../provider</module>
13 <module>../../consumer</module>
14 </modules>
15 <!--手动指定pom-->
16 <packaging>pom</packaging>
17 <!--springboot版本 2.0.7-->
18 <parent>
19 <groupId>org.springframework.boot</groupId>
20 <artifactId>spring-boot-starter-parent</artifactId>
21 <version>2.0.7.RELEASE</version>
22 </parent>
23 <properties>
24 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
25 <maven.compiler.source>1.8</maven.compiler.source>
26 <maven.compiler.target>1.8</maven.compiler.target>
27 <junit.version>4.12</junit.version>
28 <!--spring Cloud 最新的稳定版 Finchley SR2-->
29 <spring.cloud.version>Finchley.SR2</spring.cloud.version>
30 </properties>
31 <!--依赖声明-->
32 <dependencyManagement>
33 <dependencies>
34 <dependency>
35 <groupId>org.springframework.cloud</groupId>
36 <artifactId>spring-cloud-dependencies</artifactId>
37 <version>${spring.cloud.version}</version>
38 <!-- 不可或缺-->
39 <scope>import</scope>
40 <type>pom</type>
41 </dependency>
42 <dependency>
43 <groupId>org.mybatis.spring.boot</groupId>
44 <artifactId>mybatis-spring-boot-starter</artifactId>
45 <version>1.3.2</version>
46 </dependency>
47 <!--数据源-->
48 <dependency>
49 <groupId>com.alibaba</groupId>
50 <artifactId>druid</artifactId>
51 <version>1.1.12</version>
52 </dependency>
53 <!-- 数据库 8.0.13-->
54 <dependency>
55 <groupId>mysql</groupId>
56 <artifactId>mysql-connector-java</artifactId>
57 <version>8.0.13</version>
58 </dependency>
59 <!--测试单元-->
60 <dependency>
61 <groupId>junit</groupId>
62 <artifactId>junit</artifactId>
63 <version>${junit.version}</version>
64 <scope>test</scope>
65 </dependency>
66 </dependencies>
67 </dependencyManagement>
68 </project>
2.....common:
pom不需配置 引入父类即可,创建实体类pojo
1 package com.wsc.core.pojo; 2 3 /** 4 * @version 1.0 5 * @ClassName Product 6 * @Description TODO 7 * @Author WSC 8 * @Date 2019/8/26 14:53 9 **/ 10 public class Product { 11 private Long pid; 12 private String productName; 13 private String dbSource; 14 15 public Product(String productName) { 16 this.productName = productName; 17 } 18 19 public Product(Long pid, String productName, String dbSource) { 20 this.pid = pid; 21 this.productName = productName; 22 this.dbSource = dbSource; 23 } 24 public Product() { 25 } 26 27 public Long getPid() { 28 return pid; 29 } 30 31 public void setPid(Long pid) { 32 this.pid = pid; 33 } 34 35 public String getProductName() { 36 return productName; 37 } 38 39 public void setProductName(String productName) { 40 this.productName = productName; 41 } 42 43 public String getDbSource() { 44 return dbSource; 45 } 46 47 public void setDbSource(String dbSource) { 48 this.dbSource = dbSource; 49 } 50 }
3.....provider :
1...pom.xml 依赖common
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>spring-cloud-parent</artifactId> 7 <groupId>com.wsc</groupId> 8 <version>1.0-SNAPSHOT</version> 9 <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath> 10 </parent> 11 <modelVersion>4.0.0</modelVersion> 12 13 <artifactId>provider</artifactId> 14 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 </properties> 21 <dependencies> 22 <dependency> 23 <groupId>com.wsc</groupId> 24 <artifactId>common</artifactId> 25 <version>${project.version}</version> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework.boot</groupId> 29 <artifactId>spring-boot-starter-web</artifactId> 30 </dependency> 31 <dependency> 32 <groupId>mysql</groupId> 33 <artifactId>mysql-connector-java</artifactId> 34 </dependency> 35 <dependency> 36 <groupId>org.mybatis.spring.boot</groupId> 37 <artifactId>mybatis-spring-boot-starter</artifactId> 38 </dependency> 39 <dependency> 40 <groupId>junit</groupId> 41 <artifactId>junit</artifactId> 42 </dependency> 43 <dependency> 44 <groupId>com.alibaba</groupId> 45 <artifactId>druid</artifactId> 46 </dependency> 47 <dependency> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-starter-test</artifactId> 50 </dependency> 51 </dependencies> 52 </project>
2...resources/mybatis/mybatis.cfg.xml 驼峰命名规则
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <settings> 7 <!--开启驼峰命名--> 8 <setting name="mapUnderscoreToCamelCase" value="true"/> 9 </settings> 10 </configuration>
3....resources/mybatis/mapper/ProductMapper : SQL语句
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 3 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 4 <mapper namespace="com.wsc.core.mapper.ProductMapper"> 5 <select id="findAll" resultType="Product"> 6 select * from product; 7 </select> 8 9 <select id="findById" resultType="Product" parameterType="Long"> 10 select * from product where pid=#{pid}; 11 </select> 12 13 <insert id="add" parameterType="Product" > 14 insert into product values (null,#{product_name},#{db_source}); 15 </insert> 16 </mapper>
resources/application.xml 配置端口号 ,mybatis下的包的扫描 ,数据库的连接及数量 配置
1 server: 2 port: 8001 3 mybatis: 4 config-location: classpath:mybatis/mybatis.cfg.xml #mybatis 配置文件路径 5 type-aliases-package: com.wsc.core.pojo # entity别名类所在包 6 mapper-locations: mybatis/mapper/*.xml # mapper映射文件 7 spring: 8 application: 9 name: microserver-product #这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name 10 datasource: 11 type: com.alibaba.druid.pool.DruidDataSource 12 driver-class-name: com.mysql.cj.jdbc.Driver 13 url: jdbc:mysql://127.0.0.1:3306/springcloud_db01?serverTimezone=GMT%2B8 14 password: wsc 15 username: root 16 dbcp2: 17 min-idle: 5 # 数据库连接池的最小维持连接数 18 initial-size: 5 # 初始化连接数 19 max-total: 5 # 最大连接数 20 max-wait-millis: 150 # 等待连接获取的最大超时时间
4...mapper/ProductMapper 与mapper映射文件类名相同
1 package com.wsc.core.mapper; 2 3 import com.wsc.core.pojo.Product; 4 5 import java.util.List; 6 7 public interface ProductMapper { 8 //查询全部 9 public List<Product> findAll(); 10 //id查询 11 public Product findById(Long id); 12 //插入数据 13 public Boolean add(Product product); 14 }
5...service 接口
1 package com.wsc.core.service; 2 3 import com.wsc.core.pojo.Product; 4 5 import java.util.List; 6 7 public interface ProductService { 8 public List<Product> findAll(); 9 10 public Product findById(Long id); 11 12 public Boolean add(Product product); 13 }
6...impl/ProductServiceImpl 注入 ProductMapper
1 package com.wsc.core.service.impl; 2 3 import com.wsc.core.mapper.ProductMapper; 4 import com.wsc.core.pojo.Product; 5 import com.wsc.core.service.ProductService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 9 import java.util.List; 10 11 /** 12 * @version 1.0 13 * @ClassName ProductServiceImpl 14 * @Description TODO 15 * @Author WSC 16 * @Date 2019/8/27 10:53 17 **/ 18 @Service 19 public class ProductServiceImpl implements ProductService { 20 @Autowired 21 private ProductMapper productMapper; 22 @Override 23 public List<Product> findAll() { 24 return productMapper.findAll(); 25 } 26 27 @Override 28 public Product findById(Long id) { 29 return productMapper.findById(id); 30 } 31 32 @Override 33 public Boolean add(Product product) { 34 return productMapper.add(product); 35 } 36 }
7...controller/ProductController 写入访问地址
1 package com.wsc.core.controller; 2 3 import com.wsc.core.pojo.Product; 4 import com.wsc.core.service.ProductService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.*; 7 8 import java.util.List; 9 10 /** 11 * @version 1.0 12 * @ClassName ProductController 13 * @Description TODO 14 * @Author WSC 15 * @Date 2019/8/27 11:06 16 **/ 17 @RestController //返回json数据 18 public class ProductController { 19 @Autowired 20 private ProductService productService; 21 //查询全部 22 @RequestMapping(value ="/product/get/list",method = RequestMethod.GET) 23 public List<Product> getAll(){ 24 return productService.findAll(); 25 } 26 27 //查询id 28 @RequestMapping(value = "/product/get/{pid}",method = RequestMethod.GET) 29 public Product getById(@PathVariable("pid")Long pid){ 30 return productService.findById(pid); 31 } 32 33 //添加 34 @RequestMapping(value ="/product/get/add",method = RequestMethod.POST) 35 public boolean add(@RequestBody Product product){ 36 return productService.add(product); 37 } 38 }
8...start启动类
1 package com.wsc.core; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; 7 8 /** 9 * @version 1.0 10 * @ClassName Start 11 * @Description TODO 12 * @Author WSC 13 * @Date 2019/8/27 10:54 14 **/ 15 16 @MapperScan("com.wsc.core.mapper") //mapper扫描包 类 ProductMapper 17 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) //数据库设置访问 可以不加 exclude 18 public class Start { 19 public static void main(String[] args) { 20 SpringApplication.run(Start.class,args); 21 } 22 }
4.....consumer
1...pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>spring-cloud-parent</artifactId> 7 <groupId>com.wsc</groupId> 8 <version>1.0-SNAPSHOT</version> 9 <relativePath>../springCloud/spring-cloud-parent/pom.xml</relativePath> 10 </parent> 11 <modelVersion>4.0.0</modelVersion> 12 13 <artifactId>consumer</artifactId> 14 15 16 <properties> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 18 <maven.compiler.source>1.8</maven.compiler.source> 19 <maven.compiler.target>1.8</maven.compiler.target> 20 </properties> 21 <dependencies> 22 <dependency> 23 <groupId>com.wsc</groupId> 24 <artifactId>common</artifactId> 25 <version>${project.version}</version> 26 </dependency> 27 <!--springboot web启动器--> 28 <dependency> 29 <groupId>org.springframework.boot</groupId> 30 <artifactId>spring-boot-starter-web</artifactId> 31 </dependency> 32 33 </dependencies> 34 </project>
2...resources/application.xml 配置端口号
1 server: 2 port: 800
3....ConfigBean 向容器中添加RestTemplate 组件 直接该组件调用EREST接口
1 package com.wsc.core.config; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.web.client.RestTemplate; 6 7 /** 8 * @version 1.0 9 * @ClassName ConfigBean 10 * @Description TODO 11 * @Author WSC 12 * @Date 2019/8/27 14:32 13 **/ 14 @Configuration 15 public class ConfigBean { 16 // 向容器中添加RestTemplate 组件 直接该组件调用EREST接口 17 @Bean 18 public RestTemplate getRestTemplate(){ 19 return new RestTemplate(); 20 } 21 }
4....ConfigBeanController 访问地址
1 package com.wsc.core.controller; 2 3 import com.wsc.core.pojo.Product; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.web.bind.annotation.PathVariable; 6 import org.springframework.web.bind.annotation.RequestBody; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RestController; 9 import org.springframework.web.client.RestTemplate; 10 11 import java.util.List; 12 13 /** 14 * @version 1.0 15 * @ClassName ConfigBeanController 16 * @Description TODO 17 * @Author WSC 18 * @Date 2019/8/27 14:33 19 **/ 20 @RestController 21 public class ConfigBeanController { 22 //provider的访问 端口 23 private static final String REST_URL_PREFIX="http://localhost:8001"; 24 @Autowired 25 private RestTemplate restTemplate; 26 @RequestMapping("/consumer/get/add") 27 public boolean add(@RequestBody Product product){ 28 return restTemplate.postForObject(REST_URL_PREFIX+"/product/get/add",product,boolean.class); 29 } 30 31 @RequestMapping("/consumer/get/findAll") 32 public List<Product> getAll(){ 33 return restTemplate.getForObject(REST_URL_PREFIX+"/product/get/list",List.class); 34 } 35 36 @RequestMapping("/consumer/getById/{pid}") 37 public Product getById(@PathVariable("pid") Long pid){ 38 return restTemplate.getForObject(REST_URL_PREFIX+"/product/get/"+pid,Product.class); 39 } 40 }
5....启动类
1 package com.wsc.core; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 /** 7 * @version 1.0 8 * @ClassName Start_80 9 * @Description TODO 10 * @Author WSC 11 * @Date 2019/8/27 14:31 12 **/ 13 @SpringBootApplication 14 public class Start_80 { 15 public static void main(String[] args) { 16 SpringApplication.run(Start_80.class,args); 17 } 18 }
基于springBoot,,springCloud,mybatis 框架简单 微服开发 ==CRUD
原文:https://www.cnblogs.com/wangshichang/p/11420081.html