Springboot +Spring data+oracle+redis 框架整合

新建项目

1.打开idea File->New->Project
Springboot +Spring data+oracle+redis 框架整合_第1张图片2.直接Next
Springboot +Spring data+oracle+redis 框架整合_第2张图片
3.Group填写 域名组织 Articfact 填写项目 ,具体详情参考的这篇文章:链接Springboot +Spring data+oracle+redis 框架整合_第3张图片4.然后配置你需要你的,然后Next-finished
Springboot +Spring data+oracle+redis 框架整合_第4张图片Springboot +Spring data+oracle+redis 框架整合_第5张图片5.然后创建一些常用的文件夹,注意SpringBoot 启动文件 默认扫描平级文件夹及子文件夹下的文件 ,所以具体结构如下:
Springboot +Spring data+oracle+redis 框架整合_第6张图片
7.然后修改一下pom文件,导入我们需要相关整合需要的jar包,注意Oracle驱动依赖的导入请尽量保持与所用Oracle数据库版本保持一致,否则会出现相关错误。

		
            org.springframework.boot
            spring-boot-starter-data-redis
        

        
            com.oracle
            ojdbc6
            11.2.0.3
        

8.修改配置文件整合Oracle数据库和redis ,具体相关参数意义及作用,请大家踊跃去搜索。

# oracle setting
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=数据库用户名
spring.datasource.password=数据库密码
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
#jpa setting
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
server.port = 9000
#redis setting
spring.redis.jedis.pool.max-idle=10
spring.redis.jedis.pool.min-idle=5
spring.redis.pool.max-total=20
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.hostName=127.0.0.1
spring.redis.port=6379
spring.redis.timeout=5000

9.写个功能测试一下,结构如下
Springboot +Spring data+oracle+redis 框架整合_第7张图片
9.1新建一张表,并插入数据

create table STUDENT
(
  stunum  NUMBER(4) not null,
  age     NUMBER(4),
  stuname VARCHAR2(9)
);
insert into student (STUNUM, AGE, STUNAME)
values (1, 30, 'lisi');

insert into student (STUNUM, AGE, STUNAME)
values (2, 31, 'zhangsan');

9.2 新建student文件

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "STUDENT")
public class Student {
    @Id
    @GeneratedValue
    private Integer stunum;
    private Integer age;
    private String stuname;
    public Student() {
    }
    public Student(Integer stunum, Integer age, String stuname) {
        this.stunum = stunum;
        this.age = age;
        this.stuname = stuname;
    }
    public Integer getStunum() {
        return stunum;
    }
    public void setStunum(Integer stunum) {
        this.stunum = stunum;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public String getStuname() {
        return stuname;
    }
    public void setStuname(String stuname) {
        this.stuname = stuname;
    }
}

9.3 新建StudentRepository文件

port com.zechuang.dataupload.domain.Student;
import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepository  extends JpaRepository {
}

9.4 新建StudentControlle文件

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zechuang.dataupload.dao.StudentRepository;
import com.zechuang.dataupload.domain.Student;
import com.zechuang.dataupload.util.HttpRequestUtil;
import com.zechuang.dataupload.util.HttpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudnetController {

    @Autowired
    public StringRedisTemplate gStringRedisTemplate; //注入redis组件
    @Autowired
    private StudentRepository studentRepository; 
    @RequestMapping("/{id}")
    public String findById(@PathVariable Integer id ){
        //先查redis
        String st=null;
        System.out.println(id);
        String json=gStringRedisTemplate.opsForValue().get("user"+id);

        if (json==null||"".equals(json)){
            //如果没有再查数据库
            st=studentRepository.findById(id).get().getStuname();
            gStringRedisTemplate.opsForValue().set("user"+id,st);
        }else {
           //如果查出来直接返回
            return  json;
        }
        return st;
    }
    @RequestMapping("findAll")
    public List findAll(){
        //查询全部
        List st=studentRepository.findAll();
        return st;
    }
   
}

10.执行一下
Springboot +Spring data+oracle+redis 框架整合_第8张图片Springboot +Spring data+oracle+redis 框架整合_第9张图片
又执行了另外一个接口发现报错了:

2019-10-12 13:45:07.859 ERROR 19172 --- [nio-9000-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379] with root cause

java.net.ConnectException: Connection refused: no further information
	at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_151]
	at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_151]
	at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:327) ~[netty-transport-4.1.39.Final.jar:4.1.39.Final]
	at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:336) ~[netty-transport-4.1.39.Final.jar:4.1.39.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:685) ~[netty-transport-4.1.39.Final.jar:4.1.39.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:632) ~[netty-transport-4.1.39.Final.jar:4.1.39.Final]
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:549) ~[netty-transport-4.1.39.Final.jar:4.1.39.Final]
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:511) ~[netty-transport-4.1.39.Final.jar:4.1.39.Final]
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918) ~[netty-common-4.1.39.Final.jar:4.1.39.Final]
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.39.Final.jar:4.1.39.Final]
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.39.Final.jar:4.1.39.Final]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]

最后发现是redis服务没启动 ,我们手动启动一下
Springboot +Spring data+oracle+redis 框架整合_第10张图片
再试一下:
Springboot +Spring data+oracle+redis 框架整合_第11张图片
当然还可以测试一下 redis缓存效果,此处就不再展示了,以上就是我参考诸位大佬的文章加自己理解整合的结果,如有侵犯还望包涵,谢谢!

你可能感兴趣的:(新技术学习,Spring,boot,springdata,redis)