1.去官方网站//spring.io/tools/sts/all下载eclipse对应版本的springsource-tool-suite-x.x.x.RELEASE-ex.x.x-updatesite.zip插件,我这里是eclipse2018(4.8版本 ),所以选springsource-tool-suite-3.9.8.RELEASE-e4.8.0-updatesite.zip
2.打开eclipse— —help— —install new software正常安装就行。
3.安装成功后,在新建项目中多了一个创建Spring Boot,一直下一步就能创建完成。
4.修改pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
5.配置自己的maven,并修改conf文件夹中的settings.xml,最后右键项目选maven-update更新jar包
6.创建类TestSingleRedis,用来测试jedis
public class TestSingleRedis {
private static Jedis jedis; // 单实例
private static ShardedJedis shard; // 分片
private static ShardedJedisPool pool; // 池化
@BeforeClass
public static void setUpBeforeClass() throws Exception{
//单个节点
jedis = new Jedis("192.168.30.15",6379);
jedis.auth("wangzhongyan");
//分片
JedisShardInfo jedisShardInfo = new JedisShardInfo("192.168.30.15",6379);
jedisShardInfo.setPassword("wangzhongyan");
List<JedisShardInfo> shards = Arrays.asList(jedisShardInfo);
shard = new ShardedJedis(shards);
//Java通用对象池GenericObjectPool
GenericObjectPoolConfig goConfig = new GenericObjectPoolConfig();
goConfig.setMaxTotal(100);
goConfig.setMaxIdle(20);
goConfig.setMaxWaitMillis(-1);
goConfig.setTestOnBorrow(true);
pool = new ShardedJedisPool(goConfig,shards);
}
@AfterClass
public static void tearDownAfterClass() throws Exception{
jedis.disconnect();
shard.disconnect();
pool.destroy();
}
/**
* redis操作string
*/
@Test
public void testString() {
jedis.set("name","sunlin");// key-->name中放入value
System.out.println(jedis.get("name"));//执行结果
jedis.append("name", " is my lover"); //字符串拼接
System.out.println(jedis.get("name"));
jedis.del("name");//删除键
System.out.println(jedis.get("name"));
//同时设置多个键值
jedis.mset("name","yuxiaoye","age","30","qq","174442211");
jedis.incr("age"); // 自增1
System.out.println(jedis.get("name")+"-"+jedis.get("age")+"-"+jedis.get("qq"));
}
/**
* redis操作Map
*/
@Test
public void testMap() {
Map<String,String> map = new HashMap<String,String>();
map.put("name", "shenyang");
map.put("age", "20");
map.put("qq", "12334");
jedis.hmset("user", map);
//第一个参数是集合的key,后面的参数是元素的key并且可以放多个
List<String> rsmap = jedis.hmget("user", "name","age","qq");
System.out.println(rsmap);//执行结果
//删除user中的某个键
jedis.hdel("user", "age");
System.out.println(jedis.hmget("user", "age"));//删除后返回空
System.out.println(jedis.hlen("user"));//返回集合的元素个数
System.out.println(jedis.exists("user"));//验证key为user的集合是否存在
System.out.println(jedis.hkeys("user"));//返回所有key的集合
System.out.println(jedis.hvals("user"));//返回所有value的集合
Iterator<String> iter = jedis.hkeys("user").iterator();
while(iter.hasNext()) {
String key =iter.next();
System.out.println(key+":"+jedis.hmget("user", key));
}
}
/**
* redis操作list
*/
@Test
public void testList() {
// 开始,先移除所有的内容
jedis.del("java framework");
System.out.println(jedis.lrange("java framework", 0, -1));
// 分别向redis的list中添加三条数据
// lpush 先进后出
jedis.lpush("java framework","spring");
jedis.lpush("java framework","struts");
jedis.lpush("java framework","hibernate");
//取出redis中的所有数据
//第一个是key,第二个是起始位置,第三个是结束位置
System.out.println(jedis.lrange("java framework", 0, -1));
// rpush 先进先出
jedis.del("java framework");
jedis.rpush("java framework","spring");
jedis.rpush("java framework","struts");
jedis.rpush("java framework","hibernate");
System.out.println(jedis.lrange("java framework", 0, -1));
}
/**
* redis操作Set
*/
@Test
public void testSet() {
// 向suser集合中添加数据
jedis.sadd("suser","zhangsan");
jedis.sadd("suser","lisi");
jedis.sadd("suser","wangwu");
jedis.sadd("suser","baiji");
//移除suser集合中的baiji元素
jedis.srem("suser", "baiji");
System.out.println(jedis.smembers("suser"));//获得所有的加入的value
System.out.println(jedis.sismember("suser","baiji"));//验证baiji 是否在suser集合中存在
System.out.println(jedis.srandmember("suser"));//随机取出suser集合中的元素
System.out.println(jedis.scard("suser"));//获得集合中的个数
}
@Test
public void testRLpush() throws Exception{
//jedis 排序
//注意,此处的rpush和lpush是List的操作,是一个双向链表
jedis.del("a");
jedis.rpush("a","1");
jedis.lpush("a","6");
jedis.lpush("a","3");
jedis.lpush("a","9");
System.out.println(jedis.lrange("a",0,-1));// {9,3,6,1}
System.out.println(jedis.sort("a"));// {1,3,6,9}
System.out.println(jedis.lrange("a",0,-1));
}
@Test
public void testTrans() throws Exception{
long start = System.currentTimeMillis();
Transaction tx = jedis.multi();
for(int i =0; i< 1000; i++) {
tx.set("t"+i, "t"+i);
}
List<Object> result = tx.exec();
long end = System.currentTimeMillis();
System.out.println("Transaction SET:"+((end-start)/1000.0)+" seconds");
}
@Test
public void testPipeLined() throws Exception{
Pipeline pipeline = jedis.pipelined();
long start = System.currentTimeMillis();
for(int i =0; i< 1000; i++) {
pipeline.set("p"+i, "p"+i);
}
List<Object> result = pipeline.syncAndReturnAll();
long end = System.currentTimeMillis();
System.out.println("Transaction SET:"+((end-start)/1000.0)+" seconds");
}
@Test
public void testPipeLineTrans() throws Exception{
long start = System.currentTimeMillis();
Pipeline pipeline = jedis.pipelined();
pipeline.multi();
for(int i =0; i< 1000; i++) {
pipeline.set("p"+i, "p"+i);
}
pipeline.exec();
List<Object> result = pipeline.syncAndReturnAll();
long end = System.currentTimeMillis();
System.out.println("testPipeLineTrans SET:"+((end-start)/1000.0)+" seconds");
}
@Test
public void testJedis() throws Exception{
long start = System.currentTimeMillis();
for(int i =0; i< 100000; i++) {
jedis.set("jedis"+i, "n"+i);
}
long end = System.currentTimeMillis();
System.out.println("jedis SET:"+((end-start)/1000.0)+" seconds");
}
@Test
public void testShard() throws Exception{
long start = System.currentTimeMillis();
for(int i =0; i< 100000; i++) {
shard.set("shard"+i, "n"+i);
}
long end = System.currentTimeMillis();
System.out.println("shard SET:"+((end-start)/1000.0)+" seconds");
}
@Test
public void testShardPipeLined() throws Exception{
ShardedJedisPipeline pipeline = shard.pipelined();
long start = System.currentTimeMillis();
for(int i =0; i< 100000; i++) {
pipeline.set("p"+i, "p"+i);
}
List<Object> result = pipeline.syncAndReturnAll();
long end = System.currentTimeMillis();
System.out.println("shardedJedisPipeline SET:"+((end-start)/1000.0)+" seconds");
}
@Test
public void testShardPool() throws Exception{
ShardedJedis sj = pool.getResource();
long start = System.currentTimeMillis();
for(int i =0; i< 100000; i++) {
sj.set("p"+i, "p"+i);
}
long end = System.currentTimeMillis();
pool.returnResourceObject(sj);
System.out.println("testShardPool SET:"+((end-start)/1000.0)+" seconds");
}
}
7.使用Junit测试方法,并输出到控制台