关于Redis在Maven项目中的一个简单操作

关于Redis在Maven项目中的一个简单操作

一、新建maven项目。
二、maven配置文件pom.xml里面引入Redis等相关的一个jar包。

		    <dependency>
			    <groupId>com.alibaba</groupId>
			    <artifactId>fastjson</artifactId>
			    <version>1.2.47</version>
			</dependency>
			    
		    <dependency>
			    <groupId>redis.clients</groupId>
				<artifactId>jedis</artifactId>
			    <version>3.1.0</version>
				<type>jar</type>
		    <scope>compile</scope>

三、添加相关的一个类
相关的一个结构
在RedisDemo.java类里面对Redis进行一个相关的操作,具体如下:
List数据的添加:

	// redis的路径ip
	String h = " ";
	// 密码
	String passwd = " ";
	/**
	 * 引入类 将redis服务器的ip地址进行一个加入
	 */
	JedisShardInfo jedisShardInfo = new JedisShardInfo(h);
	/**
	 * 设置一个redis的密码 如果没有设置的密码的话 则可能会出现 NOAUTH Authentication required. 身份验证出现问题
	 */
	jedisShardInfo.setPassword(passwd);
	Jedis jedis = new Jedis(jedisShardInfo);

	// List集合数据的一个添加
	ArrayList<UserEntity> arrayList = new ArrayList<>();
	UserEntity user = new UserEntity("a", "男", 18, "四川");
	UserEntity user1 = new UserEntity("a1", "男", 18, "四川");
	UserEntity user2 = new UserEntity("a2", "男", 18, "四川");
	UserEntity user3 = new UserEntity("a3", "男", 18, "四川");

	arrayList.add(user);
	arrayList.add(user1);
	arrayList.add(user2);
	arrayList.add(user3);

	jedis.set("user".getBytes(), SerUtil.serialize(arrayList));
	byte[] bytes = jedis.get("user".getBytes());

	@SuppressWarnings("unchecked")
	List<UserEntity> deserialize = (List<UserEntity>) SerUtil.noserialize(bytes);

	System.err.println(deserialize);

String数据的添加:

	UserEntity user = new UserEntity("王五", "男", 18, "四川");
	/**
	 * 引入类 将redis服务器的ip地址进行一个加入
	 */
	JedisShardInfo jedisShardInfo = new JedisShardInfo(h);
	/**
	 * 设置一个redis的密码 如果没有设置的密码的话 则可能会出现 NOAUTH Authentication required. 身份验证出现问题
	 */
	jedisShardInfo.setPassword(passwd);
	Jedis jedis = new Jedis(jedisShardInfo);

	/**
	 * 添加一个redis数据 key为demo
	 */
	// 将user对象序列化
	jedis.set("user".getBytes(), SerUtil.serialize(user));

	/**
	 * 从redis获取到redis key 反序列化输出
	 */
	byte[] bytes = jedis.get("user".getBytes());

	UserEntity deserialize = (UserEntity) SerUtil.noserialize(bytes);

	System.err.println(deserialize);

在进行操作的时候注意redis服务器的ip与密码的设置。
源码地址:https://github.com/projectBoss/mavenRedis.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值