文章目录
- SpringBoot搭建
- SpringBoot整合Nacos
- 整合nacosConfig
- 整合nacosDiscovery
- SpringBoot整合MybatisPlus
SpringBoot搭建
- 按网上的教材搭建一个极简的SpringBoot项目,pom文件只留下默认声明。此时我们只需新增web声明。
- pom文件
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
- 代码
@RestController
@RequestMapping(value = "/hello")
public class HelloController {
@RequestMapping(value = "/")
String hello(){
return "hello cc ";
}
}
- 配置文件application.properties
server.port=8080
- 展现
实现如图:

SpringBoot整合Nacos
整合nacosConfig
- 下载nacos-client
- pom文件
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-config-spring-boot-starterartifactId>
<version>0.2.1version>
dependency>
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-config-spring-boot-actuatorartifactId>
<version>0.2.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
- 代码
@Controller
@RequestMapping("config")
public class ConfigController {
@NacosValue(value = "${useLocalCache:false}", autoRefreshed = true)
private boolean useLocalCache;
@RequestMapping(value = "/get", method = RequestMethod.GET)
@ResponseBody
public boolean get() {
return useLocalCache;
}
}
@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class NacosConfigApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConfigApplication.class, args);
}
}
- 配置文件application.properties
nacos.config.server-addr=127.0.0.1:8848
- 展现
通过访问Controller 就可以看到效果
整合nacosDiscovery
- pom文件
<dependency>
<groupId>com.alibaba.bootgroupId>
<artifactId>nacos-discovery-spring-boot-starterartifactId>
<version>0.2.1version>
dependency>
- 代码
@Controller
@RequestMapping("discovery")
public class DiscoveryController {
@NacosInjected
NamingService namingService;
@RequestMapping(value = "/get", method = RequestMethod.GET)
@ResponseBody
public List<Instance> get(@RequestParam String serviceName) throws NacosException {
return namingService.getAllInstances(serviceName);
}
}
- NacosDiscoveryApplication 类
@SpringBootApplication
public class NacosDiscoveryApplication {
public static void main(String[] args) {
SpringApplication.run(NacosDiscoveryApplication.class, args);
}
}
- 配置文件application.properties
nacos.discovery.server-addr=127.0.0.1:8848
- 展现
通过访问Controller 就可以看到效果
SpringBoot整合MybatisPlus
- pom文件
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.3.1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-coreartifactId>
<version>3.3.1version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>3.3.1version>
dependency>
- 代码目录

代码如下:
@RestController
@RequestMapping(value = "/hello")
public class HelloController {
@Resource
private IBoxService boxService;
@RequestMapping(value = "/get",method = RequestMethod.GET)
public Integer getBoxCount(){
return boxService.getBoxCount();
}
}
public interface IBoxService {
Integer getBoxCount();
}
@Service
public class BoxServiceImpl implements IBoxService {
@Resource
private BoxMapper boxMapper;
@Override
public Integer getBoxCount() {
return boxMapper.getBoxCount();
}
}
public class Box {
private BigInteger id;
private String name;
public BigInteger getId() {
return id;
}
public void setId(BigInteger id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@Repository
public interface BoxMapper {
Integer getBoxCount();
}
<mapper namespace="com.example.mapper.BoxMapper">
<select id="getBoxCount" resultType="java.lang.Integer">
select count(id) from box
select>
mapper>
- 配置文件
spring.datasource.url=jdbc:mysql://localhost:3306/simo?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=11111
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis-plus.type-aliases-package=com.example.entity
mybatis-plus.configuration.map-underscore-to-camel-case=true
- 展示
