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

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

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

		    
			    com.alibaba
			    fastjson
			    1.2.47
			
			    
		    
			    redis.clients
				jedis
			    3.1.0
				jar
		    compile

三、添加相关的一个类
关于Redis在Maven项目中的一个简单操作_第1张图片
在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 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 deserialize = (List) 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

你可能感兴趣的:(Redis)