基于浏览器的插件sider,使用chatgpt3.5生成的连接Redis的代码

 一.安装

首先在浏览器安装Sider插件,具体安装步骤https://www.423xz.com/gaoxiao/3949.html

收费情况:每天有几次的免费使用chatgpt3.5的次数,chatgpt4需要付费了。

二.使用步骤:

安装后使用,具体步骤:

1.找到扩展

 2.找到对应的sider

 3.选择3.5版本,在输入框输入提示词

 三.通过提示词,生成具体内容

我这里想生成springboot连接redis的代码,使用模板方法。为了更加精确,我直接指定了具体的版本号,避免歧义。

提示词:

用spring boot2.5编写连接redis4.0的demo,使用模板方法。

获得回答结果:

下面是一个使用Spring Boot 2.5连接Redis 4.0的示例代码,使用RedisTemplate作为模板方法:

首先,确保在pom.xml文件中添加以下依赖:

<dependencies>
    <!-- Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
</dependencies>
接下来,在application.properties文件中配置Redis连接信息:

# Redis
spring.redis.host=localhost
spring.redis.port=6379
然后,创建一个Redis配置类,用于配置Redis连接:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;

@Configuration
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHost, redisPort);
        return new JedisConnectionFactory(redisStandaloneConfiguration);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Object.class));
        return redisTemplate;
    }
}
最后,创建一个使用RedisTemplate的示例类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public class RedisDemo {

    private final RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public RedisDemo(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

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

    public Object getValue(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}
现在,你可以在其他类中使用RedisDemo类来操作Redis了:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    private final RedisDemo redisDemo;

    @Autowired
    public DemoApplication(RedisDemo redisDemo) {
        this.redisDemo = redisDemo;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    public void run(String... args) {
        String key = "myKey";
        String value = "myValue";

        redisDemo.setValue(key, value);

        Object retrievedValue = redisDemo.getValue(key);
        System.out.println(retrievedValue);
    }
}
这是一个简单的使用Spring Boot 2.5连接Redis 4.0的示例,使用了模板方法RedisTemplate来操作Redis。你可以根据自己的需求进行修改和扩展。

 对回答结果分析:

答案确实把关键核心内容写出来了,但是要直接使用,需要自己放到项目中,可以作为辅助编程。

但是也需要注意,这里的连接方法非常简单,只有一个set 和get  value,并没有具体针对详细数据类型的操作方法,

四.针对结果优化改进

因为上面的结果有些笼统,我就继续要求针对详细的数据类型进行操作。为了测试是否具有上下文的理解能力,我直接讲针对详细数据类型的操作,不做其他的提示,看效果是具有上下文理解能力的,给出了更加详细的结果

提示词2:

方法再详细一些,增加针对具体数据类型的操作方法

 回答结果:

好的,下面是一个更详细的示例,包括了针对具体数据类型的操作方法:

首先,确保在pom.xml文件中添加以下依赖:

<dependencies>
    <!-- Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
</dependencies>
接下来,在application.properties文件中配置Redis连接信息:

# Redis
spring.redis.host=localhost
spring.redis.port=6379
然后,创建一个Redis配置类,用于配置Redis连接:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericToStringSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private int redisPort;

    @Bean
    public JedisConnectionFactory jedisConnectionFactory() {
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(redisHost, redisPort);
        return new JedisConnectionFactory(redisStandaloneConfiguration);
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory());
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new GenericToStringSerializer<>(Object.class));
        return redisTemplate;
    }
}
然后,创建一个使用RedisTemplate的示例类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class RedisDemo {

    private final RedisTemplate<String, Object> redisTemplate;

    @Autowired
    public RedisDemo(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

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

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

    public void setList(String key, List<Object> values) {
        redisTemplate.opsForList().leftPushAll(key, values);
    }

    public List<Object> getList(String key) {
        return redisTemplate.opsForList().range(key, 0, -1);
    }

    public void setHash(String key, Map<String, Object> values) {
        redisTemplate.opsForHash().putAll(key, values);
    }

    public Map<Object, Object> getHash(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    public void setSet(String key, Set<Object> values) {
        redisTemplate.opsForSet().add(key, values.toArray());
    }

    public Set<Object> getSet(String key) {
        return redisTemplate.opsForSet().members(key);
    }
}
现在,你可以在其他类中使用RedisDemo类来操作Redis了:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@SpringBootApplication
public class DemoApplication {

    private final RedisDemo redisDemo;

    @Autowired
    public DemoApplication(RedisDemo redisDemo) {
        this.redisDemo = redisDemo;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    public void run(String... args) {
        String key = "myKey";
        String value = "myValue";

        redisDemo.setValue(key, value);

        Object retrievedValue = redisDemo.getValue(key);
        System.out.println(retrievedValue);

        String listKey = "myList";
        redisDemo.setList(listKey, Arrays.asList("value1", "value2", "value3"));

        List<Object> retrievedList = redisDemo.getList(listKey);
        System.out.println(retrievedList);

        String hashKey = "myHash";
        Map<String, Object> hashValues = new HashMap<>();
        hashValues.put("field1", "value1");
        hashValues.put("field2", "value2");
        hashValues.put("field3", "value3");
        redisDemo.setHash(hashKey, hashValues);

        Map<Object, Object> retrievedHash = redisDemo.getHash(hashKey);
        System.out.println(retrievedHash);

        String setKey = "mySet";
        Set<Object> setValues = new HashSet<>(Arrays.asList("value1", "value2", "value3"));
        redisDemo.setSet(setKey, setValues);

        Set<Object> retrievedSet = redisDemo.getSet(setKey);
        System.out.println(retrievedSet);
    }
}
这是一个更详细的示例,包括了针对具体数据类型的操作方法。你可以根据自己的需求进行修改和扩展。

截个图:

 结果分析:

增加了hash,list,set三种数据类型的操作方法,复杂的数据类型的也没有,不过基本也够用。

如果小伙伴们感兴趣,也可以安装sider,来体验chatgtp所带来的便利奥,它确实拥有上下文的理解能力,只是我们也需要做好关键词的提示,并且要对结果进行分析后再使用,毕竟就算跟人交流,也不一定会得到完全满意的答复,对chatgtp也可能需要对回答内容进行鉴别,对回答的不满意的要接着提出来,让她继续给出优化的答案。当然提示词要学习一下怎样来说也很关键。

再就是作为代码生成,最终还是需要拿到开发工具中使用的,可以具体连接一下,测试一下生成代码的准确性。

猜你喜欢

转载自blog.csdn.net/jiao_zg/article/details/132314450