/**完成步骤
1、创建关系型数据库mysql的Provice库,同时启动nosql系列的redis数据库
2、创建项目,导入相关的jar包
3、创建jedis/utils/domain/dao/service/web项目框架包
4、书写相关代码
*/
//页面
<!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) {
//1、获取select
var province=$("#province");
//2、遍历json数组
$(data).each(function () {
//3、创建option
var option ="<option name ='"+this.id+"'>"+this.name+"</option>";
//4、调用select的append追加option
province.append(option);
});
});
});
</script>
</head>
<body>
<select name="province" id="province">
<option>--请选择省份--</option>
</select>
</body>
</html>
//Web层代码实现
package cn.hope.web.servlet;
import cn.hope.domain.Province;
import cn.hope.service.ProvinceService;
import cn.hope.service.impl.ProvinceServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* @author newcityman
* @date 2019/9/20 - 23:57
*/
@WebServlet("/provinceServlet")
public class ProvinceServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/* //1、调用service查询
ProvinceService service = new ProvinceServiceImpl();
List<Province> provinces = service.findAll();
//2、序列化list为json数据
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(provinces);*/
// System.out.println(json);
//1、调用service查询
ProvinceService service = new ProvinceServiceImpl();
String json = service.findAllJson();
//3、响应结果
response.setContentType("application/json;charset=utf-8");
response.getWriter().write(json);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
//service层
package cn.hope.service.impl;
import cn.hope.dao.ProvinceDao;
import cn.hope.dao.impl.ProvinceDaoImpl;
import cn.hope.domain.Province;
import cn.hope.jedis.utils.JedisPoolUtils;
import cn.hope.service.ProvinceService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.List;
/**
* @author newcityman
* @date 2019/9/20 - 23:50
*/
public class ProvinceServiceImpl implements ProvinceService {
//申明dao
private ProvinceDao dao = new ProvinceDaoImpl();
@Override
public List<Province> findAll() {
return dao.findAll();
}
@Override
public String findAllJson() {
//1、通过工具类获取redis连接
Jedis jedis = JedisPoolUtils.getJedis();
//2、获取redis中province的值
String province_json = jedis.get("province");
//3、判断province_json值是否为null
if(province_json == null || province_json.length()<=0){
//4、说明redis中没有province的数据,需要从数据库中查找数据
List<Province> ps = dao.findAll();
ObjectMapper mapper = new ObjectMapper();
try {
province_json = mapper.writeValueAsString(ps);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
System.out.println("第一次访问redis,数据来之数据库。。。。");
jedis.set("province",province_json);
//释放jedis,回归连接池
jedis.close();
}else {
System.out.println("数据来之redis缓存。。。。");
}
return province_json;
}
}
//dao层
package cn.hope.dao.impl;
import cn.hope.dao.ProvinceDao;
import cn.hope.domain.Province;
import cn.hope.utils.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
/**
* @author newcityman
* @date 2019/9/20 - 23:29
*/
public class ProvinceDaoImpl implements ProvinceDao {
//1、声明成员变量JdbcTemplate
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));
System.out.println(list);
return list;
}
}
//Jedis工具包
package cn.hope.jedis.utils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @author newcityman
* @date 2019/9/20 - 0:33
* JedisPool工具类
* 加载配置文件,配置连接池的连接池
* 提供获取连接的方法
*/
public class JedisPoolUtils {
private static JedisPool jedisPool;
static {
//读取配置文件
InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
//创建Properties对象
Properties prop = new Properties();
try {
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
}
//获取数据,设置到JedisPoolConfig()中
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(Integer.parseInt( prop.getProperty("maxTotal")));
config.setMaxTotal(Integer.parseInt(prop.getProperty("maxIdle")));
jedisPool=new JedisPool(config,prop.getProperty("host"),Integer.parseInt(prop.getProperty("port")));
}
/**
* 获取连接方法
* @return
*/
public static Jedis getJedis(){
return jedisPool.getResource();
}
}
//JDBCUtils工具包
package cn.hope.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* 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();
}
}