账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    springboot 如何邂逅utf8mb4
    58
    0

    springboot中连接数据时想使用utf8mb4,不知配置文件application.properties中有没有相关属性可以配置呢?

    Cannot store “Pile of Poo” unicode emoji using Spring Boot Hibernate and MySQL看到个yml 文件的配置,但发现有些属性写法与properties文件不太一样。
    yml 文件中:

    spring:
    datasource:
     driverClassName: com.mysql.jdbc.Driver
     connectionInitSqls: "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;"
    jpa:
    hibernate:
     connection:
         CharSet: utf8mb4

    properties文件中,就知道

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver

    其他的属性是不是可以这样对应的写:

    spring.datasource.connectionInitSqls = "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;"
    spring.jpa.hibernate.connection.CharSet=utf8mb4

    还有像这些配置字段有没有文档可以查呢?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 在Spring Boot中邂逅UTF-8mb4主要是通过Maven配置文件和类路径中引入相关库实现的。以下是详细的步骤:

      1. 在pom.xml文件中,添加以下依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-xml</artifactId> </dependency>

      2. 在application.properties文件中,添加以下配置: spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=root

      3. 创建一个Java配置类,例如RedisConfig.java: ``` import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.data.redis.core.RedisTemplate;

      @ConfigurationProperties(prefix = "spring.redis") public class RedisConfig { private String host; private int port; private String password;

      // getters and setters
      

      } ```

      1. 在RedisTemplate的构造函数中,使用spring.redis.host和spring.redis.port参数设置数据库连接信息: ``` @Autowired private RedisConfig redisConfig;

      public RedisTemplate redisTemplate() { return new RedisTemplate<>(); }

      public RedisTemplate withRedisConfig(RedisConfig redisConfig) { this.redisConfig = redisConfig; return this; } ```

      1. 使用RedisTemplate对象进行数据操作,例如查找、设置等: ``` @Autowired private RedisTemplate redisTemplate;

      public void add(String key, String value) { redisTemplate.opsForValue().set(key, value); }

      public String get(String key) { return redisTemplate.opsForValue().get(key); } ```

      1. 在主类中,配置Spring Boot和Redis的依赖: ``` import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.redis.core.RedisTemplate;

      @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ```

      现在,当你启动Spring Boot应用时,Redis将会与Spring Boot集成,你可以使用Spring Data Redis来操作数据库。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部