キャッシュとしてredisを使用する方法、データへのユーザーアクセスを強化する方法
/**
1、 mysql Provice , nosql redis
2、 , jar
3、 jedis/utils/domain/dao/service/web
4、
*/
//
span style="color:#bababa;">html>
lang="en">
charset="UTF-8">
Title<span style="color:#e8bf6a;"/>
src=<span style="color:#a5c261;">"js/jquery-3.3.1.min.js"<span style="color:#e8bf6a;">>
<br><span style="color:#e8bf6a;"> <span style="color:#9876aa;">$(<span style="color:#cc7832;font-weight:bold;">function () {<br><span style="color:#278009;">// ajax <br><span style="color:#278009;"> <span style="color:#9876aa;">$.<span style="color:#ffc66d;">get(<span style="color:#6a8759;">"provinceServlet"<span style="color:#cc7832;">,{}<span style="color:#cc7832;">,<span style="color:#cc7832;font-weight:bold;">function (data) {<br><span style="color:#278009;">//1、 select<br><span style="color:#278009;"> <span style="color:#cc7832;font-weight:bold;">var province=<span style="color:#9876aa;">$(<span style="color:#6a8759;">"#province")<span style="color:#cc7832;">;<br><span style="color:#cc7832;"> <span style="color:#278009;">//2、 json <br><span style="color:#278009;"> <span style="color:#9876aa;">$(data).<span style="color:#ffc66d;">each(<span style="color:#cc7832;font-weight:bold;">function () {<br><span style="color:#278009;">//3、 option<br><span style="color:#278009;"> <span style="color:#cc7832;font-weight:bold;">var option =<span style="color:#6a8759;">"<option name ='"+<span style="color:#cc7832;font-weight:bold;">this.<span style="color:#9876aa;">id+<span style="color:#6a8759;">"'>"+<span style="color:#cc7832;font-weight:bold;">this.<span style="color:#9876aa;">name+<span style="color:#6a8759;">"</option>"<span style="color:#cc7832;">;<br><span style="color:#cc7832;"> <span style="color:#278009;">//4、 select append option<br><span style="color:#278009;"> province.<span style="color:#ffc66d;">append(option)<span style="color:#cc7832;">;<br><span style="color:#cc7832;"> })<span style="color:#cc7832;">;<br><span style="color:#cc7832;"> })<span style="color:#cc7832;">;<br><span style="color:#cc7832;"> })<span style="color:#cc7832;">;<br><span style="color:#cc7832;"> <span style="color:#e8bf6a;">
//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 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 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 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 findAll() {
//1、 sql
String sql = "select * from province";
//2、 sql
List list = template.query(sql, new BeanPropertyRowMapper(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();
}
}
転載先:https://www.cnblogs.com/newcityboy/p/11565158.html