创建一个普通的Maven项目
4.0.0
com.kuang
springcloud
1.0-SNAPSHOT
UTF-8
8
8
4.12
1.16.10
1.2.17
pom
org.springframework.cloud
spring-cloud-dependencies
Greenwich.SR1
pom
import
org.springframework.boot
spring-boot-dependencies
2.1.4.RELEASE
pom
import
mysql
mysql-connector-java
5.1.47
com.alibaba
druid
1.1.10
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.2
ch.qos.logback
logback-core
1.2.3
junit
junit
${junit.version}
org.projectlombok
lombok
${lombok.version}
log4j
log4j
${log4j.version}
insert into dept(dname, db_source) values ('开发部',database());
insert into dept(dname, db_source) values ('人事部',database());
insert into dept(dname, db_source) values ('财务部',database());
insert into dept(dname, db_source) values ('市场部',database());
insert into dept(dname, db_source) values ('运维部',database());
select * from dept
springcloud
com.kuang
1.0-SNAPSHOT
4.0.0
springcloud-api
8
8
org.projectlombok
lombok
package com.kuang.springcloud.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
@NoArgsConstructor
@Accessors(chain = true)//链式写法
@Data
//实现序列化 实体类 ORM映射 mysql--dept
public class Dept implements Serializable {
private Long deptno;//主键
private String dname;
//这个数据库存在哪个数据库字段。~微服务,一个服务对应一个数据库,同一个信息可能存在不同的数据库
private String db_source;
public Dept(String dname) {
this.dname = dname;
}
/**
* 链式写法:
*Dept dept = new Dept();
* dept.setDeptNo(11).setDbane('sss').setDb_source("001");
*/
}
springcloud
com.kuang
1.0-SNAPSHOT
4.0.0
springcloud-provider-dept-8001
8
8
com.kuang
springcloud-api
1.0-SNAPSHOT
junit
junit
mysql
mysql-connector-java
com.alibaba
druid
ch.qos.logback
logback-core
org.mybatis.spring.boot
mybatis-spring-boot-starter
org.springframework.boot
spring-boot-test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-jetty
org.springframework.boot
spring-boot-devtools
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
# configuration: 这个配置驼峰命名转化为键值映射
# map-underscore-to-camel-case: true
#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&useSSL=false
username: root
password: 2001
package com.kuang.springcloud.dao;
import com.kuang.springcloud.pojo.Dept;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface DeptMapper {
int addDept(Dept dept);
Dept queryDeptById(Long id);
List queryAll();
}
inner join dept(dname,db_source)
values (#{dname},DATABASE())
package com.kuang.springcloud.service;
import com.kuang.springcloud.pojo.Dept;
import java.util.List;
public interface DeptService {
int addDept(Dept dept);
Dept queryDeptById(Long id);
List queryAll();
}
package com.kuang.springcloud.service.impl;
import com.kuang.springcloud.dao.DeptMapper;
import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptService;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public int addDept(Dept dept) {
return deptMapper.addDept(dept);
}
@Override
public Dept queryDeptById(Long id) {
return deptMapper.queryDeptById(id);
}
@Override
public List queryAll() {
return deptMapper.queryAll();
}
}
Controller
package com.kuang.springcloud.controller;
import com.kuang.springcloud.pojo.Dept;
import com.kuang.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
//提供Restful服务
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@PostMapping("/dept/add")
public String addDept(Dept dept){
int i = deptService.addDept(dept);
if (i>0){
return "添加成功";
}
return "添加失败";
}
@GetMapping("/dept/get/{id}")
public Dept getDept(@PathVariable("id")Long id){
Dept dept = deptService.queryDeptById(id);
return dept;
}
@GetMapping("/dept/list")
public List getAll(){
return deptService.queryAll();
}
}
package com.kuang.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DeptApplicationProvider {
public static void main(String[] args) {
SpringApplication.run(DeptApplicationProvider.class,args);
}
}