前言
在实际开中我们都知道 ,前后端的交互中数据的传递是一个很重要的部分,在项目中一般前端请求后端的数据时controller层一般返回对象格式的数据,例如数组,对象,或者是json格式的数据。
而使用Redis作为缓存在实际的开发中也是提高系统响应时间和降低后台数据库的压力也是常用的操作。
前端获取springBoot的数据(以json格式)进行展示,并相应的存取到Redis缓存中过程
1.做数据库和redis 的配置
server.port=8081
spring.application.name=travels
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jdbctest?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=277171
# Redis_config
# Redis数据库索引(默认为0)
spring.redis.database=4
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接超时时间(毫秒)
spring.redis.timeout=3600
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# jedis超时
spring.redis.jedis.shutdown-timeout=100
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
mysql数据库例子(可自行写表进行测试)
2.编写server层,controller层和Dao层
server层
package com.mzc.Service.impl;
import com.mzc.Dao.impl.BookDao;
import com.mzc.Model.Book;
import lombok.experimental.Accessors;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* @author jikeMisma
* @date 2020/6/19 - 11:13
*/
@Service
public class BookService {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Autowired
BookDao bookDao;
public int addBook(Book book){
return bookDao.addBook(book);
}
public int updateBook(Book book){
return bookDao.updateBook(book);
}
public int deleteBookBtId(Integer id){
return bookDao.deleteBookBtId(id);
}
public Book getBookById(Integer id){
return bookDao.getBookById(id);
}
public List<Book> getAllBooks(){
return bookDao.getAllBooks();
}
public Object getBookMsg(){
//查询book表中的信息以list形式返回
List<Map<String,Object>> list = bookDao.getBookMsg();
for(Map m : list){
String key = (String) m.get("author");
String value = (String) m.get("bookname");
stringRedisTemplate.boundListOps(key).rightPush(value);
List redislist = stringRedisTemplate.boundListOps(key).range(0, 10);
System.out.println("fromRedis--->"+redislist);
}
System.out.println(list);
//list设置为json格式
JSONArray jsonObject = new JSONArray(list);
FileWriter fw = null;
try {
fw = new FileWriter(new File(System.getProperty("user.dir") + "\\src\\main\\resources\\BookMsg.json"));
BufferedWriter bw = new BufferedWriter(fw);
bw.write(jsonObject.toString());
bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
public Object getBookByAuthor(String author){
List<Map<String,Object>> list1 = bookDao.getBookByAuthor(author);
System.out.println(list1);
return list1;
}
public boolean isAuthor(String author) {
List<Map<String,Object>> list1 = bookDao.getBookByAuthor(author);
int size = list1.size();
if(size == 0){
return false;
}else{
return true;
}
}
}
controller层
package com.mzc.Controller;
import com.mzc.Model.Book;
import com.mzc.Service.impl.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @author jikeMisma
* @date 2020/6/19 - 11:16
*/
@RestController
public class BookController {
@Autowired
BookService bookService;
@GetMapping("/getBookMsg")
public Object getBookMsg(){
return bookService.getBookMsg();
}
//根据作者姓名查询相应的图书数据
@GetMapping("/getBookByAuthor/{author}")
public Object getBookByAuthor(@PathVariable String author){
if(bookService.isAuthor(author) == true) {
return bookService.getBookByAuthor(author);
}else{
System.out.println("请求的作者<"+author+">不存在!");
return "数据不存在!";
}
}
}
Dao层
package com.mzc.Dao.impl;
import com.mzc.Model.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
/**
* @author jikeMisma
* @date 2020/6/19 - 10:52
*/
@Repository
public class BookDao {
@Autowired
JdbcTemplate jdbcTemplate;
public int addBook(Book book) {
return jdbcTemplate.update("insert into t_book(bookname,author) values (?,?)", book.getBookname(), book.getAuthor());
}
public int updateBook(Book book) {
return jdbcTemplate.update("insert into t_book set bookname=?,author=? where id =?", book.getBookname(), book.getAuthor(), book.getId());
}
public int deleteBookBtId(Integer id) {
return jdbcTemplate.update("delete from t_book where id=?", id);
}
public Book getBookById(Integer id){
return jdbcTemplate.queryForObject("select * from t_book where id=?",new BeanPropertyRowMapper<>(Book.class),id);
}
public List<Book> getAllBooks(){
return jdbcTemplate.query("select * from t_book",new BeanPropertyRowMapper<>(Book.class));
}
public List<Map<String, Object>> getBookMsg(){
String sSQL = "SELECT bookname,author FROM t_book ";
return jdbcTemplate.queryForList(sSQL);
}
//根据请求的作者查询相应的信息
public List<Map<String, Object>> getBookByAuthor(String author) {
String sql = "SELECT bookname,author FROM t_book where author = ?";
return jdbcTemplate.queryForList(sql,author);
}
}
3.启动项目(mysql服务器,redis服务器)
启动项目,在浏览器发起请求
浏览器发起对于后台的请求:
http://localhost:8081/getBookMsg
请求后前端显示如下:
控制台和redis显示如下: