diff --git a/springboot-mybatis-redis/pom.xml b/springboot-mybatis-redis/pom.xml new file mode 100755 index 0000000..6c62d49 --- /dev/null +++ b/springboot-mybatis-redis/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + + springboot + springboot-mybatis-redis + 0.0.1-SNAPSHOT + springboot-mybatis :: 整合 Mybatis 并使用 Redis 作为缓存 + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.1.RELEASE + + + + 1.2.0 + 5.1.39 + 1.3.2.RELEASE + + + + + + + org.springframework.boot + spring-boot-starter-redis + ${spring-boot-starter-redis-version} + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + ${mybatis-spring-boot} + + + + + mysql + mysql-connector-java + ${mysql-connector} + + + + + junit + junit + 4.12 + + + diff --git a/springboot-mybatis-redis/src/main/java/org/spring/springboot/Application.java b/springboot-mybatis-redis/src/main/java/org/spring/springboot/Application.java new file mode 100644 index 0000000..79807ed --- /dev/null +++ b/springboot-mybatis-redis/src/main/java/org/spring/springboot/Application.java @@ -0,0 +1,26 @@ +package org.spring.springboot; + +import org.mybatis.spring.annotation.MapperScan; +import org.spring.springboot.dao.CityDao; +import org.spring.springboot.domain.City; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Spring Boot 应用启动类 + * + * Created by bysocket on 16/4/26. + */ +// Spring Boot 应用的标识 +@SpringBootApplication +// mapper 接口类扫描包配置 +@MapperScan("org.spring.springboot.dao") +public class Application { + + public static void main(String[] args) { + // 程序启动入口 + // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 + SpringApplication.run(Application.class,args); + } +} diff --git a/springboot-mybatis-redis/src/main/java/org/spring/springboot/controller/CityRestController.java b/springboot-mybatis-redis/src/main/java/org/spring/springboot/controller/CityRestController.java new file mode 100644 index 0000000..a806401 --- /dev/null +++ b/springboot-mybatis-redis/src/main/java/org/spring/springboot/controller/CityRestController.java @@ -0,0 +1,40 @@ +package org.spring.springboot.controller; + +import org.spring.springboot.domain.City; +import org.spring.springboot.service.CityService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * Created by bysocket on 07/02/2017. + */ +@RestController +public class CityRestController { + + @Autowired + private CityService cityService; + + + @RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET) + public City findOneCity(@PathVariable("id") Long id) { + return cityService.findCityById(id); + } + + @RequestMapping(value = "/api/city", method = RequestMethod.POST) + public void createCity(@RequestBody City city) { + cityService.saveCity(city); + } + + @RequestMapping(value = "/api/city", method = RequestMethod.PUT) + public void modifyCity(@RequestBody City city) { + cityService.updateCity(city); + } + + @RequestMapping(value = "/api/city/{id}", method = RequestMethod.DELETE) + public void modifyCity(@PathVariable("id") Long id) { + cityService.deleteCity(id); + } +} diff --git a/springboot-mybatis-redis/src/main/java/org/spring/springboot/dao/CityDao.java b/springboot-mybatis-redis/src/main/java/org/spring/springboot/dao/CityDao.java new file mode 100644 index 0000000..22f2654 --- /dev/null +++ b/springboot-mybatis-redis/src/main/java/org/spring/springboot/dao/CityDao.java @@ -0,0 +1,35 @@ +package org.spring.springboot.dao; + +import org.apache.ibatis.annotations.Param; +import org.spring.springboot.domain.City; + +import java.util.List; + +/** + * 城市 DAO 接口类 + * + * Created by bysocket on 07/02/2017. + */ +public interface CityDao { + + /** + * 获取城市信息列表 + * + * @return + */ + List findAllCity(); + + /** + * 根据城市 ID,获取城市信息 + * + * @param id + * @return + */ + City findById(@Param("id") Long id); + + Long saveCity(City city); + + Long updateCity(City city); + + Long deleteCity(Long id); +} diff --git a/springboot-mybatis-redis/src/main/java/org/spring/springboot/domain/City.java b/springboot-mybatis-redis/src/main/java/org/spring/springboot/domain/City.java new file mode 100644 index 0000000..ed2e6e7 --- /dev/null +++ b/springboot-mybatis-redis/src/main/java/org/spring/springboot/domain/City.java @@ -0,0 +1,75 @@ +package org.spring.springboot.domain; + +import java.io.Serializable; + +/** + * 城市实体类 + * + * Created by bysocket on 07/02/2017. + */ +public class City implements Serializable { + + private static final long serialVersionUID = -1L; + + /** + * 城市编号 + */ + private Long id; + + /** + * 省份编号 + */ + private Long provinceId; + + /** + * 城市名称 + */ + private String cityName; + + /** + * 描述 + */ + private String description; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getProvinceId() { + return provinceId; + } + + public void setProvinceId(Long provinceId) { + this.provinceId = provinceId; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @Override + public String toString() { + return "City{" + + "id=" + id + + ", provinceId=" + provinceId + + ", cityName='" + cityName + '\'' + + ", description='" + description + '\'' + + '}'; + } +} diff --git a/springboot-mybatis-redis/src/main/java/org/spring/springboot/service/CityService.java b/springboot-mybatis-redis/src/main/java/org/spring/springboot/service/CityService.java new file mode 100644 index 0000000..8d5dcd0 --- /dev/null +++ b/springboot-mybatis-redis/src/main/java/org/spring/springboot/service/CityService.java @@ -0,0 +1,44 @@ +package org.spring.springboot.service; + +import org.spring.springboot.domain.City; + +import java.util.List; + +/** + * 城市业务逻辑接口类 + * + * Created by bysocket on 07/02/2017. + */ +public interface CityService { + /** + * 根据城市 ID,查询城市信息 + * + * @param id + * @return + */ + City findCityById(Long id); + + /** + * 新增城市信息 + * + * @param city + * @return + */ + Long saveCity(City city); + + /** + * 更新城市信息 + * + * @param city + * @return + */ + Long updateCity(City city); + + /** + * 根据城市 ID,删除城市信息 + * + * @param id + * @return + */ + Long deleteCity(Long id); +} diff --git a/springboot-mybatis-redis/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java b/springboot-mybatis-redis/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java new file mode 100644 index 0000000..3f0bb41 --- /dev/null +++ b/springboot-mybatis-redis/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java @@ -0,0 +1,104 @@ +package org.spring.springboot.service.impl; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.spring.springboot.dao.CityDao; +import org.spring.springboot.domain.City; +import org.spring.springboot.service.CityService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.data.redis.core.ValueOperations; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * 城市业务逻辑实现类 + *

