Next, use Redis for caching
Create a new spring boot project, add a pom reference
<dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring- boot-starter-data-redis</artifactid> </dependency> <dependency> <groupId> Org.mybatis.spring.boot</groupid> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupid>c Om.alibaba</groupid> <artifactId>druid-spring-boot-starter</artifactId> <versio n>1.1.6</version> </dependency> <dependency> <groupid>org.springframew Ork.boot</groupid> <artifactId>spring-boot-starter-web</artifactId> </dependency> ; <!--https://Mvnrepository.com/artifact/com.google.code.gson/gson --<dependency> <groupId>com.google.code.gson</groupId> <artifactid>gson</ar tifactid> <version>2.2.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> &L T;scope>runtime</scope> </dependency>
View Code
User class
Public classUser {PrivateInteger ID; PrivateString name; PrivateInteger sex; PrivateInteger age; PublicUser (String name, integer sex, integer age) { This. Name =name; This. Sex =sex; This. Age =Age ; } PublicUser (integer ID, String name, Integer sex, integer age) { This. ID =ID; This. Name =name; This. Sex =sex; This. Age =Age ; } PublicUser () {} PublicInteger getId () {returnID; } Public voidsetId (Integer id) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } PublicInteger Getsex () {returnsex; } Public voidsetsex (Integer sex) { This. Sex =sex; } PublicInteger getage () {returnAge ; } Public voidsetage (Integer age) { This. Age =Age ; }}
View Code
Userrepository interface
import org.apache.ibatis.annotations.*; @Mapperpublicinterface userrepository { @Select ("select * from person where id=#{id}") User FindByID (@Param ("id") Integer ID); @Insert ("Insert into person (name,sex,age) VALUES (#{name},#{sex},#{age})") user Save (user user); @Update ("Update person SET name=#{name},sex=#{sex},age=#{age} WHERE Id=#{id}") user Update (user user) ; @Delete ("Delete from person WHERE id=#{id}") void Delete (Integer ID);}
View Code
Userredis
ImportCom.google.gson.Gson;ImportCom.google.gson.reflect.TypeToken;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.data.redis.core.RedisTemplate;Importorg.springframework.stereotype.Repository;Importorg.springframework.util.StringUtils;Importjava.util.List;Importjava.util.concurrent.TimeUnit; @Repository Public classUserredis {@AutowiredPrivateRedistemplate<string,string>redistemplate; Public voidAdd (String key,long time,user User) {Gson Gson=NewGson (); Redistemplate.opsforvalue (). Set (Key,gson.tojson (user), time, timeunit.minutes); } Public voidAdd (String key, Long time, list<user>users) {Gson Gson=NewGson (); Redistemplate.opsforvalue (). Set (Key,gson.tojson (users), time, timeunit.minutes); } PublicUser get (String key) {Gson Gson=NewGson (); User User=NULL; String Userjson=Redistemplate.opsforvalue (). get (key); if(!Stringutils.isempty (Userjson)) {User=gson.fromjson (Userjson,user.class); } returnuser; } PublicList<user>getList (String key) {Gson Gson=NewGson (); List<User> users=NULL; String Listjson=Redistemplate.opsforvalue (). get (key); if(!Stringutils.isempty (Listjson)) {Users=gson.fromjson (Listjson,NewTypetoken<list<user>>() {}.gettype ()); } returnusers; } Public voidDelete (String key) {Redistemplate.opsforvalue (). GetOperations (). Delete (key); }}
View Code
UserService
Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service; @Service Public classUserService {@AutowiredPrivateuserrepository userrepository; @AutowiredPrivateUserredis Userredis; Private Static FinalString keyhead= "Mysql:get:user:"; Publicuser FindByID (Integer ID) {User user=userredis.get (keyhead+ID); if(user==NULL) {User=Userrepository.findbyid (ID); if(user!=NULL) {Userredis.add (Keyhead+id,30l, user); } } returnuser; } PublicUser Create (user user) {User NewUser=userrepository.save (user); if(newuser!=NULL) {Userredis.add (Keyhead+newuser.getid (), 30L, NewUser); } returnNewUser; } PublicUser update (user user) {if(user!=NULL) {userredis.delete (Keyhead+User.getid ()); Userredis.add (Keyhead+user.getid (), 30L, user); } returnuserrepository.update (user); } Public voidDelete (Integer id) {userredis.delete (Keyhead+ID); Userrepository.delete (ID); }}
View Code
Redisconfig
ImportOrg.springframework.cache.CacheManager;ImportOrg.springframework.cache.annotation.CachingConfigurerSupport;Importorg.springframework.cache.annotation.EnableCaching;ImportOrg.springframework.context.annotation.Bean;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.data.redis.cache.RedisCacheManager;Importorg.springframework.data.redis.core.RedisTemplate, @Configuration @enablecaching Public classRedisconfigextendsCachingconfigurersupport {@Bean PublicCacheManager CacheManager (@SuppressWarnings ("Rawtypes") Redistemplate redistemplate) {Rediscachemanager Manager=NewRediscachemanager (redistemplate); Manager.setdefaultexpiration (43200); returnManager; }}
View Code
Usercontroller
Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController; @RestController Public classUsercontroller {@AutowiredPrivateUserService UserService; @RequestMapping (Value= "/user/{id}") PublicString Show (@PathVariable Integer ID) {User user=Userservice.findbyid (ID); returnUser.getname (); }}
View Code
Application.propeties Configuration
Spring.datasource.url= Jdbc:mysql://192.168.31.146:3306/mydbSpring.datasource.username=Rootspring.datasource.password=Passspring.redis.host=192.168.31.146Spring.redis.port=6379Spring.datasource.druid.url= Jdbc:mysql://192.168.31.146:3306/mydbSpring.datasource.druid.username=Rootspring.datasource.druid.password=passspring.datasource.druid.initial-size=5Spring.datasource.druid.max-active=20Spring.datasource.druid.min-idle=5Spring.datasource.druid.max-wait=60000Spring.datasource.druid.pool-prepared-statements=trueSpring.datasource.druid.max-pool-prepared-statement-per-connection-size=20#spring. Datasource.druid.max-open-prepared-statements=spring.datasource.druid.validation-query=select 1From dual#spring.datasource.druid.validation-query-timeout=spring.datasource.druid.test-on-borrow=falsespring.datasource.druid.test-on-return=falsespring.datasource.druid.test- while-idle=trueSpring.datasource.druid.time-between-eviction-runs-millis=60000Spring.datasource.druid.min-evictable-idle-time-millis=300000#spring. Datasource.druid.max-evictable-idle-time-millis=#配置多个英文逗号分隔spring. Datasource.druid.filters=stat,wall,log4j
View Code
Turn on Druid monitoring sql:http://localhost:8080/druid/sql.html
Starting a docker Redis image, configuring 6379 port Mappings
Docker run-d-P 6379:6379 Redis
Open Redisdesktopmanager Client View data
Open page: HTTP://LOCALHOST:8080/USER/1
You can see that Druid has a query
Data is also available in Redis
Several times to refresh the page, you can see no more to check the database, Druid monitoring only one SQL
Spring boot + mybatis + Druid + Redis