import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@NoArgsConstructor
@Accessors(chain = true) //链式写法
public class Dept implements Serializable {//orm 类表关系映射
private Long deptno; //主键
private String dname;
//这个数据库存在哪个数据库的字段~微服务,一个服务对应一个数据库,同一个信息可能存在不同的数据库
private String db_source;
public Dept(String dname) {
this.dname = dname;
}
}
第四步 springcloud-provider-dept-8001 依赖
com.tian
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
第五步、配置application.yml
- resource
server:
port: 8001
mybatis配置
mybatis:
type-aliases-package: com.tian.springcloud.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
config-location: classpath:mybatis/mybatis-config.xml
#spring配置
spring:
application:
name: springcloud-provider-dept
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db01?useUnicode=tue&characterEncoding=utf-8&serverTimezone=GMT%2B8
username: root
password: root
第六步 dao层
@Mapper
@Repository
public interface DeptDao {
public boolean addDept(Dept dept);
public Dept queryById(Long id);
public List queryAll();
}
第七步 mapper.xml
- resource/mybaits/mapper/DeptMapper.xml
insert into dept (dname ,db_source) values
(#{dname} ,DATABASE());
select * from dept where deptno = #{deptno}
select * from dept
第八步 service
public interface DeptService {
public boolean addDept(Dept dept);
public Dept queryById(Long id);
public List queryAll();
}
第九步 serviceImpl
@Service
public class DeptServiceImpl implements DeptService{
@Autowired
private DeptDao deptDao;
public boolean addDept(Dept dept) {
return deptDao.addDept(dept);
}
public Dept queryById(Long id) {
return deptDao.queryById(id);
}
public List queryAll() {
return deptDao.queryAll();
}
}
第十步 controller
//提供restful服务!
@RestController
public class DeptController {
@Autowired
private DeptService deptService;
@PostMapping(“/dept/add”)
public boolean addDept(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 queryAll(){
return deptService.queryAll();
}
}
第十一步 主启动类
//启动类
@SpringBootApplication
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
}
测试 访问~
第一步、new一个maven 依赖
com.tian
springcloud-api
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
第二步、application.yml
server: