java中的接口是类吗
461
2022-11-21
Java中的使用及连接Redis数据库(附源码)
引言:
本文主要分享了Redis如何在IDEA中部署,运行;模拟加入Redis的操作;
目录结构
1. 在IDEA中搭建Redis
1.1 创建项目
新建含有web的SpringBoot项目;
搭建项目参考:SpringBoot超详细笔记:https://blog.csdn.net/weixin_42601136/article/details/108396511
1.2 添加依赖(不够手动添加)
1.3 测试环境是否搭建成功
@Test
public void test0() {
//连接Redis
Jedis jedis = new Jedis("127.0.0.1", 6379);
//获取kak
String kaka = jedis.get("kak");
log.info(kaka);
//创建datex
String datex = jedis.set("datex", new Date().toString());
//获取datex
String datex1 = jedis.get("datex");
log.info(datex1);
//关闭资源
jedis.close();
}
1.4 利用json存储数据
@Test
public void test1() {
JedisPool pool = new JedisPool("127.0.0.1", 6379);
Jedis jedis = pool.getResource();
Student student = new Student(1001, "kak", "man", "20");
String key = "student";
String field = "1001";
String s = JsonUtils.objectToJson(student);
String value = s;
jedis.hset(key, field, value);
String hget = jedis.hget(key, field);
log.info(hget);
jedis.close();
}
1.5 以byte的形式存储对象
@Test
public void test02() {
//连接redis
Jedis jedis = new Jedis("127.0.0.1", 6379);
String key = "Student";
Student value = new Student(1002, "taotao", "woman", "20");
//将key和value转换为byte[]
byte[] byteKey = SerializationUtils.serialize(key);
byte[] byteValue = SerializationUtils.serialize(value);
//将key和value存储到redis中
jedis.set(byteKey, byteValue);
//获取value值
byte[] bytes = jedis.get(byteKey);
//bytes反序列化为Student对象
Student stu = (Student) SerializationUtils.deserialize(bytes);
System.out.println(stu);
jedis.close();
}
1.6 加入连接池的操作
使用连接池操作,可以避免频繁的创建和销毁连接对象的消耗资源
@Test
public void test3(){
//创建连接池
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(100);//连接池中的最大活跃数
poolConfig.setMaxIdle(10);//最大空闲数
poolConfig.setMinIdle(5);//最小空闲数
poolConfig.setMaxWaitMillis(3000);//连接池空了,3000毫秒后没有获取Jedis对象,超时
//创建连接池
JedisPool pool = new JedisPool(poolConfig,"127.0.0.1",6379);
//通过连接池获取Jedis对象
Jedis resource = pool.getResource();
//获取上面添加的datex
String value = resource.get("datex");
log.info(value);
resource.close();
}
1.7 管道操作
Redis的管道操作,执行一个命令需要先发送请求到Redis,需要经历网络延迟,Redis还需给客户端一个响应;如果需要一次操作多个命令,可以通过管道,将命令放到客户端的一个Pipeline中,之后一次将命令发送到服务端,服务端一次性返回到客户端;
@Test
public void test3(){
//创建连接池
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(100);//连接池中的最大活跃数
poolConfig.setMaxIdle(10);//最大空闲数
poolConfig.setMinIdle(5);//最小空闲数
poolConfig.setMaxWaitMillis(3000);//连接池空了,3000毫秒后没有获取Jedis对象,超时
//创建连接池
JedisPool pool = new JedisPool(poolConfig,"127.0.0.1",6379);
//通过连接池获取Jedis对象
Jedis resource = pool.getResource();
//获取上面添加的datex
String value = resource.get("datex");
log.info(value);
resource.close();
}
2. 从数据中查询数据
场景:
将数据存入Redis中,避免访问量过大,造成数据库奔溃;
2.1 生成实体类
@Test
public void test3(){
//创建连接池
GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
poolConfig.setMaxTotal(100);//连接池中的最大活跃数
poolConfig.setMaxIdle(10);//最大空闲数
poolConfig.setMinIdle(5);//最小空闲数
poolConfig.setMaxWaitMillis(3000);//连接池空了,3000毫秒后没有获取Jedis对象,超时
//创建连接池
JedisPool pool = new JedisPool(poolConfig,"127.0.0.1",6379);
//通过连接池获取Jedis对象
Jedis resource = pool.getResource();
//获取上面添加的datex
String value = resource.get("datex");
log.info(value);
resource.close();
}
2.2 生成实体模板类
package com.sx.kak.po;
import java.util.ArrayList;
import java.util.List;
public class StudentExample {
protected String orderByClause;
protected boolean distinct;
protected List
public StudentExample() {
oredCriteria = new ArrayList
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public String getOrderByClause() {
return orderByClause;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List
return oredCriteria;
}
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
protected abstract static class GeneratedCriteria {
protected List
protected GeneratedCriteria() {
super();
criteria = new ArrayList
}
public boolean isValid() {
return criteria.size() > 0;
}
public List
return criteria;
}
public List
return criteria;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}
public Criteria andIdIsNull() {
addCriterion("id is null");
return (Criteria) this;
}
public Criteria andIdIsNotNull() {
addCriterion("id is not null");
return (Criteria) this;
}
public Criteria andIdEqualTo(Integer value) {
addCriterion("id =", value, "id");
return (Criteria) this;
}
public Criteria andIdNotEqualTo(Integer value) {
addCriterion("id <>", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThan(Integer value) {
addCriterion("id >", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThanOrEqualTo(Integer value) {
addCriterion("id >=", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThan(Integer value) {
addCriterion("id <", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThanOrEqualTo(Integer value) {
addCriterion("id <=", value, "id");
return (Criteria) this;
}
public Criteria andIdIn(List
addCriterion("id in", values, "id");
return (Criteria) this;
}
public Criteria andIdNotIn(List
addCriterion("id not in", values, "id");
return (Criteria) this;
}
public Criteria andIdBetween(Integer value1, Integer value2) {
addCriterion("id between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andIdNotBetween(Integer value1, Integer value2) {
addCriterion("id not between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andNameIsNull() {
addCriterion("NAME is null");
return (Criteria) this;
}
public Criteria andNameIsNotNull() {
addCriterion("NAME is not null");
return (Criteria) this;
}
public Criteria andNameEqualTo(String value) {
addCriterion("NAME =", value, "name");
return (Criteria) this;
}
public Criteria andNameNotEqualTo(String value) {
addCriterion("NAME <>", value, "name");
return (Criteria) this;
}
public Criteria andNameGreaterThan(String value) {
addCriterion("NAME >", value, "name");
return (Criteria) this;
}
public Criteria andNameGreaterThanOrEqualTo(String value) {
addCriterion("NAME >=", value, "name");
return (Criteria) this;
}
public Criteria andNameLessThan(String value) {
addCriterion("NAME <", value, "name");
return (Criteria) this;
}
public Criteria andNameLessThanOrEqualTo(String value) {
addCriterion("NAME <=", value, "name");
return (Criteria) this;
}
public Criteria andNameLike(String value) {
addCriterion("NAME like", value, "name");
return (Criteria) this;
}
public Criteria andNameNotLike(String value) {
addCriterion("NAME not like", value, "name");
return (Criteria) this;
}
public Criteria andNameIn(List
addCriterion("NAME in", values, "name");
return (Criteria) this;
}
public Criteria andNameNotIn(List
addCriterion("NAME not in", values, "name");
return (Criteria) this;
}
public Criteria andNameBetween(String value1, String value2) {
addCriterion("NAME between", value1, value2, "name");
return (Criteria) this;
}
public Criteria andNameNotBetween(String value1, String value2) {
addCriterion("NAME not between", value1, value2, "name");
return (Criteria) this;
}
public Criteria andSexIsNull() {
addCriterion("sex is null");
return (Criteria) this;
}
public Criteria andSexIsNotNull() {
addCriterion("sex is not null");
return (Criteria) this;
}
public Criteria andSexEqualTo(String value) {
addCriterion("sex =", value, "sex");
return (Criteria) this;
}
public Criteria andSexNotEqualTo(String value) {
addCriterion("sex <>", value, "sex");
return (Criteria) this;
}
public Criteria andSexGreaterThan(String value) {
addCriterion("sex >", value, "sex");
return (Criteria) this;
}
public Criteria andSexGreaterThanOrEqualTo(String value) {
addCriterion("sex >=", value, "sex");
return (Criteria) this;
}
public Criteria andSexLessThan(String value) {
addCriterion("sex <", value, "sex");
return (Criteria) this;
}
public Criteria andSexLessThanOrEqualTo(String value) {
addCriterion("sex <=", value, "sex");
return (Criteria) this;
}
public Criteria andSexLike(String value) {
addCriterion("sex like", value, "sex");
return (Criteria) this;
}
public Criteria andSexNotLike(String value) {
addCriterion("sex not like", value, "sex");
return (Criteria) this;
}
public Criteria andSexIn(List
addCriterion("sex in", values, "sex");
return (Criteria) this;
}
public Criteria andSexNotIn(List
addCriterion("sex not in", values, "sex");
return (Criteria) this;
}
public Criteria andSexBetween(String value1, String value2) {
addCriterion("sex between", value1, value2, "sex");
return (Criteria) this;
}
public Criteria andSexNotBetween(String value1, String value2) {
addCriterion("sex not between", value1, value2, "sex");
return (Criteria) this;
}
public Criteria andAgeIsNull() {
addCriterion("age is null");
return (Criteria) this;
}
public Criteria andAgeIsNotNull() {
addCriterion("age is not null");
return (Criteria) this;
}
public Criteria andAgeEqualTo(String value) {
addCriterion("age =", value, "age");
return (Criteria) this;
}
public Criteria andAgeNotEqualTo(String value) {
addCriterion("age <>", value, "age");
return (Criteria) this;
}
public Criteria andAgeGreaterThan(String value) {
addCriterion("age >", value, "age");
return (Criteria) this;
}
public Criteria andAgeGreaterThanOrEqualTo(String value) {
addCriterion("age >=", value, "age");
return (Criteria) this;
}
public Criteria andAgeLessThan(String value) {
addCriterion("age <", value, "age");
return (Criteria) this;
}
public Criteria andAgeLessThanOrEqualTo(String value) {
addCriterion("age <=", value, "age");
return (Criteria) this;
}
public Criteria andAgeLike(String value) {
addCriterion("age like", value, "age");
return (Criteria) this;
}
public Criteria andAgeNotLike(String value) {
addCriterion("age not like", value, "age");
return (Criteria) this;
}
public Criteria andAgeIn(List
addCriterion("age in", values, "age");
return (Criteria) this;
}
public Criteria andAgeNotIn(List
addCriterion("age not in", values, "age");
return (Criteria) this;
}
public Criteria andAgeBetween(String value1, String value2) {
addCriterion("age between", value1, value2, "age");
return (Criteria) this;
}
public Criteria andAgeNotBetween(String value1, String value2) {
addCriterion("age not between", value1, value2, "age");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
public static class Criterion {
private String condition;
private Object value;
private Object secondValue;
private boolean noValue;
private boolean singleValue;
private boolean betweenValue;
private boolean listValue;
private String typeHandler;
public String getCondition() {
return condition;
}
public Object getValue() {
return value;
}
public Object getSecondValue() {
return secondValue;
}
public boolean isNoValue() {
return noValue;
}
public boolean isSingleValue() {
return singleValue;
}
public boolean isBetweenValue() {
return betweenValue;
}
public boolean isListValue() {
return listValue;
}
public String getTypeHandler() {
return typeHandler;
}
protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
}
protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List>) {
this.listValue = true;
} else {
this.singleValue = true;
}
}
protected Criterion(String condition, Object value) {
this(condition, value, null);
}
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
}
protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}
2.3 生成studentMapper.java
package com.sx.kak.po;
import java.util.ArrayList;
import java.util.List;
public class StudentExample {
protected String orderByClause;
protected boolean distinct;
protected List
public StudentExample() {
oredCriteria = new ArrayList
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public String getOrderByClause() {
return orderByClause;
}
public void setDistinct(boolean distinct) {
this.distinct = distinct;
}
public boolean isDistinct() {
return distinct;
}
public List
return oredCriteria;
}
public void or(Criteria criteria) {
oredCriteria.add(criteria);
}
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
protected Criteria createCriteriaInternal() {
Criteria criteria = new Criteria();
return criteria;
}
public void clear() {
oredCriteria.clear();
orderByClause = null;
distinct = false;
}
protected abstract static class GeneratedCriteria {
protected List
protected GeneratedCriteria() {
super();
criteria = new ArrayList
}
public boolean isValid() {
return criteria.size() > 0;
}
public List
return criteria;
}
public List
return criteria;
}
protected void addCriterion(String condition) {
if (condition == null) {
throw new RuntimeException("Value for condition cannot be null");
}
criteria.add(new Criterion(condition));
}
protected void addCriterion(String condition, Object value, String property) {
if (value == null) {
throw new RuntimeException("Value for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value));
}
protected void addCriterion(String condition, Object value1, Object value2, String property) {
if (value1 == null || value2 == null) {
throw new RuntimeException("Between values for " + property + " cannot be null");
}
criteria.add(new Criterion(condition, value1, value2));
}
public Criteria andIdIsNull() {
addCriterion("id is null");
return (Criteria) this;
}
public Criteria andIdIsNotNull() {
addCriterion("id is not null");
return (Criteria) this;
}
public Criteria andIdEqualTo(Integer value) {
addCriterion("id =", value, "id");
return (Criteria) this;
}
public Criteria andIdNotEqualTo(Integer value) {
addCriterion("id <>", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThan(Integer value) {
addCriterion("id >", value, "id");
return (Criteria) this;
}
public Criteria andIdGreaterThanOrEqualTo(Integer value) {
addCriterion("id >=", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThan(Integer value) {
addCriterion("id <", value, "id");
return (Criteria) this;
}
public Criteria andIdLessThanOrEqualTo(Integer value) {
addCriterion("id <=", value, "id");
return (Criteria) this;
}
public Criteria andIdIn(List
addCriterion("id in", values, "id");
return (Criteria) this;
}
public Criteria andIdNotIn(List
addCriterion("id not in", values, "id");
return (Criteria) this;
}
public Criteria andIdBetween(Integer value1, Integer value2) {
addCriterion("id between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andIdNotBetween(Integer value1, Integer value2) {
addCriterion("id not between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andNameIsNull() {
addCriterion("NAME is null");
return (Criteria) this;
}
public Criteria andNameIsNotNull() {
addCriterion("NAME is not null");
return (Criteria) this;
}
public Criteria andNameEqualTo(String value) {
addCriterion("NAME =", value, "name");
return (Criteria) this;
}
public Criteria andNameNotEqualTo(String value) {
addCriterion("NAME <>", value, "name");
return (Criteria) this;
}
public Criteria andNameGreaterThan(String value) {
addCriterion("NAME >", value, "name");
return (Criteria) this;
}
public Criteria andNameGreaterThanOrEqualTo(String value) {
addCriterion("NAME >=", value, "name");
return (Criteria) this;
}
public Criteria andNameLessThan(String value) {
addCriterion("NAME <", value, "name");
return (Criteria) this;
}
public Criteria andNameLessThanOrEqualTo(String value) {
addCriterion("NAME <=", value, "name");
return (Criteria) this;
}
public Criteria andNameLike(String value) {
addCriterion("NAME like", value, "name");
return (Criteria) this;
}
public Criteria andNameNotLike(String value) {
addCriterion("NAME not like", value, "name");
return (Criteria) this;
}
public Criteria andNameIn(List
addCriterion("NAME in", values, "name");
return (Criteria) this;
}
public Criteria andNameNotIn(List
addCriterion("NAME not in", values, "name");
return (Criteria) this;
}
public Criteria andNameBetween(String value1, String value2) {
addCriterion("NAME between", value1, value2, "name");
return (Criteria) this;
}
public Criteria andNameNotBetween(String value1, String value2) {
addCriterion("NAME not between", value1, value2, "name");
return (Criteria) this;
}
public Criteria andSexIsNull() {
addCriterion("sex is null");
return (Criteria) this;
}
public Criteria andSexIsNotNull() {
addCriterion("sex is not null");
return (Criteria) this;
}
public Criteria andSexEqualTo(String value) {
addCriterion("sex =", value, "sex");
return (Criteria) this;
}
public Criteria andSexNotEqualTo(String value) {
addCriterion("sex <>", value, "sex");
return (Criteria) this;
}
public Criteria andSexGreaterThan(String value) {
addCriterion("sex >", value, "sex");
return (Criteria) this;
}
public Criteria andSexGreaterThanOrEqualTo(String value) {
addCriterion("sex >=", value, "sex");
return (Criteria) this;
}
public Criteria andSexLessThan(String value) {
addCriterion("sex <", value, "sex");
return (Criteria) this;
}
public Criteria andSexLessThanOrEqualTo(String value) {
addCriterion("sex <=", value, "sex");
return (Criteria) this;
}
public Criteria andSexLike(String value) {
addCriterion("sex like", value, "sex");
return (Criteria) this;
}
public Criteria andSexNotLike(String value) {
addCriterion("sex not like", value, "sex");
return (Criteria) this;
}
public Criteria andSexIn(List
addCriterion("sex in", values, "sex");
return (Criteria) this;
}
public Criteria andSexNotIn(List
addCriterion("sex not in", values, "sex");
return (Criteria) this;
}
public Criteria andSexBetween(String value1, String value2) {
addCriterion("sex between", value1, value2, "sex");
return (Criteria) this;
}
public Criteria andSexNotBetween(String value1, String value2) {
addCriterion("sex not between", value1, value2, "sex");
return (Criteria) this;
}
public Criteria andAgeIsNull() {
addCriterion("age is null");
return (Criteria) this;
}
public Criteria andAgeIsNotNull() {
addCriterion("age is not null");
return (Criteria) this;
}
public Criteria andAgeEqualTo(String value) {
addCriterion("age =", value, "age");
return (Criteria) this;
}
public Criteria andAgeNotEqualTo(String value) {
addCriterion("age <>", value, "age");
return (Criteria) this;
}
public Criteria andAgeGreaterThan(String value) {
addCriterion("age >", value, "age");
return (Criteria) this;
}
public Criteria andAgeGreaterThanOrEqualTo(String value) {
addCriterion("age >=", value, "age");
return (Criteria) this;
}
public Criteria andAgeLessThan(String value) {
addCriterion("age <", value, "age");
return (Criteria) this;
}
public Criteria andAgeLessThanOrEqualTo(String value) {
addCriterion("age <=", value, "age");
return (Criteria) this;
}
public Criteria andAgeLike(String value) {
addCriterion("age like", value, "age");
return (Criteria) this;
}
public Criteria andAgeNotLike(String value) {
addCriterion("age not like", value, "age");
return (Criteria) this;
}
public Criteria andAgeIn(List
addCriterion("age in", values, "age");
return (Criteria) this;
}
public Criteria andAgeNotIn(List
addCriterion("age not in", values, "age");
return (Criteria) this;
}
public Criteria andAgeBetween(String value1, String value2) {
addCriterion("age between", value1, value2, "age");
return (Criteria) this;
}
public Criteria andAgeNotBetween(String value1, String value2) {
addCriterion("age not between", value1, value2, "age");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {
protected Criteria() {
super();
}
}
public static class Criterion {
private String condition;
private Object value;
private Object secondValue;
private boolean noValue;
private boolean singleValue;
private boolean betweenValue;
private boolean listValue;
private String typeHandler;
public String getCondition() {
return condition;
}
public Object getValue() {
return value;
}
public Object getSecondValue() {
return secondValue;
}
public boolean isNoValue() {
return noValue;
}
public boolean isSingleValue() {
return singleValue;
}
public boolean isBetweenValue() {
return betweenValue;
}
public boolean isListValue() {
return listValue;
}
public String getTypeHandler() {
return typeHandler;
}
protected Criterion(String condition) {
super();
this.condition = condition;
this.typeHandler = null;
this.noValue = true;
}
protected Criterion(String condition, Object value, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.typeHandler = typeHandler;
if (value instanceof List>) {
this.listValue = true;
} else {
this.singleValue = true;
}
}
protected Criterion(String condition, Object value) {
this(condition, value, null);
}
protected Criterion(String condition, Object value, Object secondValue, String typeHandler) {
super();
this.condition = condition;
this.value = value;
this.secondValue = secondValue;
this.typeHandler = typeHandler;
this.betweenValue = true;
}
protected Criterion(String condition, Object value, Object secondValue) {
this(condition, value, secondValue, null);
}
}
}
2.4 生成StudentMapper.xml
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
and ${criterion.condition}
and ${criterion.condition} #{criterion.value}
and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}
and ${criterion.condition}
#{listItem}
id, NAME, sex, age
select
distinct
from student
order by ${orderByClause}
select
from student
where id = #{id,jdbcType=INTEGER}
delete from student
where id = #{id,jdbcType=INTEGER}
delete from student
insert into student (id, NAME, sex,
age)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{sex,jdbcType=VARCHAR},
#{age,jdbcType=VARCHAR})
insert into student
id,
NAME,
sex,
age,
#{id,jdbcType=INTEGER},
#{name,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
#{age,jdbcType=VARCHAR},
select count(*) from student
update student
id = #{record.id,jdbcType=INTEGER},
NAME = #{record.name,jdbcType=VARCHAR},
sex = #{record.sex,jdbcType=VARCHAR},
age = #{record.age,jdbcType=VARCHAR},
update student
set id = #{record.id,jdbcType=INTEGER},
NAME = #{record.name,jdbcType=VARCHAR},
sex = #{record.sex,jdbcType=VARCHAR},
age = #{record.age,jdbcType=VARCHAR}
update student
NAME = #{name,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
age = #{age,jdbcType=VARCHAR},
where id = #{id,jdbcType=INTEGER}
update student
set NAME = #{name,jdbcType=VARCHAR},
sex = #{sex,jdbcType=VARCHAR},
age = #{age,jdbcType=VARCHAR}
where id = #{id,jdbcType=INTEGER}
2.5 编写application.yml
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db0711?characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
mybatis:
type-aliases-package: po
mapper-locations: classpath:mapping/*.xml
2.6 编写JsonUtils工具类
package com.sx.kak.utils;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtils {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 将对象转换成json字符串。
*
Title: pojoToJson
*
Description:
* @param data
* @return
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json结果集转化为对象
*
* @param jsonData json数据
* @param clazz 对象中的object类型
* @return
*/
public static
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
*
Title: jsonToList
*
Description:
* @param jsonData
* @param beanType
* @return
*/
public static
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
2.7 编写RedisUtils工具类
package com.sx.kak.utils;
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtils {
// 定义jackson对象
private static final ObjectMapper MAPPER = new ObjectMapper();
/**
* 将对象转换成json字符串。
*
Title: pojoToJson
*
Description:
* @param data
* @return
*/
public static String objectToJson(Object data) {
try {
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json结果集转化为对象
*
* @param jsonData json数据
* @param clazz 对象中的object类型
* @return
*/
public static
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成pojo对象list
*
Title: jsonToList
*
Description:
* @param jsonData
* @param beanType
* @return
*/
public static
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
2.8 编写RedisUtils工具类的实现类
package com.sx.kak.utils;
import redis.clients.jedis.Jedis;
/**
* Created by Kak on 2020/9/17.
*/
public class SingleTonRedisUtil implements RedisUtils{
private Jedis jedis;
public SingleTonRedisUtil(Jedis jedis){
this.jedis = jedis;
}
@Override
public void hset(String key, String filed, String value) {
jedis.hset(key, filed, value);
}
@Override
public String hget(String key, String field) {
return jedis.hget(key, field);
}
}
2.9 配置初始化文件
package com.sx.kak.config;
import com.sx.kak.utils.SingleTonRedisUtil;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
/**
* Created by Kak on 2020/9/17.
*/
@Configuration
public class RedisConfig {
@Bean
public SingleTonRedisUtil singleTonRedisUtil(@Qualifier("jedis") Jedis jedis){
SingleTonRedisUtil singleTonRedisUtil = new SingleTonRedisUtil(jedis);
return singleTonRedisUtil;
}
@Bean(name = "jedis")
public Jedis getJedis(){
Jedis jedis = new Jedis("127.0.0.1",6379);
return jedis;
}
}
2.10 配置StudentService.java
package com.sx.kak.service;
import com.sx.kak.po.Student;
import java.util.List;
/**
* Created by Kak on 2020/9/17.
*/
public interface StudentService {
public List
}
2.11 配置StudentServiceImpl.java
package com.sx.kak.service.serviceImhttp://pl;
import com.sx.kak.mapper.StudentMapper;
import com.sx.kak.po.Student;
import com.sx.kak.po.StudentExample;
import com.sx.kak.service.StudentService;
import com.sx.kak.utils.JsonUtils;
import com.sx.kak.utils.SingleTonRedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by Kak on 2020/9/17.
*/
@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
@Autowired(required = false)
private StudentMapper studentMapper;
//注入自定义的redis工具
@Autowired
private SingleTonRedisUtil redisUtil;
@Override
public List
//获取redis中存出的json字符串
String student = redisUtil.hget("student", "0");
if(student!=null){
log.info("get student from redis");
//json对象,封装的对象类型
List
return students;
}
//创建查询模板对象
StudentExample studentExample = new StudentExample();
List
log.info("get student from db");
try{
//将查询的集合转化为json字符串
String s = JsonUtils.objectToJson(students);
//放入redis中
redisUtil.hset("student","0",s);
log.info("set data to redis");
}catch (Exception ex){
log.info(ex.getMessage());
}
return students;
}
}
2.12 编写全查展示页面
2.13 编写StudentController
package com.sx.kak.Controller;
import com.sx.kak.po.Student;
import com.sx.kak.service.StudentService;
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 org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by Kak on 2020/9/17.
*/
@Controller
public class StudentController {
@Autowired(required = false)
private StudentService studentService;
@RequestMapping("/findStudent")
public String findAllStu(Model model){
List
model.addAttribute("students",allStudent);
return "showAllStudent";
}
}
3. 结果
从打印出的日志可以看出,第一次访问是从数据库得到数据,然后存入redis中
第二次直接从redis中获取
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~