+ * Created by bysocket on 07/02/2017. + */ +@Service +public class CityServiceImpl implements CityService { + + private static final Logger LOGGER = LoggerFactory.getLogger(CityServiceImpl.class); + + @Autowired + private CityDao cityDao; + + @Autowired + private RedisTemplate redisTemplate; + + /** + * 获取城市逻辑: + * 如果缓存存在,从缓存中获取城市信息 + * 如果缓存不存在,从 DB 中获取城市信息,然后插入缓存 + */ + public City findCityById(Long id) { + // 从缓存中获取城市信息 + String key = "city_" + id; + ValueOperations operations = redisTemplate.opsForValue(); + + // 缓存存在 + boolean hasKey = redisTemplate.hasKey(key); + if (hasKey) { + City city = operations.get(key); + + LOGGER.info("CityServiceImpl.findCityById() : 从缓存中获取了城市 >> " + city.toString()); + return city; + } + + // 从 DB 中获取城市信息 + City city = cityDao.findById(id); + + // 插入缓存 + operations.set(key, city, 10, TimeUnit.SECONDS); + LOGGER.info("CityServiceImpl.findCityById() : 城市插入缓存 >> " + city.toString()); + + return city; + } + + @Override + public Long saveCity(City city) { + return cityDao.saveCity(city); + } + + /** + * 更新城市逻辑: + * 如果缓存存在,删除 + * 如果缓存不存在,不操作 + */ + @Override + public Long updateCity(City city) { + Long ret = cityDao.updateCity(city); + + // 缓存存在,删除缓存 + String key = "city_" + city.getId(); + boolean hasKey = redisTemplate.hasKey(key); + if (hasKey) { + redisTemplate.delete(key); + + LOGGER.info("CityServiceImpl.updateCity() : 从缓存中删除城市 >> " + city.toString()); + } + + return ret; + } + + @Override + public Long deleteCity(Long id) { + + Long ret = cityDao.deleteCity(id); + + // 缓存存在,删除缓存 + String key = "city_" + id; + boolean hasKey = redisTemplate.hasKey(key); + if (hasKey) { + redisTemplate.delete(key); + + LOGGER.info("CityServiceImpl.deleteCity() : 从缓存中删除城市 ID >> " + id); + } + return ret; + } + +} diff --git a/springboot-mybatis-redis/src/main/resources/application.properties b/springboot-mybatis-redis/src/main/resources/application.properties new file mode 100644 index 0000000..209b80c --- /dev/null +++ b/springboot-mybatis-redis/src/main/resources/application.properties @@ -0,0 +1,29 @@ +## 数据源配置 +spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8 +spring.datasource.username=root +spring.datasource.password=123456 +spring.datasource.driver-class-name=com.mysql.jdbc.Driver + +## Mybatis 配置 +mybatis.typeAliasesPackage=org.spring.springboot.domain +mybatis.mapperLocations=classpath:mapper/*.xml + +## Redis 配置 +## Redis数据库索引(默认为0) +spring.redis.database=0 +## Redis服务器地址 +spring.redis.host=127.0.0.1 +## Redis服务器连接端口 +spring.redis.port=6379 +## Redis服务器连接密码(默认为空) +spring.redis.password= +## 连接池最大连接数(使用负值表示没有限制) +spring.redis.pool.max-active=8 +## 连接池最大阻塞等待时间(使用负值表示没有限制) +spring.redis.pool.max-wait=-1 +## 连接池中的最大空闲连接 +spring.redis.pool.max-idle=8 +## 连接池中的最小空闲连接 +spring.redis.pool.min-idle=0 +## 连接超时时间(毫秒) +spring.redis.timeout=0 \ No newline at end of file diff --git a/springboot-mybatis-redis/src/main/resources/mapper/CityMapper.xml b/springboot-mybatis-redis/src/main/resources/mapper/CityMapper.xml new file mode 100644 index 0000000..47528bf --- /dev/null +++ b/springboot-mybatis-redis/src/main/resources/mapper/CityMapper.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + id, province_id, city_name, description + + + + + + + + insert into + city(id,province_id,city_name,description) + values + (#{id},#{provinceId},#{cityName},#{description}) + + + + update + city + set + + province_id = #{provinceId}, + + + city_name = #{cityName}, + + + description = #{description} + + where + id = #{id} + + + + delete from + city + where + id = #{id} + + +