【java web】 Redis & Jedis 简单入门 (JQuery+AJAX+JSON)

Redis

缓冲在内存中,持久化在硬盘里,非关系型数据库,数据类型为键值对

Jedis

java操作redis

步骤

 // 创建
Jedis jedis = new Jedis("localhost",6379);

// 操作
jedis.set("username","zhangsan");

// 关闭
jedis.close();

连接池 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();

配置文件,写工具类

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();
    }
}

然后调用

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

jq和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";

入口函数&绑定单击事件

// 入口函数
$(function () {
    
    
    // 绑定单击事件
    $("#bt").click(function () {
    
    
        alert("abc");
    });
});

样式控制

// 入口函数
$(function () {
    
    
    // 样式控制
    $("#div1").css("backgroundColor","pink");

});

AJAX

js实现

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实现

$.ajax({
    
    
    url:"servlet",
    type:"POST",
    data:{
    
    "username" : "jack", "age" : "22"},
    success:function (data) {
    
    
        alert(data);
    },
    error:function () {
    
    
        alert("出错了");

    },
    dataType:"text"
})

简化get和post请求

$.get("servlet",{
    
    "username" : "jack"}, function (data){
    
    
    alert(data);
},"text");
$.post("servlet",{
    
    "username" : "jack"}, function (data){
    
    
    alert(data);
},"text");

JSON

定义格式

var index = {
    
    "username":"张三",age : 22}

获取数据

var name = index.username;
var name2 = index["username"];

java --> json

// 创建json对象
ObjectMapper mapper  = new ObjectMapper();

// 转换
String json = mapper.writeValueAsString(user);

注解:

@jsonIgnore // 忽略属性转换为json
@jsonFormat(pattern = "yyyy-MM-dd") // 格式化

代码案例

$(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")

猜你喜欢

转载自blog.csdn.net/weixin_62726289/article/details/122780228
今日推荐