• Redis简单介绍以及使用


    Redis

    一、初始redis

    redies一般有做缓存,在以前我们操作数据是都是从数据库中取,但数据库中的数据是存放在磁盘中,每一次从数据库中取数据时都会涉及到IO操作比较耗时,所以引用了缓存机制

    1.1 redis软件

    阿里网盘地址:https://www.aliyundrive.com/s/tkuu1vKaoky

    软件打开后的页面

    在这里插入图片描述

    redis-cli.exe:是redis的操作程序

    redis-server.exe:是redis的客户端

    redis.windows.conf ;是redis的配置文件

    注意:在使用radis的时候一定要启动redis的客户端

    打开客户端的命令:redis-server.exe 全路径名\redis.windows.conf

    在这里插入图片描述

    注意:要在redis安装文件的根目录下执行这个命令

    看到这个页面后,就说明redis的客户端已经开启,此时就不要去动它了,直接去执行redis-cli.exe就可以输入redis的相关命令

    1.2 redis的数据结构,及相关命令

    1.2.1 数据结构

    redis存储结构就是key:value,它的key都是字符串类型,value有5中数据类型:

    字符串

    list 列表类型 有序 重复

    ​ set集合 :无序(存取顺序不一致) 不重复

    ​ hash 哈希类型

    ​ sortedset 有序集合类型 :有序 ,不允许重复

    ####1.2.2 相关命令

    #####String:字符串类型

    命令格式:
    set key:value
    
    **get key**
    
    **del key**
    
    **append key value**
    
    **expire key time**
    
    **setnx key value
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    127.0.0.1:6379> set name lisi  //向缓存中添加数据 key: name value : list
    OK
    127.0.0.1:6379> get name //从缓存中取数据 get key
    "lisi"
    127.0.0.1:6379> set name wangwu //覆盖缓存中已经存在的数据
    OK
    127.0.0.1:6379> get name
    "wangwu"
    127.0.0.1:6379> del name//删除 
    (integer) 1
    127.0.0.1:6379> get name
    (nil)
    127.0.0.1:6379> set num 1
    OK
    127.0.0.1:6379> get num
    "1"
    127.0.0.1:6379> incr num//缓存中key 对应的value自增1
    (integer) 2
    127.0.0.1:6379> decr num//缓存中key 对应的value自减1
    (integer) 1
    127.0.0.1:6379> incrby num 3//缓存中key 对应的value自增3
    (integer) 4
    127.0.0.1:6379> decrby num 3//缓存中key 对应的value自减3
    (integer) 1
    127.0.0.1:6379> set str hello
    OK
    127.0.0.1:6379> append str world//向key对应的value后追加world
    (integer) 10
    127.0.0.1:6379> strlen str//获取key对应value的长度
    (integer) 10
    127.0.0.1:6379> mset age 12 address usa score 65//添加多个
    OK
    127.0.0.1:6379> keys *//查询所有的key
    1) "itemId"
    2) "age"
    3) "address"
    4) "json_province"
    5) "num"
    6) "mylist"
    7) "str"
    8) "user"
    9) "zset"
    10) "myset"
    11) "score"
    127.0.0.1:6379> expire name 30
    (integer) 0
    127.0.0.1:6379> expire age 30//设置age 的过期时间 为 30s,30s后在age在缓存中就无法获取
    (integer) 1
    127.0.0.1:6379> get age
    "12"
    127.0.0.1:6379> get age
    (nil)
    127.0.0.1:6379> setnx address china//判断缓存中是否有key,如果存在就无操作,否则就添加
    (integer) 0
    127.0.0.1:6379>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    哈希类型 hash
    //命令格式
    hset key field value :添加
    
    hget key field:获取
    
    hdel key field:删除
    
    hgetall key:查询所有
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    127.0.0.1:6379> hset myhash name lisi//添加
    (integer) 1
    127.0.0.1:6379> hset myhash password 123
    (integer) 1
    127.0.0.1:6379> hget myhash name//获取
    "lisi"
    127.0.0.1:6379> hget myhash password
    "123"
    127.0.0.1:6379> hgetall myhash//显示所有
    1) "name"
    2) "lisi"
    3) "password"
    4) "123"
    127.0.0.1:6379> hdel myhash password
    (integer) 1
    127.0.0.1:6379> hget myhash password
    (nil)
    127.0.0.1:6379> hlen myhash//获取长度
    (integer) 1
    127.0.0.1:6379> hset user name wuwu
    (integer) 1
    127.0.0.1:6379> hset user age 12
    (integer) 0
    127.0.0.1:6379> hset user password 321
    (integer) 1
    127.0.0.1:6379> hgetall user
    1) "username"
    2) "zhangsan"
    3) "age"
    4) "12"
    5) "address"
    6) "CHN"
    7) "name"
    8) "wuwu"
    9) "password"
    10) "321"
    127.0.0.1:6379> hlen user
    (integer) 5
    127.0.0.1:6379> hkeys user
    1) "username"
    2) "age"
    3) "address"
    4) "name"
    5) "password"
    127.0.0.1:6379> hvals user
    1) "zhangsan"
    2) "12"
    3) "CHN"
    4) "wuwu"
    5) "321"
    127.0.0.1:6379>
    127.0.0.1:6379> hincrby user age 2
    (integer) 14
    127.0.0.1:6379> hget user age
    "14"
    127.0.0.1:6379>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    list 列表类型

    left:左 right: 右

    push 入

    pop 出

    这个list就好比一个队列:先进先出

    lpush key value 从左边入队

    lrange key start step :start 0 表示从下标0开始 -1表示步长 -1, 0 -1显示list中所有信息

    lpop key 从左边出

    同理

    rpush

    rpop

    > 127.0.0.1:6379> lpush list a 
    > (integer) 1
    > 127.0.0.1:6379> lpush list b
    > (integer) 2
    > 127.0.0.1:6379> lpush list c
    > (integer) 3
    > 127.0.0.1:6379> lpush list d
    > (integer) 4
    > 127.0.0.1:6379> lrange list 0 -1
    > 1) "d"
    > 2) "c"
    > 3) "b"
    > 4) "a"
    > 127.0.0.1:6379> rpush list a
    > (integer) 5
    > 127.0.0.1:6379> lrange list 0 -1
    > 1) "d"
    > 2) "c"
    > 3) "b"
    > 4) "a"
    > 5) "a"
    > 127.0.0.1:6379> lpop list
    > "d"
    > 127.0.0.1:6379> lrange list 0 -1
    > 1) "c"
    > 2) "b"
    > 3) "a"
    > 4) "a"
    > 127.0.0.1:6379> rpop list
    > "a"
    > 127.0.0.1:6379> lrange list 0 -1
    > 1) "c"
    > 2) "b"
    > 3) "a"
    > 127.0.0.1:6379> lindex list 2
    > "a"
    > 127.0.0.1:6379> llen list
    > (integer) 3
    > 127.0.0.1:6379> lrem list 1 b
    > (integer) 1
    > 127.0.0.1:6379>
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    set 集合 不重复,存取无序

    sadd key vale :添加

    smembers key :显示

    srem key value:删除

    s1集合 11 12 13 14

    s2集合 13 14 15 16 17

    sunion s1 s2//两个集合取并集

    sdiff s1 s2//取s1的差集

    sinter s1 s2//取交集

    127.0.0.1:6379> sadd set a
    (integer) 1
    127.0.0.1:6379> sadd set b
    (integer) 1
    127.0.0.1:6379> smembers set
    1) "a"
    2) "b"
    127.0.0.1:6379> sadd set c
    (integer) 1
    127.0.0.1:6379> sadd set d e f
    (integer) 3
    127.0.0.1:6379> smember set
    (error) ERR unknown command 'smember'
    127.0.0.1:6379> smembers set
    1) "b"
    2) "f"
    3) "a"
    4) "d"
    5) "e"
    6) "c"
    127.0.0.1:6379> sadd set c
    (integer) 0
    127.0.0.1:6379>
    127.0.0.1:6379> srem set c
    (integer) 1
    127.0.0.1:6379> smembers set
    1) "f"
    2) "a"
    3) "d"
    4) "e"
    5) "b"
    127.0.0.1:6379> sadd s1 11 12 13 14
    (integer) 4
    127.0.0.1:6379> sadd s2 13 14 15 16 17
    (integer) 5
    127.0.0.1:6379> sunion s1 s2//两个集合取并集
    1) "11"
    2) "12"
    3) "13"
    4) "14"
    5) "15"
    6) "16"
    7) "17"
    127.0.0.1:6379> sdiff s1 s2//取s1的差集:s1 - s2 == s1 - (s1 交 s2)
    1) "11"
    2) "12"
    127.0.0.1:6379> sinter s1 s2//取交集
    1) "13"
    2) "14"
    127.0.0.1:6379> sdiff s2 s1// s2 - s1 == s2 - (s2 交 s1)
    1) "15"
    2) "16"
    3) "17"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    #####Sortedset有序集合类型

    因为它是有序的,通过score进行排序,添加的时候也要加上

    zadd key score value 添加

    zrange key start temp:查

    zrem key value 删

    zrange key start temp withscores;连带排序分数一起查

    127.0.0.1:6379> zadd sort 60 kobe
    (integer) 1
    127.0.0.1:6379> zadd sort 89 lily
    (integer) 1
    127.0.0.1:6379> zadd sort 88 miiler
    (integer) 1
    127.0.0.1:6379> zadd sort 34 durant
    (integer) 1
    127.0.0.1:6379> zrange sort 0 -1
    1) "durant"
    2) "kobe"
    3) "miiler"
    4) "lily"
    127.0.0.1:6379> zrange sort 0 -1 withscores
    1) "durant"
    2) "34"
    3) "kobe"
    4) "60"
    5) "miiler"
    6) "88"
    7) "lily"
    8) "89"
    127.0.0.1:6379> zrem sort lily
    (integer) 1
    127.0.0.1:6379> zrange sort 0 -1
    1) "durant"
    2) "kobe"
    3) "miiler"
    127.0.0.1:6379>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    二,redis和Java结合

    使用springIOC容器管理redis所需要的类

    2.1 搭建环境

    2.1.3 引入依赖

    <dependencies>
       <dependency>
           <groupId>redis.clients</groupId>
           <artifactId>jedis</artifactId>
           <version>2.7.0</version>
       </dependency>
    
       <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
    
       <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-pool2</artifactId>
           <version>2.4.2</version>
       </dependency>
    
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.13.2</version>
           <scope>test</scope>
       </dependency>
    
    
       <!-- 1.Spring核心依赖 -->
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-core</artifactId>
           <version>4.3.7.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-beans</artifactId>
           <version>4.3.7.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>4.3.7.RELEASE</version>
       </dependency>
    
    
    
    
    </dependencies>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    2.1.2 配置文件:applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--    开启包扫描-->
       <context:component-scan base-package="com.cq"></context:component-scan>
    
    <!--    配置JedisPoolConfig-->
       <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
           <property name="maxTotal" value="100"></property>
           <property name="maxIdle" value="100"></property>
           <property name="minIdle" value="10"></property>
           <property name="maxWaitMillis" value="20000"></property>
       </bean>
    <!--    配置JedisPool-->
       <bean id="pool" class="redis.clients.jedis.JedisPool">
           <constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
           <constructor-arg name="host" value="localhost"></constructor-arg>
       </bean>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    2.2 测试

    package com.cq;
    
    import com.cq.utils.JedisUtils;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    
    /**
    * @BelongsProject: day0618-redis
    * @BelongsPackage: com.cq
    * @Author: CodeArts
    * @CreateTime: 2022-06-18  15:01
    * @Description: TODO
    * @Version: 1.0
    */
    public class RedisTest {
       @Test
       public void test02(){
          ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
           JedisPool pool = context.getBean("pool", JedisPool.class);
           Jedis resource = pool.getResource();
           resource.set("user","wang");
           String user = resource.get("user");
           System.out.println(user);
           resource.close();
    
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    三,redis的综合项目SSM

    通过连接数据库实现页面的下拉框

    分析:前端需要接收json串然后进行数据的渲染,所以后端传给前端也要是个json串,因为这里用了redis,要先判断缓存中是否有数据有者直接返回,没有查数据库,再把数据写到缓存中,下次直接查缓存即可

    在这里插入图片描述

    1 用骨架创建web项目

    2 引入依赖

    <properties>
     <spring.version>5.0.2.RELEASE</spring.version>
     <slf4j.version>1.6.6</slf4j.version>
     <log4j.version>1.2.12</log4j.version>
     <oracle.version>11.2.0.1.0</oracle.version>
     <mybatis.version>3.4.5</mybatis.version>
     <spring.security.version>5.0.1.RELEASE</spring.security.version>
     <mysql.version>5.1.6</mysql.version>
    </properties>
    
    <dependencies>
     <!-- spring -->
     <dependency>
       <groupId>org.aspectj</groupId>
       <artifactId>aspectjweaver</artifactId>
       <version>1.6.8</version>
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-aop</artifactId>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context-support</artifactId>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-web</artifactId>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-orm</artifactId>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-beans</artifactId>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-core</artifactId>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-test</artifactId>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-webmvc</artifactId>
       <version>${spring.version}</version>
    
     </dependency>
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-tx</artifactId>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.12</version>
       <scope>test</scope>
     </dependency>
    
     <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
       <version>3.1.0</version>
       <scope>provided</scope>
     </dependency>
     <dependency>
       <groupId>javax.servlet.jsp</groupId>
       <artifactId>jsp-api</artifactId>
       <version>2.0</version>
       <scope>provided</scope>
     </dependency>
     <dependency>
       <groupId>jstl</groupId>
       <artifactId>jstl</artifactId>
       <version>1.2</version>
     </dependency>
     <!-- log start -->
     <dependency>
       <groupId>log4j</groupId>
       <artifactId>log4j</artifactId>
       <version>${log4j.version}</version>
     </dependency>
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>${slf4j.version}</version>
     </dependency>
     <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <version>${slf4j.version}</version>
     </dependency>
     <!-- log end -->
    
     <dependency>
       <groupId>org.mybatis</groupId>
       <artifactId>mybatis</artifactId>
       <version>${mybatis.version}</version>
     </dependency>
     <dependency>
       <groupId>org.mybatis</groupId>
       <artifactId>mybatis-spring</artifactId>
       <version>1.3.0</version>
     </dependency>
     <dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>druid</artifactId>
       <version>1.1.10</version>
     </dependency>
    
     <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>${mysql.version}</version>
     </dependency>
    <!--redis-->
     <dependency>
       <groupId>redis.clients</groupId>
       <artifactId>jedis</artifactId>
       <version>2.9.0</version>
     </dependency>
    
     <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-test</artifactId>
       <version>${spring.version}</version>
     </dependency>
    
     <!--json工具-->
     <dependency>
       <groupId>com.fasterxml.jackson.core</groupId>
       <artifactId>jackson-databind</artifactId>
       <version>2.9.0</version>
     </dependency>
    
     <dependency>
       <groupId>com.fasterxml.jackson.core</groupId>
       <artifactId>jackson-core</artifactId>
       <version>2.9.0</version>
     </dependency>
    
     <dependency>
       <groupId>com.fasterxml.jackson.core</groupId>
       <artifactId>jackson-annotations</artifactId>
       <version>2.9.0</version>
     </dependency>
    
     <dependency>
       <groupId>junit</groupId>
       <artifactId>junit</artifactId>
       <version>4.13</version>
       <scope>test</scope>
     </dependency>
    </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170

    3 创建ssm结构

    在这里插入图片描述

    ###4 配置文件

    ####4.1 applicationContext.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--    开启包扫描-->
       <context:component-scan base-package="com.cq"></context:component-scan>
    <!--    加载外部的propertier文件-->
       <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <!--    配置数据源-->
       <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
           <property name="driverClassName" value="${jdbc.driver}"></property>
           <property name="url"     value="${jdbc.url}"></property>
           <property name="username" value="${jdbc.username}"></property>
           <property name="password" value="${jdbc.password}"></property>
       </bean>
    <!--    配置sqlSessionFactoryBean-->
       <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
           <property name="dataSource" ref="dataSource"></property>
       </bean>
    <!--    配置MapperScannerConfigure-->
       <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
           <property name="basePackage" value="com.cq.dao"></property>
       </bean>
       
    <!--    配置redis-->
    <!--    配置JedisPoolConfig-->
       <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
           <property name="maxTotal" value="${redis.pool.maxActive}"></property>
           <property name="maxIdle" value="${redis.pool.maxIdle}"></property>
           <property name="minIdle" value="${redis.pool.minIdle}"></property>
           <property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}"></property>
       </bean>
    
    <!--    配置JedisPool-->
       <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
           <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
           <constructor-arg name="host" value="${redis.host}"></constructor-arg>
       </bean>
    <!--    配置平台事务管理器-->
       <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
           <property name="dataSource" ref="dataSource"></property>
       </bean>
    <!--开启注解对事务的支持-->
       <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    4.2 db.properties

    # mysql的配置信息
    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3309/day0618
    jdbc.username=root
    jdbc.password=root
    
    # redis的配置信息
    # 最大分配的对象数
    redis.pool.maxActive=200
    # 最大能够保持idel状态的对象数
    redis.pool.maxIdle=50
    redis.pool.minIdle=10
    redis.pool.maxWaitMillis=20000
    
    #  连接redis的主机和端口号
    redis.host = 127.0.0.1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    4.3 日志文件log4j.properties

    log4j.rootLogger=DEBUG,myConsole
    log4j.appender.myConsole=org.apache.log4j.ConsoleAppender
    log4j.appender.myConsole.ImmediateFlush=true
    log4j.appender.myConsole.Target=System.out
    log4j.appender.myConsole.layout=org.apache.log4j.PatternLayout
    log4j.appender.myConsole.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %n
    
    log4j.logger.com.mchange.v2=ERROR
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4.4 springmvc.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
          xmlns:context="http://www.springframework.org/schema/context"
          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描包-->
       <context:component-scan base-package="com.cq"></context:component-scan>
       <!--配置视图解析器-->
       <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"></property>
        <property name="suffix" value=".jsp"></property>
       </bean>
    
    
    <!--    开启对处理器映射器,处理器适配器的支持-->
       <mvc:annotation-driven></mvc:annotation-driven>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5 代码的编写

    5.1 实体 City.java

    package com.cq.pojo;
    
    /**
    * @BelongsProject: day0618-ssm-redis
    * @BelongsPackage: com.cq.pojo
    * @Author: CodeArts
    * @CreateTime: 2022-06-18  15:44
    * @Description: TODO
    * @Version: 1.0
    */
    public class City {
       private Integer id;
       private String name;
    
       public City() {
       }
    
       public City(Integer id, String name) {
           this.id = id;
           this.name = name;
       }
    
       public Integer getId() {
           return id;
       }
    
       public void setId(Integer id) {
           this.id = id;
       }
    
       public String getName() {
           return name;
       }
    
       public void setName(String name) {
           this.name = name;
       }
    
       @Override
       public String toString() {
           return "CityDao{" +
                   "id=" + id +
                   ", name='" + name + '\'' +
                   '}';
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    5.2 控制层CityController.java

    package com.cq.controller;
    
    import com.cq.pojo.City;
    import com.cq.service.CityService;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletResponse;
    import java.util.List;
    
    /**
    * @BelongsProject: day0618-ssm-redis
    * @BelongsPackage: com.cq.controller
    * @Author: CodeArts
    * @CreateTime: 2022-06-18  15:44
    * @Description: TODO
    * @Version: 1.0
    */
    @Controller
    @RequestMapping("city")
    public class CityController {
       @Autowired
       CityService cityService;
       @RequestMapping("findAll")
       public void findAll(HttpServletResponse response) throws Exception {
          response.setContentType("application/json;charset=utf-8");
           String string = cityService.findAll();
    //        System.out.println(string);
           response.getWriter().write(string);
    
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    5.4 service包下的CityService.java接口

    package com.cq.service;
    
    import com.cq.pojo.City;
    
    import java.util.List;
    
    /**
    * @BelongsProject: day0618-ssm-redis
    * @BelongsPackage: com.cq.service
    * @Author: CodeArts
    * @CreateTime: 2022-06-18  15:43
    * @Description: TODO
    * @Version: 1.0
    */
    public interface CityService {
       String findAll();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    5.5 service包下的impl中改接口的实现类CityServiceImpl.java

    package com.cq.service.impl;
    
    import com.cq.dao.CityDao;
    import com.cq.pojo.City;
    import com.cq.service.CityService;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import org.springframework.util.StringUtils;
    import redis.clients.jedis.Jedis;
    import redis.clients.jedis.JedisPool;
    
    import java.util.List;
    
    /**
    * @BelongsProject: day0618-ssm-redis
    * @BelongsPackage: com.cq.service.impl
    * @Author: CodeArts
    * @CreateTime: 2022-06-18  15:50
    * @Description: TODO
    * @Version: 1.0
    */
    @Service
    public class CityServiceImpl implements CityService {
       @Autowired
       CityDao cityDao;
       @Autowired
       JedisPool jedisPool;
       public String findAll() {
           Jedis resource = jedisPool.getResource();//获取缓存连接,前提是开启了redis
           String city_name = resource.get("city_name");
           if(StringUtils.isEmpty(city_name)){//缓存中不存在
               ObjectMapper mapper = new ObjectMapper();
               try {
                   //转成json串
                   city_name = mapper.writeValueAsString(cityDao.findAll());
               } catch (JsonProcessingException e) {
                   e.printStackTrace();
               }
               //放入缓存
               resource.set("city_name",city_name);
               return city_name;
    
    
           }else{
               System.out.println("缓存中取");
               return city_name;
           }
    
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    5.6 CityDao.java

    package com.cq.dao;
    
    import com.cq.pojo.City;
    import org.apache.ibatis.annotations.Select;
    import org.springframework.stereotype.Repository;
    
    import javax.annotation.Resources;
    import java.util.List;
    
    /**
    * @BelongsProject: day0618-ssm-redis
    * @BelongsPackage: com.cq.dao
    * @Author: CodeArts
    * @CreateTime: 2022-06-18  15:43
    * @Description: TODO
    * @Version: 1.0
    */
    @Repository
    public interface CityDao {
       @Select("select * from city")
       public List<City> findAll();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    5.7 前端页面

    <%--
     Created by IntelliJ IDEA.
     User: 86186
     Date: 2022/6/18
     Time: 16:23
     To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
    <html>
    <head>
       <title>Title</title>
    </head>
    <body>
    <select name="province" id="province">
       <option>请选择</option>
    </select>
    
    </body>
    <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
    <script type="text/javascript">
       $(function () {
           $.ajax({
               type:"GET",
               url:"${pageContext.request.contextPath}/city/findAll",
               contentType:"application/json;charset=UTF-8",
               dataType:"json",
               success:function (data) {
                   $(data).each(function () {
                       var option = "<option id="+this.id+">"+this.name + "</option>";
                       $("#province").append(option);
    
                   })
    
               }
           })
       })
    </script>
    </html>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    6 web.xml配置

    <!DOCTYPE web-app PUBLIC
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app>
     <display-name>Archetype Created Web Application</display-name>
    <!-- 加载spring配置文件路径-->
     <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>classpath:applicationContext.xml</param-value>
     </context-param>
    
     <!-- 解决中文乱码过滤器 -->
     <filter>
       <filter-name>characterEncodingFilter</filter-name>
       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
       <init-param>
         <param-name>encoding</param-name>
         <param-value>UTF-8</param-value>
       </init-param>
     </filter>
    
    
     <filter-mapping>
       <filter-name>characterEncodingFilter</filter-name>
       <url-pattern>/*</url-pattern>
     </filter-mapping>
     <!--  配置监听器-->
     <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>
    
     <!--配置前端控制器-->
     <servlet>
       <servlet-name>dispatcherServlet</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <!--加载springmvc配置文件-->
       <init-param>
         <param-name>contextConfigLocation</param-name>
         <param-value>classpath:springmvc.xml</param-value>
       </init-param>
     </servlet>
     <servlet-mapping>
       <servlet-name>dispatcherServlet</servlet-name>
       <url-pattern>/</url-pattern>
     </servlet-mapping>
    </web-app>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    效果图

    在这里插入图片描述

  • 相关阅读:
    鸿蒙HarmonyOS实战-Web组件(请求响应和页面调试)
    第1章、HDFS_核心参数
    ubuntu 修改磁盘名称
    Docker学习系列3:常用命令之容器命令
    分析和比较深度学习框架 PyTorch 和 Tensorflow
    DOM元素可编辑自定义样式获取编辑后的数据
    使用antv/X6实现流程图(包括线条动画流动,路径标签,悬浮窗口等)快速搭建流程图,DAG图等初始实践记录
    健身器材销售网站的设计与实现(SSH)
    [SpringBoot] SpringBoot-04-读取yaml配置文件
    Python接口自动化搭建过程,含request请求封装
  • 原文地址:https://blog.csdn.net/qq_43728862/article/details/125360175