SpringBoot整合Nacos、MybatisPlus

文章目录

  • SpringBoot搭建
    • SpringBoot整合Nacos
      • 整合nacosConfig
      • 整合nacosDiscovery
    • SpringBoot整合MybatisPlus

SpringBoot搭建

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

SpringBoot整合Nacos

整合nacosConfig

  1. 下载nacos-client
  2. 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>
  1. 代码
  • 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);
    }
}
  1. 配置文件application.properties
nacos.config.server-addr=127.0.0.1:8848
  1. 展现
    通过访问Controller 就可以看到效果

整合nacosDiscovery

  1. pom文件

		<dependency>
			<groupId>com.alibaba.bootgroupId>
			<artifactId>nacos-discovery-spring-boot-starterartifactId>
			<version>0.2.1version>
		dependency>
  1. 代码
  • 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);
    }
}

  1. 配置文件application.properties
nacos.discovery.server-addr=127.0.0.1:8848
  1. 展现
    通过访问Controller 就可以看到效果

SpringBoot整合MybatisPlus

  1. 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>
  1. 代码目录
    SpringBoot整合Nacos、MybatisPlus_第2张图片
    代码如下:
  • 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 层
 

<mapper namespace="com.example.mapper.BoxMapper">
    <select id="getBoxCount" resultType="java.lang.Integer">
      select count(id) from box
    select>
mapper>
  1. 配置文件
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
  1. 展示
    SpringBoot整合Nacos、MybatisPlus_第3张图片

你可能感兴趣的:(小芝麻)