SSM整合Redis

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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--springMVC主配置文件-->
<!--    配置扫描controller路径-->
    <context:component-scan base-package="com.controller"></context:component-scan>
    <!--启用MVC注解功能,增加了下面的<mvc:resources ></mvc:resources>必须使用,否则不能访问spring
    控制器controller-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <mvc:default-servlet-handler></mvc:default-servlet-handler>

<!--    配置视图页面解析-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          id="internalResourceViewResolver">
<!--        前缀-->
        <property name="prefix" value="/"></property>
<!--        后缀-->
        <property name="suffix" value=".html" ></property>
    </bean>
    <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
<!--    <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>-->
</beans>

Spring 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">
<!--spring主配置文件-->
<!--自动扫描包 com.service 为包名 也可以为com.*-->
    <context:component-scan base-package="com.service"></context:component-scan>
<!--    包含mybatis主配置文件-->
    <import resource="mybatis.xml"></import>
<!--    <import resource="redis.xml"></import>-->
</beans>

Mbatis+Redis 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">
    <!--mybatis主配置文件-->
    <!--    引入配置文件-->
    <bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:jdbc.properties"></property>
    </bean>
    <!--    配置数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
        <!--        初始化连接大小-->
        <property name="initialSize" value="${initialSize}"></property>
        <!--        连接池最大数量-->
        <property name="maxActive" value="${maxActive}"></property>
        <!--        连接池最大空闲-->
        <property name="maxIdle" value="${maxIdle}"></property>
        <!--        连接池最小空闲-->
        <property name="minIdle" value="${minIdle}"></property>
        <!--        获取链接最大等待时间-->
        <property name="maxWait" value="${maxWait}"></property>
    </bean>
    <!--    spring和Mybatis完美整合,不需要mybatis的映射文件-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <property name="dataSource" ref="dataSource"></property>
        <!--        自动扫描mapperxml文件夹  类似于mybatis中主配置文件Mapper.xml文件 中的mappers-->
        <property name="mapperLocations" value="classpath:mapperxml/*.xml"></property>
    </bean>
    <!-- redis连接池配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxTotal" value="300"></property>
        <property name="maxIdle" value="200"/>
        <property name="maxWaitMillis" value="1000"/>
    </bean>
    <!-- redis连接工厂 -->
    <bean class="redis.clients.jedis.JedisPool" id="jedisPool">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
        <constructor-arg name="host" value="127.0.0.1"></constructor-arg>
        <constructor-arg name="port" value="6379"></constructor-arg>
        <constructor-arg name="password" value="root"></constructor-arg>
        <constructor-arg name="timeout" value="1000"></constructor-arg>
    </bean>
    <!--    DAO接口所在包名,Spring会自动查找其下的接口类-->
    <!--    <import resource="redis.xml"></import>-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao"></property>
    </bean>
</beans>

jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/demo
username=root
password=root
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000

service层

package com.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.entity.Classinfo;
import com.dao.ClassinfoMapper;
import com.service.ClassinfoService;
import redis.clients.jedis.JedisPool;
import java.util.List;

@Service
public class ClassinfoServiceImpl implements ClassinfoService {

    @Resource
    private ClassinfoMapper classinfoMapper;
    @Resource
    private JedisPool jedisPool;

    @Override

    public int deleteByPrimaryKey(Integer classid) {
        return classinfoMapper.deleteByPrimaryKey(classid);
    }

    @Override
    public int insert(Classinfo record) {
        return classinfoMapper.insert(record);
    }

    @Override
    public int insertSelective(Classinfo record) {
        return classinfoMapper.insertSelective(record);
    }

    @Override
    public Classinfo selectByPrimaryKey(Integer classid) {
        return classinfoMapper.selectByPrimaryKey(classid);
    }

    @Override
    public int updateByPrimaryKeySelective(Classinfo record) {
        return classinfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Classinfo record) {
        return classinfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public String findAll() {
        jedisPool.getResource().select(3);
        String json = jedisPool.getResource().get("findAll");
        if (json == null) {
            List<Classinfo> lists = classinfoMapper.findAll();
            System.out.println(lists);
            json = JSONObject.toJSONString(lists);
            jedisPool.getResource().select(3);
            jedisPool.getResource().set("findAll",json);
        }
        return json;
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值