数据的查询:
先将数据库中的数据存储到Redis中
球队表的数据:
球队表的存储:
球队表的测试代码:
@Test
public void testqueryRedis() {
Jedis jedis = new Jedis("192.168.100.132", 6379);
//查询部门的id集合
List list = jedis.lrange("team:team_id", 0, -1);
List dList = new ArrayList();
for(String teamId : list){
//获得team_id
String tname = jedis.hget("team:"+teamId, "t_name");
String loc = jedis.hget("team:"+teamId, "loc");
Dept dept = new Dept();
dept.setDeptId(new Integer(teamId));
dept.setDname(tname);
dept.setLoc(loc);
dList.add(dept);
}
System.out.println(dList);
jedis.close();
}
人员表的数据:
人员表的存储:
人员表的测试代码:
@Test
public void testqueryRedis1() {
Jedis jedis = new Jedis("192.168.100.132", 6379);
String empId = jedis.hget("emp:1", "emp_id");
String name = jedis.hget("emp:1", "ename");
String gender = jedis.hget("emp:1", "gender");
String teamId = jedis.hget("emp:1", "team_id");
Emp emp = new Emp();
emp.setEmpId(new Integer(empId));
emp.setEname(name);
emp.setGender(new Integer(gender));
//获得部门信息
String tname = jedis.hget("team:"+teamId, "t_name");
String loc = jedis.hget("team:"+teamId, "loc");
Dept dept = new Dept();
dept.setDeptId(new Integer(teamId));
dept.setDname(tname);
dept.setLoc(loc);
emp.setDept(dept);
jedis.close();
}
球队跟员工是一对多的关系, 以上的是从多对一的方向来看的, 下面介绍从一对多的方向来看
@Test
public void testqueryRedis2() {
Jedis jedis = new Jedis("192.168.100.132", 6379);
//获得部门信息
String tname = jedis.hget("team:1", "t_name");
String loc = jedis.hget("team:1", "loc");
Dept dept = new Dept();
dept.setDeptId(new Integer(1));
dept.setDname(tname);
dept.setLoc(loc);
List list = jedis.lrange("team:1:emp", 0, -1);
Set set = new HashSet();
for(String empId : list){
String name = jedis.hget("emp:"+empId, "ename");
String gender = jedis.hget("emp:"+empId, "gender");
String teamId = jedis.hget("emp:"+empId, "team_id");
Emp emp = new Emp();
emp.setEmpId(new Integer(empId));
emp.setEname(name);
emp.setGender(new Integer(gender));
set.add(emp);
}
dept.setEmpSet(set);
jedis.close();
}
数据的同步:
关系型数据库中的数据:
同步代码:
@Test
public void importRedis(){
SessionFactory factory = HiberUtils.getSessionFactory();
Session session = factory.openSession();
Jedis jedis = new Jedis("192.168.100.132", 6379);
try {
//查询时不但会查询出自己的数据,还会查询出关联的数据
Dept dept = (Dept) session.get(Dept.class, 1);
//dept:dept_id
jedis.hset("dept:"+dept.getDeptId(), "dept_id", dept.getDeptId()+"");
jedis.hset("dept:"+dept.getDeptId(), "dname", dept.getDname()+"");
jedis.hset("dept:"+dept.getDeptId(), "loc", dept.getLoc()+"");
Set empSet = dept.getEmpSet();
for(Emp emp : empSet){
jedis.hset("emp:"+emp.getEmpId(), "emp_id", emp.getEmpId()+"");
jedis.hset("emp:"+emp.getEmpId(), "ename", emp.getEname()+"");
jedis.hset("emp:"+emp.getEmpId(), "gender", emp.getGender()+"");
jedis.hset("emp:"+emp.getEmpId(), "birthday", new SimpleDateFormat("yyyy-MM-dd").format(emp.getBirthday())+"");
jedis.hset("emp:"+emp.getEmpId(), "dept_id", emp.getDept().getDeptId()+"");
jedis.lpush("dept:"+dept.getDeptId()+":emp", emp.getEmpId()+"");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
session.close();
}
}