package com.book.controller;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import com.book.model.Person;
import com.book.service.PersonService;
@Controller
public class PersonController {
@Autowired
private PersonService personService;
public void setUserService(PersonService personService) {
this.personService = personService;
}
@RequestMapping("/person")
public ModelAndView person(){
Person person =personService.find(2);
String str= person.toString();
return new ModelAndView("message","str",str);
}
@RequestMapping("/personlist")
public ModelAndView personlist(){
List
person= new ArrayList
();
person=personService.FindAll();
JSONObject json=new JSONObject();
json.put("person", person);
return new ModelAndView("message","str",json);
}
@RequestMapping("/addPerson")
public ModelAndView addPerson(){
Person person = new Person();
person.setName("hehe");
person.setAge(22);
personService.addPerson(person);
return new ModelAndView("message","str","aa");
}
@RequestMapping("/getById")
public ModelAndView getById(){
Person person =personService.getById(2);
String str= person.toString();
return new ModelAndView("message","str",str);
}
}
PersonMapper
package com.book.dao;
import java.util.List;
import com.book.model.Person;
public interface PersonMapper {
public Person find(int id);
public List
FindAll();
public Person getByName(String name);
public void addPerson(Person person);
public Person getById(int id);
}
package com.book.model;
import java.io.Serializable;
public class Person implements Serializable{
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Person(){
super();
}
@Override
public String toString() {
return "Person [Id=" + id + ", Name=" + name
+ ", age=" + age + "]";
}
}
PersonService
package com.book.service;
import java.util.List;
import com.book.model.Person;
public interface PersonService {
public Person find(int id);
public List
FindAll();
public Person getByName(String name);
public void addPerson(Person person);
public Person getById(int id);
}
PersonServiceImpl
package com.book.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.book.dao.PersonMapper;
import com.book.model.Person;
import com.book.service.PersonService;
@Service
public class PersonServiceImpl implements PersonService {
@Autowired
private PersonMapper personDao;
public void setUserDao(PersonMapper personDao) {
this.personDao = personDao;
}
public Person find(int id) {
return personDao.find(id);
}
public List
FindAll() {
return personDao.FindAll();
}
public Person getByName(String name) {
return personDao.getByName(name);
}
public void addPerson(Person person) {
personDao.addPerson(person);
}
public Person getById(int id) {
return personDao.getById(id);
}
}
RedisCache
package com.book.util;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.ibatis.cache.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.jedis.JedisConnection;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import redis.clients.jedis.exceptions.JedisConnectionException;
/**
*
* @描述: 使用第三方内存数据库Redis作为二级缓存
* @版权: Copyright (c) 2016
* @作者:
* @版本: 1.0
* @创建日期:
* @创建时间:
*/
public class RedisCache implements Cache {
private static final Logger logger = LoggerFactory.getLogger(RedisCache.class);
private static JedisConnectionFactory jedisConnectionFactory;
private final String id;
/**
* The {@code ReadWriteLock}.
*/
private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock();
public RedisCache(final String id) {
if (id == null) {
throw new IllegalArgumentException("Cache instances require an ID");
}
logger.debug("MybatisRedisCache:id=" + id);
this.id = id;
}
public void clear() {
JedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
connection.flushDb();
connection.flushAll();
} catch (JedisConnectionException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.close();
}
}
}
public String getId() {
return this.id;
}
public Object getObject(Object key) {
Object result = null;
JedisConnection connection = null;
try {
connection = jedisConnectionFactory.getConnection();
RedisSerializer