三十四SpringBoot Redisの統合(組み合わせSpringBoot統合MyBatisの)
34.0ケースRedisの統合の単一バージョンを達成することです
34.1依存に関連します
1 < 依存性> 2 < のgroupId > org.springframework.boot </ groupIdを> 3 < たartifactId >ばねブートスタータウェブ</ たartifactId > 4 </ 依存> 5 6 <! - MyBatisの(注解方式)- > 7 < 依存性> 8 < のgroupId > org.mybatis.spring.boot </ groupIdを> 9 < たartifactId > MyBatisのスプリングブートスタータ</ たartifactId > 10 < バージョン> 1.3.1 </ バージョン> 11 </ 依存> 12 13 <! - MySQLの- > 14 < 依存性> 15 < のgroupId > MySQLの</ groupIdを> 16 < たartifactId >のMySQLコネクタ-java </ たartifactId > 17 </ 依存> 18 19 <! - Redisの依赖- > 20 <依存> 21 < のgroupId > org.springframework.boot </ groupIdを> 22 < たartifactId >ばねブートスタータデータRedisの</ たartifactId > 23 </ 依存>
34.2 MySQL設定とRedisの
1 #数据库配置 2 spring.datasource.driverクラス名=はcom.mysql.jdbc.Driver 3 spring.datasource.username =ルート 4 spring.datasource.password = 5 = JDBC spring.datasource.url:MySQLの:// localhost:3306 / springboot 6 7 #redis单服务器配置 8 spring.redis.database = 0 9 spring.redis.host = 127.0.0.1 10 spring.redis.port = 6379 11 spring.redis.pool.max活性= 8 12 spring.reids.pool.maxウェイト= -1 13 spring.redis.pool.maxアイドル= 8 14 spring.redis.pool.minアイドル= 0 15 spring.redis.timeout = 0 16
34.3は、キャッシュを開きます
起動クラスに追加しました@EnableCaching コメント
1つの パッケージcom.wu.app。 2 3 インポートorg.mybatis.spring.annotation.MapperScan。 4 インポートorg.springframework.boot.SpringApplication。 5 輸入org.springframework.boot.autoconfigure.SpringBootApplication。 6 インポートorg.springframework.cache.annotation.EnableCaching。 7 8 9 @SpringBootApplication(scanBasePackages = { "com.wu.controller"、 "com.wu.service"}) 10 @MapperScan( "com.wu.mapper")//需要单独扫描 11 @ EnableCaching //开启缓存 12 パブリッククラスSpringApplications { 13 //程序启动入口 14 のpublic static無効メイン(文字列[] args){ 15 SpringApplication.run(SpringApplications.class、引数)。 16 } 17 }
サービス層のメソッドに追加するにキャッシュする必要があります@Cacheableコメント
1つの パッケージcom.wu.service。 2 3 インポートorg.springframework.beans.factory.annotation.Autowired。 4 インポートorg.springframework.cache.annotation.Cacheable。 5 輸入org.springframework.stereotype.Service。 6 7 インポートcom.wu.mapper.UsersMapper。 8 インポートcom.wu.pojo.Users。 9 @Service 10 パブリック クラス UsersServiceImpが実装UsersService { 11 @Autowired 12 プライベートUsersMapperマッパーと、 13 14 @Cacheable(値= "ユーザー")//のキーセット 15 @Override 16 公衆selectByName(文字列名){ユーザー 17 のSystem.out.println( "データベースから探す" )、 18は 戻りmapper.selectByName(名); 19 } 20は、 21である 22れます }
シリアルインタフェースを達成するためのエンティティ必要が Serializableを実装します
1人の パブリック クラスユーザー実装シリアライズ{ 2 プライベート整数IDと、 3 4 プライベート文字列名; 5 6 プライベート文字列のパスワード。 7 8 プライベート文字列のメール。 9 10 プライベート日の誕生日。 11 12 公共の整数のgetId(){ 13 リターンID。 14 } 15 16 公共 ボイドSETID(整数ID){ 17 この .ID = ID。 18 } 19 20 公共の文字列のgetName(){ 21 リターン名。 22 } 23 24 公共の ボイドのsetName(文字列名){ 25 この .nameの=名== NULL?ヌル:name.trim(); 26 } 27 28 公共の文字列getPasswordに(){ 29 リターンパスワード。 30 } 31 32 公共 ボイドするsetPassword(文字列のパスワード){ 33 このみましょう。パスワード=パスワード== NULL?ヌル:password.trim(); 34 } 35 36 公衆ストリングgetEmail(){ 37 リターンメール、 38 } 39 40 公共 ボイドsetEmail(文字列のメール){ 41 この .email =メール== NULL?ヌル:email.trim(); 42 } 43 44 公衆日付getBirthday(){ 45 リターン誕生。 46 } 47 48 公共 のボイドsetBirthday(日誕生日){ 49 本.birthday = 誕生日。 50 }
34.4結果
これは、データベースクエリから、このようなクエリ、再びキャッシュからロードされたクエリは初めてです