@
SpringBoot搭建
- 按網上的教材搭建一個極簡的SpringBoot項目,pom文件只留下默認聲明。此時我們只需新增web聲明。
- pom文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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文件
<!-- 1. nacos 配置管理功能依賴 -->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-config-spring-boot-actuator</artifactId>
<version>0.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 代碼
- ConfigController類
@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;
}
}
- NacosConfigApplication 類
@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文件
<!-- 2. nacos 服務發現功能依賴-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.1</version>
</dependency>
- 代碼
- DiscoveryController類
@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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybatis plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<!-- mybatis plus core : QueryWrapper LambdaQuery @MapperScan-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-core</artifactId>
<version>3.3.1</version>
</dependency>
<!-- mybatis plus 代碼生成器依賴 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1</version>
</dependency>
- 代碼目錄
代碼如下:
- Controller層
@RestController
@RequestMapping(value = "/hello")
public class HelloController {
@Resource
private IBoxService boxService;
@RequestMapping(value = "/get",method = RequestMethod.GET)
public Integer getBoxCount(){
return boxService.getBoxCount();
}
}
- Service層
public interface IBoxService {
Integer getBoxCount();
}
- Service實現層
@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;
}
}
- Mapper層
@Repository
public interface BoxMapper {
Integer getBoxCount();
}
- Mapper.xml 層
<?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.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
- 展示