总体介绍
插入一些数据:
父Maven工程:springcloud
父Maven工程下的三个模块:
springcloud-api【封装整体的entity/公共配置等】
springcloud-provider-dept-8001【服务提供者】
springcloud-consumer-dept-80【服务消费者】
版本选择
父Maven工程:springcloud
<packaging>pom</packaging>
<properties>
<mybatis-version>2.1.2</mybatis-version>
<druid-version>1.1.20</druid-version>
<lombok-version>1.16.16</lombok-version>
</properties>
<dependencyManagement>
<dependencies>
<!-- SpringCloud依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- SpringBoot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- MyBatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-version}</version>
</dependency>
<!-- Druid依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid-version}</version>
</dependency>
<!-- Lombok依赖 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
springcloud-api【封装整体的entity/公共配置等】
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
@Data
@NoArgsConstructor
public class Dept implements Serializable {
private long deptno;
private String deptname;
private String dbSource;
public Dept(String deptname){
this.deptname=deptname;
}
}
springcloud-provider-dept-8001【服务提供者】
在父Maven项目下新建一个Module,创建子Maven项目springcloud-provider-dept-8001。
在pom文件中添加依赖。
<dependencies>
<dependency>
<groupId>com.yinrz</groupId>
<artifactId>springcloud-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
</dependencies>
server:
port: 8001
mybatis:
type-aliases-package: com.yinrz.springcloud.entity
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/sc01?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
<?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>
<settings>
<setting name="cacheEnabled" value="true"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
@Mapper
@Repository
public interface DeptMapper {
List<Dept> selAll();
Dept selById(long id);
boolean insDept(Dept dept);
}
public interface DeptService {
List<Dept> queryAll();
Dept queryById(long id);
boolean addDept(Dept dept);
}
@Service
public class DeptServiceImpl implements DeptService {
@Resource
private DeptMapper deptMapper;
public List<Dept> queryAll(){
return deptMapper.selAll();
}
public Dept queryById(long id) {
return deptMapper.selById(id);
}
public boolean addDept(Dept dept) {
return deptMapper.insDept(dept);
}
}
<?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.yinrz.springcloud.mapper.DeptMapper">
<select id="selAll" resultType="com.yinrz.springcloud.entity.Dept" >
select * from sc01.dept
</select>
<select id="selById" resultType="com.yinrz.springcloud.entity.Dept" parameterType="long" >
select * from sc01.dept where deptno = #{deptno}
</select>
<insert id="insDept" parameterType="com.yinrz.springcloud.entity.Dept" >
insert into sc01.dept(deptname, db_source) values (#{deptname},DATABASE())
</insert>
</mapper>
@RestController
public class DeptProviderController {
@Resource
DeptService deptServiceImpl;
@GetMapping("/dept/all")
public List<Dept> queryAll(){
return deptServiceImpl.queryAll();
}
@PostMapping("/dept/add")
public boolean addDept(Dept dept){
return deptServiceImpl.addDept(dept);
}
@GetMapping("/dept/get/{id}")
public Dept queryById(@PathVariable("id") long id){
return deptServiceImpl.queryById(id);
}
}
@SpringBootApplication
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
}
springcloud-consumer-dept-80【服务消费者】
在父Maven项目下新建一个Module,创建子Maven项目springcloud-consumer-dept-80。
在pom文件中添加依赖。
<dependencies>
<dependency>
<groupId>com.yinrz</groupId>
<artifactId>springcloud-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
server:
port: 80
@Configuration
public class BeanConfig {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
@RestController
public class DeptConsumerController {
@Resource
RestTemplate restTemplate;
private static final String REST_PATH_PREFIX="http://localhost:8001";
@RequestMapping("/consumer/dept/all")
public List<Dept> all(){
return restTemplate.getForObject(REST_PATH_PREFIX+"/dept/all",List.class);
}
@RequestMapping("/consumer/dept/get/{id}")
public Dept get(@PathVariable("id") long id){
return restTemplate.getForObject(REST_PATH_PREFIX+"/dept/get/"+id,Dept.class);
}
@RequestMapping("/consumer/dept/add")
public boolean add(Dept dept){
return restTemplate.postForObject(REST_PATH_PREFIX+"/dept/add",dept,Boolean.class);
}
}
@SpringBootApplication
public class DeptConsumer_80 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumer_80.class,args);
}
}
测试
浏览器访问http://localhost/consumer/dept/add?deptname=123
浏览器访问http://localhost/consumer/dept/all
原文:https://www.cnblogs.com/yinrz/p/12748972.html