redis在项目中的运用

controller代码

@Autowired

private RedisService redisService;

// 存入redis缓存 将用户数据都存到缓存中,便于快速查询

redisService.initializationAccount(pd);

//redis取值

redisService.getAccont(pd.getString("uid"));//从redis中取值

1

2

3

4

5

6

7

service层

@Service("RedisService")

public class RedisService {

@Autowired

private JedisPool jedisPool;

public void setAccount(String key,UserAccount accont){

if(StringUtils.isNotBlank(key) && accont != null){

RedisUtils redisUtils = new RedisUtils(jedisPool);

redisUtils.setObject("U"+key, accont, 0);

}

}

public UserAccount getAccont(String id){

UserAccount accont;

RedisUtils redisUtils = new RedisUtils(jedisPool);

accont = (UserAccount)redisUtils.getObject("U"+id);

if(accont == null ){

accont = new UserAccount();

}

return accont;

}

public UserAccount getAccont2(String id){

RedisUtils redisUtils = new RedisUtils(jedisPool);

return (UserAccount)redisUtils.getObject("U"+id);

}

public Integer setLong(Object value){

Long long1 = (Long)value;

String string = long1.toString();

Integer integer = Integer.parseInt(string);

return integer;

}

@Autowired

private IntRedisAccountService accountService;

public UserAccount initializationAccount(PageData pd){

UserAccount account = new UserAccount();

try {

//注释:此处代码是从数据库取出需要存的值(按需求定)

PageData pageData = accountService.redisAccountList(pd);

//account.setU_id(pageData.getString("u_id"));

account.setID(pageData.getString("ID"));

account.setHx_id(pageData.getString("hx_id"));

account.setNick_name(pageData.getString("nick_name"));

account.setName(pageData.getString("name"));

account.setPassword(pageData.getString("password"));

account.setId_card(pageData.getString("id_card"));

account.setPhone(pageData.getString("phone"));

account.setPic_url(pageData.getString("pic_url"));

account.setLastlogin_time(pageData.getString("lastlogin_time"));

account.setScode(pageData.getString("scode"));

account.setCountry(pageData.getString("country"));

account.setReferee(pageData.getString("referee"));

account.setRefereenum(pageData.getLong("refereenum").intValue());//

account.setWalletaddress(pageData.getString("walletaddress"));

account.setRegistertime(pageData.getString("registertime"));

account.setStatus(pageData.getLong("status").intValue());//

account.setAssets(pageData.getFloat("assets"));//

account.setColdassets(pageData.getFloat("coldassets"));

account.setHotassets(pageData.getFloat("hotassets"));

account.setUsertype(pageData.getLong("usertype").intValue());//long转换成integer

account.setRegisterip(pageData.getString("registerip"));

account.setLastloginip(pageData.getString("lastloginip"));

account.setAuditstatus(pageData.getLong("auditstatus").intValue());//

account.setAudituid(pageData.getInteger("audituid"));

account.setAudittime(pageData.getString("audittime"));

setAccount(pageData.getString("u_id"), account);

} catch (Exception e) {

e.printStackTrace();

}

return account;

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

dao层

/*

*列表(全部)

*/

public PageData redisAccountList(PageData pd)throws Exception{

return (PageData)dao.findForObject("RedisAccountMapper.RedisAccount", pd);

}

1

2

3

4

5

6

mapper

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="RedisAccountMapper">

<select id="RedisAccount" parameterType="pd" resultType="pd">

SELECT

IFNULL((SELECT u_id FROM bit_user WHERE u_id= #{u_id}),0) AS u_id,

IFNULL((SELECT ID FROM bit_user WHERE u_id= #{u_id}),0) AS ID,

IFNULL((SELECT hx_id FROM bit_user WHERE u_id= #{u_id}),0) AS hx_id,

IFNULL((SELECT nick_name FROM bit_user WHERE u_id= #{u_id}),0) AS nick_name,

IFNULL((SELECT name FROM bit_user WHERE u_id= #{u_id}),0) AS name,

IFNULL((SELECT password FROM bit_user WHERE u_id= #{u_id}),0) AS password,

IFNULL((SELECT id_card FROM bit_user WHERE u_id= #{u_id}),0) AS id_card,

IFNULL((SELECT phone FROM bit_user WHERE u_id= #{u_id}),0) AS phone,

IFNULL((SELECT pic_url FROM bit_user WHERE u_id= #{u_id}),0) AS pic_url,

IFNULL((SELECT lastlogin_time FROM bit_user WHERE u_id= #{u_id}),0) AS lastlogin_time,

IFNULL((SELECT scode FROM bit_user WHERE u_id= #{u_id}),0) AS scode,

IFNULL((SELECT country FROM bit_user WHERE u_id= #{u_id}),0) AS country,

IFNULL((SELECT referee FROM bit_user WHERE u_id= #{u_id}),0) AS referee,

IFNULL((SELECT refereenum FROM bit_user WHERE u_id= #{u_id}),0) AS refereenum,

IFNULL((SELECT walletaddress FROM bit_user WHERE u_id= #{u_id}),0) AS walletaddress,

IFNULL((SELECT registertime FROM bit_user WHERE u_id= #{u_id}),0) AS registertime,

IFNULL((SELECT status FROM bit_user WHERE u_id= #{u_id}),0) AS status,

IFNULL((SELECT assets FROM bit_user WHERE u_id= #{u_id}),0) AS assets,

IFNULL((SELECT coldassets FROM bit_user WHERE u_id= #{u_id}),0) AS coldassets,

IFNULL((SELECT hotassets FROM bit_user WHERE u_id= #{u_id}),0) AS hotassets,

IFNULL((SELECT usertype FROM bit_user WHERE u_id= #{u_id}),0) AS usertype,

IFNULL((SELECT registerip FROM bit_user WHERE u_id= #{u_id}),0) AS registerip,

IFNULL((SELECT lastloginip FROM bit_user WHERE u_id= #{u_id}),0) AS lastloginip,

IFNULL((SELECT auditstatus FROM bit_user WHERE u_id= #{u_id}),0) AS auditstatus,

IFNULL((SELECT audituid FROM bit_user WHERE u_id= #{u_id}),0) AS audituid,

IFNULL((SELECT audittime FROM bit_user WHERE u_id= #{u_id}),0) AS audittime

</select>

</mapper>

redis在项目中的运用

相关推荐