Java Spring代码方式和XML配置文件方式配置Redis

1、pom.xml配置

<dependency>
   <groupId>redis.clients</groupId>
   <artifactId>jedis</artifactId>
   <version>2.9.0</version>
</dependency>
<dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-redis</artifactId>
   <version>1.8.23.RELEASE</version>
</dependency>

2、xml配置文件实现redis的配置

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/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
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- redis连接池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="${redis.maxTotal}"></property>
        <property name="maxIdle" value="${redis.maxIdle}"/>
        <property name="maxWaitMillis" value="${redis.maxWait}"/>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"/>
        <property name="testOnReturn" value="${redis.testOnReturn}"></property>
    </bean>

    <!-- redis连接工厂 -->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="poolConfig" ref="jedisPoolConfig"/>

        <property name="hostName" value="${redis.host}"/>
        <property name="port" value="${redis.port}"/>
        <property name="password" value="${redis.password}"/>
        <property name="timeout" value="${redis.timeout}"/>
    </bean>

    <!--  注册StringRedisTemplate  -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>

        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
        </property>

        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
        <property name="hashValueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>
        </property>
    </bean>

    <bean id="redisUtil" class="com.RedisUtils">
        <property name="redisTemplate" ref="redisTemplate"/>
    </bean>

</beans>

3、java代码方式实现redis的配置

在spring的配置类中,加上如下三个类。

    /**
     * redis连接池配置
     */
    @Bean
    public JedisPoolConfig jedisPoolConfig() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        // 链接池中最大空闲的连接数,默认为8
        jedisPoolConfig.setMaxTotal(300);
        // 连接池中最少空闲的连接数,默认为0.
        jedisPoolConfig.setMaxIdle(200);
        // 当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时.
        jedisPoolConfig.setMaxWaitMillis(1000);
        return jedisPoolConfig;
    }

    /**
     * redis连接工厂
     */
    @Bean
    public JedisConnectionFactory connectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();

        jedisConnectionFactory.setPoolConfig(this.jedisPoolConfig());
        jedisConnectionFactory.setHostName(localhost);
        jedisConnectionFactory.setPort(6379);
        jedisConnectionFactory.setPassword(123456);
        jedisConnectionFactory.setTimeout(10000);

        return jedisConnectionFactory;
    }

    /**
     * 注册StringRedisTemplate
     */
    @Bean
    public StringRedisTemplate redisTemplate() {
        StringRedisTemplate redisTemplate = new StringRedisTemplate();
        redisTemplate.setConnectionFactory(this.connectionFactory());

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());
        return redisTemplate;
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1)代码生成主要依赖于freeMark模板,不同的项目需求可以通过修改freeMark模板来实现。 2)如果是后台管理系统,则可以生成管理系统基本的网站页面及其功能。其他系统则很难生成前端页面,主要困难在于模板不统一,如果页面风格都相似,也可以用模板生成页面,然后再对页面进行修改。 3)项目基本功能:根据数据库表生成基本功能代码,包含Mybatis文件,dao、servic、entity、controller以及查询页面、增加编辑页面。 4)生成过程中可以选择编辑页面所需要的字段,列表页面所需要的字段以及查询条件所需要的字段。 # 数据库 1)数据库文件在项目根目录下的doc文件夹下xcode.sql文件 2)创建数据库xCode 默认设置root账户、密码root123 3)执行xcode.sql文件的SQL,创建数据表结构即可 # 项目结构 1)项目根目录下的doc文件夹放置的是开发相关的文档 2) pom.xml 文件是maven相关配置文件 3)src.main 包下有三个文件夹,Java文件夹很明显,是Java文件相关。 resources文件夹是 配置相关的文件夹,包括spring相关配置,Mybatis相关配置,数据库相关配置redis相关配 置都在此文件夹下,webapp文件夹下是页面相关的 4)com.cn.cooxin包,admin包主要是管理代码生成后台功能的文件,包含用户的管理、角色菜单管理,代码生成管理等,code包主要是代码生成相关的功能,common包是公共服务相关的功能,ueditor是百度编辑器相关的功能,如果不用,可以不用管。util包是开发工具类相关的功能。
项目描述 说明: spring security 全注解式的权限管理 动态配置权限,角色和资源,权限控制到按钮粒度 采用token进行权限校验,禁用session,未登录返回401,权限不足返回403 采用redis存储token及权限信息 内置功能: 用户管理:用户查询、添加用户、修改用户、给用户分配角色 菜单管理:菜单列表、添加菜单、修改菜单、删除菜单、权限配置、菜单图标设置、菜单排序 角色管理:角色查询、添加角色、修改角色、删除角色 代码生成:根据表名生成bean、controller、dao、Mapper.xml、列表页、搜索、分页、新增页、修改页 job集群:创建job、取消job、查询job、下拉搜索spring bean 数据源监控:druid 接口swagger文档 日志查询 邮件管理:发送邮件、搜索邮件 文件管理:上传文件、文件列表、文件删除 公告管理:公告未读提醒、发布公告、查询公告、公告阅读人列表 excel下载:自定义sql导出excel、也可在页面展示sql结果数据 字典管理:一些常量字典的维护 个人信息修改 修改密码 头像修改 其他说明: 日志模块 sl4j日志分包:将sql日志、业务日志、异常日志进行了分离,更方便定位问题 日志表:使用aop拦截实现 权限控制:基于token方式,禁用session 对各种不同异常进行了全局统一处理 使用lombok简化java代码,让源码更简洁,可读性高 mybatis未进行二次封装,原滋原味,简单sql采用注解,复杂sql采用Mapper.xml配置 使用了layui的弹出层、菜单、文件上传、富文本编辑、日历、选项卡、数据表格等 表单数据采用bootstrapValidator校验,简单快捷方便 运行环境 jdk8+mysql+redis+IntelliJ IDEA+maven 项目技术(必填) Springboot+Mybatis+ SpringMvc+springsecrity+Redis+bootstrap+jquery 数据库文件 压缩包内 jar包文件 maven搭建
Spring Redis是一个用于在Java应用中操作Redis数据库的框架。它提供了一种方便的方式来集成和使用Redis,使得开发人员可以更轻松地使用Redis的各种功能。 在使用Spring Redis时,我们首先需要添加相关的依赖到项目的pom.xml文件中。其中包括spring-boot-starter-data-redis和Lettuce(或Jedis)等必要的依赖。 接下来,在Spring配置文件中进行相应的配置。首先,我们需要配置Redis连接工厂,指定Redis服务器的地址和端口号。可以通过RedisStandaloneConfiguration类来实现这一功能。 然后,我们需要配置RedisTemplate,这个模板类是Spring Redis的核心,用于执行Redis的各种操作,如存储、读取、删除等。我们可以使用StringRedisTemplate或RedisTemplate类来创建模板对象,并将Redis连接工厂设置给它。 在配置RedisTemplate之后,我们还可以配置其他的一些属性,例如序列化方式、连接池配置等。这些属性可以通过RedisStandaloneConfiguration类的setter方法来设置。 最后,在需要使用Redis的地方,我们可以通过@Autowired注解将RedisTemplate注入到对应的类中,然后就可以使用RedisTemplate来执行各种Redis操作了。 总的来说,Spring RedisJava配置主要包括添加相关的依赖、配置Redis连接工厂、配置RedisTemplate以及使用RedisTemplate执行操作等步骤。通过这些配置,我们可以方便地在Java应用中使用Redis数据库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值