Redis
Buffered in memory, persisted in hard disk, non-relational database, data type is key-value pair
Jedis
java operation redis
step
// 创建
Jedis jedis = new Jedis("localhost",6379);
// 操作
jedis.set("username","zhangsan");
// 关闭
jedis.close();
Connection pool JedisPool
// 创建配置对象
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(50);
// 创建连接池对象
JedisPool jedisPool = new JedisPool(jedisPoolConfig,"localhost",6379);
// 获取连接
Jedis jedis = jedisPool.getResource();
// 使用
jedis.set("hello","dd");
// 关闭,归还到连接池
jedis.close();
Configuration files, writing tools
public class JedisPoolUtils {
private static JedisPool jedisPool;
static {
// 加载配置文件
InputStream is = JedisPoolUtils.class.getClassLoader().getResourceAsStream("jedis.properties");
// 创建properties对象
Properties pro = new Properties();
// 关联文件
try {
pro.load(is);
} catch (IOException e) {
e.printStackTrace();
}
// 获取数据,配置到JedisPoolConfig中
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(Integer.parseInt(pro.getProperty("maxTotal")));
config.setMaxIdle(Integer.parseInt(pro.getProperty("maxIdle")));
// 初始化JedisPool
jedisPool = new JedisPool(config,pro.getProperty("host"),Integer.parseInt(pro.getProperty("port")));
}
public static Jedis getJedis() {
return jedisPool.getResource();
}
}
and then call
Jedis jedis = JedisPoolUtils.getJedis();
// 使用
jedis.set("abc","defg");
// 关闭,归还到连接池
jedis.close();
```Jedis jedis = JedisPoolUtils.getJedis();
// 使用
jedis.set("abc","defg");
// 关闭,归还到连接池
jedis.close();
# JQuery
jq和js之间的转换
```js
var $div1 =$("#div1");
alert(div1.html());
// js --> jq
var div2 = document.getElementsByTagName("div");
$(div2).html("aaa");
// jq --> js
$div1[0].innerHTML = "aaa";
$div1.get(0).innerHTML = "aaa";
JQuery
Conversion between jq and js
var $div1 =$("#div1");
alert(div1.html());
// js --> jq
var div2 = document.getElementsByTagName("div");
$(div2).html("aaa");
// jq --> js
$div1[0].innerHTML = "aaa";
$div1.get(0).innerHTML = "aaa";
Entry function & binding click event
// 入口函数
$(function () {
// 绑定单击事件
$("#bt").click(function () {
alert("abc");
});
});
style control
// 入口函数
$(function () {
// 样式控制
$("#div1").css("backgroundColor","pink");
});
AJAX
js implementation
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
// 建立连接
xhttp.open("GET","test1.txt",true)
// 发送请求
xhttp.send();
JQuery implementation
$.ajax({
url:"servlet",
type:"POST",
data:{
"username" : "jack", "age" : "22"},
success:function (data) {
alert(data);
},
error:function () {
alert("出错了");
},
dataType:"text"
})
Simplify get and post requests
$.get("servlet",{
"username" : "jack"}, function (data){
alert(data);
},"text");
$.post("servlet",{
"username" : "jack"}, function (data){
alert(data);
},"text");
JSON
define format
var index = {
"username":"张三",age : 22}
retrieve data
var name = index.username;
var name2 = index["username"];
java --> json
// 创建json对象
ObjectMapper mapper = new ObjectMapper();
// 转换
String json = mapper.writeValueAsString(user);
annotation:
@jsonIgnore // 忽略属性转换为json
@jsonFormat(pattern = "yyyy-MM-dd") // 格式化
Code examples
$(function () {
$("#username").blur(function () {
var username = $(this).val();
$.get("demoServlet",{
username:username},function (data) {
var span = $("#s_username");
if (data.userExist) {
span.css("color","red");
span.html(data.msg)
} else {
span.css("color","green");
span.html(data.msg)
}
},"json")