黑马B站网课案例
1. 提供index.html页面,页面中有一个省份 下拉列表
2. 当 页面加载完成后 发送ajax请求,加载所有省份
**注意:**使用redis缓存一些不经常发生变化的数据。
数据库的数据一旦发生改变,则需要更新缓存。
数据库的表执行 增删改的相关操作,需要将redis缓存数据情况,再次存入
在service对应的增删改方法中,将redis数据删除。
提醒:按照项目结构安排好每个文件的位置,注意导入所欲的jar包和配置文件,还有JQuery文件
CREATE TABLE province( -- 创建表
id INT PRIMARY KEY AUTO_INCREMENT,
NAME VARCHAR(20) NOT NULL
);
-- 插入数据
INSERT INTO province VALUES(NULL,'北京');
INSERT INTO province VALUES(NULL,'上海');
INSERT INTO province VALUES(NULL,'广州');
INSERT INTO province VALUES(NULL,'陕西');
jar包百度网盘链接,可以自己找到对应的下载
链接:https://pan.baidu.com/s/1qYyOQMH-rJCX14LYhVLeCQ
提取码:ggq9
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
$(function (){
//发送ajax请求,加载所有省份数据
$.get("provinceServlet", {}, function (data){
//获取到的是这个样子[{"id":1,"name":"北京"},{"id":2,"name":"上海"},{"id":3,"name":"广州"},{"id":4,"name":"陕西"}]
//1.获取select
var province = $("#province");
//2.遍历json数组
$(data).each(function (){
//3.创建
var option = "+this.name+""
//4.调用select的appe追加option
province.append(option);
})
});
});
</script>
</head>
<body>
<select id="province">
<option>---请选择省份--- </option>
</select>
</body>
</html>
实体类,对应数据库的实体类
package cn.itcast.domain;
public class Province {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public interface ProvinceDao {
public List<Province> findAll();
}
public class ProvinceDaoImpl implements ProvinceDao {
//1.声明成员变量jdbctemplement
private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Override
public List<Province> findAll() {
//1.定义sql
String sql = "select * from province";
//2.执行sql
List<Province> list = template.query(sql, new BeanPropertyRowMapper<Province>(Province.class));
return list;
}
}
public interface ProvinceService {
public List<Province> findAll();
public String findAllJson();
}
public class ProvinceServiceImpl implements ProvinceService {
//声明dao
private ProvinceDao dao = new ProvinceDaoImpl();
@Override
public List<Province> findAll() {
return dao.findAll();
}
/**
* 使用redis缓存
* @return
*/
@Override
public String findAllJson() {
//1.先从redis中查询数据
//1.1获取redis客户端连接
Jedis jedis = JedisPoolUtils.getJedis();
String province_json = jedis.get("province");
//2.判断province_json数据是否为null
if (province_json == null || province_json.length() == 0){
//redis中没有数据
System.out.println("redis中没数据,查询数据库。。。");
//2.1从数据中查询
List<Province> ps = dao.findAll();
//2.2将list序列化为json
ObjectMapper mapper = new ObjectMapper();
try {
province_json = mapper.writeValueAsString(ps);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
//2.3将json数据导入redis
jedis.set("province",province_json);
//归还连接
jedis.close();
}else {
System.out.println("redis中有数据,查询缓存。。。");
}
return province_json;
}
}
@WebServlet("/provinceServlet")
public class ProvinceServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.调用service查询
ProvinceService service = new ProvinceServiceImpl();
String json = service.findAllJson();
System.out.println(json);
//3.响应结果
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(json);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
/**
* JDBC工具类 使用Durid连接池
*/
public class JDBCUtils {
private static DataSource ds ;
static {
try {
//1.加载配置文件
Properties pro = new Properties();
//使用ClassLoader加载配置文件,获取字节输入流
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//2.初始化连接池对象
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取连接池对象
*/
public static DataSource getDataSource(){
return ds;
}
/**
* 获取连接Connection对象
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
}
host=127.0.0.1
port=6379
maxTotal=50
maxIdle=10
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///heima
username=root
password=
initialSize=5
maxActive=10
maxWait=3000