14 springboot中使用ehcache、mybatisgenerator和logback

这两个其实都特别简单,但是还是记一下吧。

集成ehcache

首先添加依赖

    
        org.springframework
        spring-context-support
    

    
        net.sf.ehcache
        ehcache
    

然后写加入配置文件,我这里放在resource下面

14 springboot中使用ehcache、mybatisgenerator和logback_第1张图片
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;  
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;  
import org.springframework.context.annotation.Bean;  
import org.springframework.context.annotation.Configuration;  
import org.springframework.core.io.ClassPathResource;  
   
@Configuration
@EnableCaching
public class EhCacheConfiguration {
 
/** 
 * ehcache 主要的管理器
 * @param bean
 * @return 
 */  
@Bean  
public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
   System.out.println("CacheConfiguration.ehCacheCacheManager()");  
   return new EhCacheCacheManager(bean.getObject());
}  
 
/**
   * 据shared与否的设置, 
   * Spring分别通过CacheManager.create() 
   * 或new CacheManager()方式来创建一个ehcache基地. 
   * 也说是说通过这个来设置cache的基地是这里的Spring独用,还是跟别的(如hibernate的Ehcache共享) 
   * 
*/
  @Bean  
  public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){  
    System.out.println("CacheConfiguration.ehCacheManagerFactoryBean()");  
    EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();  
    cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("ehcache.xml"));
    cacheManagerFactoryBean.setShared(true);  
    return cacheManagerFactoryBean;  
  }  
}  

主要的配置文件ehcache.xml


  
     
     
    

    
    
  

看其中的usercache,之后再Service中的getUser加上头@Cacheable(value=USER_CACHE_NAME,key="'user_'+#id")

@Cacheable(value=USER_CACHE_NAME,key="'user_'+#id")
public User getUser(Long id) {
    System.out.println("没有走缓存");
    return  userMapper.getUser(id);
}

测试类中添加

@org.junit.Test
public void testa() throws Exception {
    System.out.println(userService.getUser(1L).getId());
    System.out.println(userService.getUser(1L).getId());
}

运行之后会出现第一次没有走缓存,第二次走缓存了。


加入@CacheEvict(value = USER_CACHE_NAME,key="'user_'+#id")去掉缓存

@CacheEvict(value = USER_CACHE_NAME,key="'user_'+#id")
public void deleteUser(long id){

}

测试再调用

@org.junit.Test
public void testa() throws Exception {
    System.out.println(userService.getUser(1L).getId());
    userService.deleteUser(1L);
    System.out.println(userService.getUser(1L).getId());
}

运行就会


集成mybatisgenerator

用完这个之后发现简化了CRUD的dao层。
在resource目录下加上generatorConfig.xml






    
    
    

    
    
        
        
            
            
        

        
        

        
        
            
            
        

        
        

        
        
            
        


        
        

最主要的还是在pom文件中加上


    
        
            org.mybatis.generator
            mybatis-generator-maven-plugin
            1.3.2
            
                src/main/resources/generatorConfig.xml
                true
                true
            
            
                
                    Generate MyBatis Artifacts
                    
                        generate
                    
                
            
            
                
                    org.mybatis.generator
                    mybatis-generator-core
                    1.3.2
                
            
        
    

具体的解释都写在了generatorConfig.xml,最后通过点击


14 springboot中使用ehcache、mybatisgenerator和logback_第2张图片

完成代码的自动生成。

集成logback

在resource下加入logback.xml,不用配置什么



    
    
    

    
    
    
    
    
    
    

    
    
    
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
        
    
    
    
        
            
            ${LOG_APP_HOME}/base.%d{yyyy-MM-dd}.log
            
            
            ${LOG_MAX_HISTORY}
        
        
            
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{500} - %msg%n
        
    
    
    
        
    
    
        
    
    
        
    

    
        
        
    

运行之后会保存到下面的日志里面。

14 springboot中使用ehcache、mybatisgenerator和logback_第3张图片

代码还是放在 https://github.com/lijiaccy/springboot-sharding-table.git

你可能感兴趣的:(14 springboot中使用ehcache、mybatisgenerator和logback)