准备:在mysql中准备两张表 ,student表存学生信息的,score表存学生的各科成绩
studnet表
score表
例题:
一、通过班级名称查询班级人数(要求加入缓存,这里缓存用的数据库是redis)
1.写了一个工具类,连接mysql数据库
package com.shujia.util;
import java.sql.Connection;
import java.sql.DriverManager;
/**
* 这是连接数据库的工具类
*/
public class JDBCUtil {
private static Connection conn;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://master:3306/user?useUnicode=true&characterEncoding=utf-8", "root", "123456");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取jdbc
*/
public static Connection getConnection(){
return conn;
}
}
2.studentsController类的代码,该层实现的是与用户交互
package com.shujia.controller;
import com.shujia.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentsController {
@Autowired
private StudentService studentService;
/**
* 该方法是用来通过班级名称来查询该班的人数
* @param clazz
* @return
*/
@GetMapping("/sumPerson")
public String sumPerson(String clazz){
String str = studentService.sumPerson(clazz);
return str;
}
}
3.业务层,有一个StudentService接口和一个StudentServiceImpl实现类
public interface StudentService {
String sumPerson(String clazz);
String sumScore(String id);
}
package com.shujia.service.impl;
import com.shujia.dao.StudentDao;
import com.shujia.dao.impl.RedisDao;
import com.shujia.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Autowired
private RedisDao redisDao;
@Override
public String sumPerson(String clazz) {
/**
* 先去缓存数据库redis查询有没有该班级
*/
String s = redisDao.queryStudent(clazz);
/**
* 有就直接返回该班级的人数
*/
if(s!=null){
redisDao.expire(clazz,10);
return s;
}
/**
* 没有就去数据中查询
*/
String str = studentDao.sumPerson(clazz);
/**
* 将数据中查询到的结果存到缓存中
*/
redisDao.insertStudent(clazz,str);
/**
* 设置数据过期时间,当不在访问时,自动删除
*/
redisDao.expire(clazz,10);
return str;
}
}
4.持久层,有一个StudentDao接口和StudentDaoImpl实现类
package com.shujia.dao;
import com.shujia.bean.Student;
public interface StudentDao {
String sumPerson(String clazz);
}
package com.shujia.dao.impl;
import com.shujia.bean.Student;
import com.shujia.dao.StudentDao;
import com.shujia.util.JDBCUtil;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@Component
public class StudentDaoImpl implements StudentDao {
/**
* 通过工具类连接数据库
*/
private Connection con = JDBCUtil.getConnection();
@Override
public String sumPerson(String clazz) {
Connection con = JDBCUtil.getConnection();
try {
//通过班级字段去查询得到班级的总人数
PreparedStatement stat = con.prepareStatement("select clazz,count(*) as sum from student where clazz=?");
stat.setString(1,clazz);
ResultSet resultSet = stat.executeQuery();
if(resultSet.next()){
String clazz1 = resultSet.getString("clazz");
int sum = resultSet.getInt("sum");
return clazz1+":"+sum;
}
} catch (SQLException e) {
e.printStackTrace();
}
return "输入的班级错误";
}
}
5.加入了缓存redis
package com.shujia.dao.impl;
import com.shujia.bean.Student;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
@Component
public class RedisDao {
private static Jedis jedis;
static {
//建立redis连接
jedis = new Jedis("master", 6379);
}
/**
* 增加过期时间
*
*/
public void expire(String key,int ex){
jedis.expire(key, ex);
}
/**
* 查看班级人数
*
*/
public String queryStudent(String key){
String s = jedis.get(key);
if(s!=null){
return s;
}
return null;
}
/**
* 插入缓存中没有的数据
*/
public void insertStudent(String key,String value){
jedis.set(key,value);
}
}
6.浏览器进行查询
二、通过学号查询学生总分(要求加入缓存,这里用到的是redis,该题代码是在题一里面加的)
1.studentsController类的代码,该层实现的是与用户交互
package com.shujia.controller;
import com.shujia.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StudentsController {
@Autowired
private StudentService studentService;
/**
* 该方法是用来通过班级名称来查询该班的人数
* @param clazz
* @return
*/
@GetMapping("/sumPerson")
public String sumPerson(String clazz){
String str = studentService.sumPerson(clazz);
return str;
}
@GetMapping("/sumScore")
private String sumScore(String id){
String s = studentService.sumScore(id);
return s;
}
}
2.业务层,有一个StudentService接口和一个StudentServiceImpl实现类
public interface StudentService {
String sumPerson(String clazz);
String sumScore(String id);
}
package com.shujia.service.impl;
import com.shujia.dao.StudentDao;
import com.shujia.dao.impl.RedisDao;
import com.shujia.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentDao studentDao;
@Autowired
private RedisDao redisDao;
@Override
public String sumPerson(String clazz) {
/**
* 先去缓存数据库redis查询有没有该班级
*/
String s = redisDao.queryStudent(clazz);
/**
* 有就直接返回该班级的人数
*/
if(s!=null){
redisDao.expire(clazz,10);
return s;
}
/**
* 没有就去数据中查询
*/
String str = studentDao.sumPerson(clazz);
/**
* 将数据中查询到的结果存到缓存中
*/
redisDao.insertStudent(clazz,str);
/**
* 设置数据过期时间,当不在访问时,自动删除
*/
redisDao.expire(clazz,10);
return str;
}
/**
* 统计学生的总分
*/
public String sumScore(String id){
/**
* 先去缓存数据库redis查询有没有该同学
*/
String s1 = redisDao.queryStudent(id);
if(s1!=null){
redisDao.expire(id,10);
return s1;
}
String s = studentDao.sumScore(id);
/**
* 将数据中查询到的结果存到缓存中
*/
redisDao.insertStudent(id,s);
redisDao.expire(id,10);
return s;
}
}
3.持久层,有一个StudentDao接口和StudentDaoImpl实现类
package com.shujia.dao;
import com.shujia.bean.Student;
public interface StudentDao {
String sumPerson(String clazz);
String sumScore(String id);
}
package com.shujia.dao.impl;
import com.shujia.bean.Student;
import com.shujia.dao.StudentDao;
import com.shujia.util.JDBCUtil;
import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
@Component
public class StudentDaoImpl implements StudentDao {
/**
* 通过工具类连接数据库
*/
private Connection con = JDBCUtil.getConnection();
@Override
public String sumPerson(String clazz) {
Connection con = JDBCUtil.getConnection();
try {
//通过班级字段去查询得到班级的总人数
PreparedStatement stat = con.prepareStatement("select clazz,count(*) as sum from student where clazz=?");
stat.setString(1,clazz);
ResultSet resultSet = stat.executeQuery();
if(resultSet.next()){
String clazz1 = resultSet.getString("clazz");
int sum = resultSet.getInt("sum");
return clazz1+":"+sum;
}
} catch (SQLException e) {
e.printStackTrace();
}
return "输入的班级错误";
}
@Override
public String sumScore(String id) {
try {
//通过id去score表中查询该学生的成绩,并得到总成绩
PreparedStatement stat = con.prepareStatement("select id,sum(score) as score from score where id=?");
stat.setString(1,id);
//是想在返回数据的时候是姓名+总分,所以通过id去student表中查询该学生的姓名
PreparedStatement stat1 = con.prepareStatement("select name from student where id=?");
stat1.setString(1,id);
ResultSet resultSet1 = stat1.executeQuery();
String name=null;
if(resultSet1.next()){
name = resultSet1.getString("name");
System.out.println(name);
}
ResultSet resultSet = stat.executeQuery();
if(resultSet.next()){
String score = resultSet.getString("score");
return name+":"+score;
}
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
4.加入了缓存redis
package com.shujia.dao.impl;
import com.shujia.bean.Student;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
@Component
public class RedisDao {
private static Jedis jedis;
static {
//建立redis连接
jedis = new Jedis("master", 6379);
}
/**
* 增加过期时间
*
*/
public void expire(String key,int ex){
jedis.expire(key, ex);
}
/**
* 查看班级人数
*
*/
public String queryStudent(String key){
String s = jedis.get(key);
if(s!=null){
return s;
}
return null;
}
/**
* 插入缓存中没有的数据
*/
public void insertStudent(String key,String value){
jedis.set(key,value);
}
}
5.浏览器进行查询