public interface EmployeeMapper {
@Select("select * from employee where id=#{id}")
public Employee getEmpById(Integer id);
@Insert("insert into employee(lastName,email,gender,d_id) values(#{lastName},#{email},#{gender},#{dId})")
public void insertEmp(Employee employee);
@Update("update employee set lastName=#{lastName},email=#{email},gender=#{gender},d_id=#{dId} where id=#{id}")
public void updateEmp(Employee employee);
@Delete("delete from employee where id = #{id}")
public void deleteEmpById(Integer id);
}
EmployeeService示例如下:
@Service
public class EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
public Employee getEmp(Integer id){
System.out.println("查询"+id+"号员工");
Employee empById = employeeMapper.getEmpById(id);
return empById;
}
}
EmployeeController示例如下:
@RestController
public class EmployeeController {
@Autowired
EmployeeService employeeService;
@GetMapping("/emp/{id}")
public Employee getEmp(@PathVariable("id") Integer id){
return employeeService.getEmp(id);
}
}
@MapperScan(value = "com.web.springboot.mapper")
@SpringBootApplication
// 主程序添加@EnableCaching注解
@EnableCaching
public class SpringBoot01CacheApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01CacheApplication.class, args);
}
}
// 具体方法上添加@Cacheable注解
@Cacheable(cacheNames = "emp")
public Employee getEmp(Integer id){
System.out.println("查询"+id+"号员工");
Employee empById = employeeMapper.getEmpById(id);
return empById;
}
测试,连续两次获取id为1的Employee,控制台输出如下:
即,只进行了一次查询,第二次从缓存中直接获取!
注意:此时没有使用第三方缓存组件如Redis、memcached等,本地缓存内容是在内存中!
【4】@EnableCaching注解
Enables Spring’s annotation-driven cache management capability, similar to the support found in Spring’s {@code cache:*} XML namespace.
注解示例如下:
@Configuration
@EnableCaching
public class AppConfig {
@Bean
public MyService myService() {
// configure and return a class having @Cacheable methods
return new MyService();
}
//自定义CacheManager
@Bean
public CacheManager cacheManager() {
// configure and return an implementation of Spring's CacheManager SPI
SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
return cacheManager;
}
}
等同于Spring xml配置如下:
源码说明如下:
* In both of the scenarios above, {@code @EnableCaching} and {@code
* } are responsible for registering the necessary Spring
* components that power annotation-driven cache management, such as the
* {@link org.springframework.cache.interceptor.CacheInterceptor CacheInterceptor} and the
* proxy- or AspectJ-based advice that weaves the interceptor into the call stack when
* {@link org.springframework.cache.annotation.Cacheable @Cacheable} methods are invoked.
*
*
If the JSR-107 API and Spring's JCache implementation are present, the necessary
* components to manage standard cache annotations are also registered. This creates the
* proxy- or AspectJ-based advice that weaves the interceptor into the call stack when
* methods annotated with {@code CacheResult}, {@code CachePut}, {@code CacheRemove} or
* {@code CacheRemoveAll} are invoked.
*
*
A bean of type {@link org.springframework.cache.CacheManager CacheManager}
* must be registered, as there is no reasonable default that the framework can
* use as a convention. And whereas the {@code } element assumes
* a bean named "cacheManager", {@code @EnableCaching} searches for a cache
* manager bean by type. Therefore, naming of the cache manager bean method is
* not significant.
【5】@Cacheable注解
源码如下:
*//标明方法返回结果应该被缓存
* //每次目标方法被调用前,都会根据给定的方法参数检查是否方法已经被调用(进行了缓存)
*//默认使用方法参数得到缓存的key,但是你可以在key属性中使用SpELl表达式或者使用自定义的keyGenerator来指定key
*//如果根据key没有从缓存里拿到值,就调用方法并将返回值进行缓存
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Cacheable {
@AliasFor("cacheNames")
String[] value() default {};
@AliasFor("value")
String[] cacheNames() default {};
// value等同于cacheNames,指定方法结果应该被存放的Cache组件名字
/**
* Spring Expression Language (SpEL) expression for computing the key dynamically.
*
Default is {@code ""}, meaning all method parameters are considered as a key,
* unless a custom {@link #keyGenerator} has been configured.
* // 默认方法的所有参数作为key,除非配置了自定义的keyGenerator
*
The SpEL expression evaluates against a dedicated context that provides the
* following meta-data:
*
*
{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
* references to the {@link java.lang.reflect.Method method}, target object, and
* affected cache(s) respectively.
*
Shortcuts for the method name ({@code #root.methodName}) and target class
* ({@code #root.targetClass}) are also available.
*
Method arguments can be accessed by index. For instance the second argument
* can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
* can also be accessed by name if that information is available.
*
*/
String key() default "";
/**
* The bean name of the custom {@link org.springframework.cache.interceptor.KeyGenerator}
* to use.
*
Mutually exclusive with the {@link #key} attribute.
* @see CacheConfig#keyGenerator
*/
String keyGenerator() default "";
/**
* The bean name of the custom {@link org.springframework.cache.CacheManager} to use to
* create a default {@link org.springframework.cache.interceptor.CacheResolver} if none
* is set already.
*
Mutually exclusive with the {@link #cacheResolver} attribute.
* @see org.springframework.cache.interceptor.SimpleCacheResolver
* @see CacheConfig#cacheManager
*/
String cacheManager() default "";
/**
* The bean name of the custom {@link org.springframework.cache.interceptor.CacheResolver}
* to use.
* @see CacheConfig#cacheResolver
*/
String cacheResolver() default "";
/**
* Spring Expression Language (SpEL) expression used for making the method
* caching conditional.
*
Default is {@code ""}, meaning the method result is always cached.
*
The SpEL expression evaluates against a dedicated context that provides the
* following meta-data:
*
*
{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
* references to the {@link java.lang.reflect.Method method}, target object, and
* affected cache(s) respectively.
*
Shortcuts for the method name ({@code #root.methodName}) and target class
* ({@code #root.targetClass}) are also available.
*
Method arguments can be accessed by index. For instance the second argument
* can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
* can also be accessed by name if that information is available.
*
*/
String condition() default "";
/**
* Spring Expression Language (SpEL) expression used to veto method caching.
*
Unlike {@link #condition}, this expression is evaluated after the method
* has been called and can therefore refer to the {@code result}.
*
Default is {@code ""}, meaning that caching is never vetoed.
*
The SpEL expression evaluates against a dedicated context that provides the
* following meta-data:
*
*
{@code #result} for a reference to the result of the method invocation. For
* supported wrappers such as {@code Optional}, {@code #result} refers to the actual
* object, not the wrapper
*
{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
* references to the {@link java.lang.reflect.Method method}, target object, and
* affected cache(s) respectively.
*
Shortcuts for the method name ({@code #root.methodName}) and target class
* ({@code #root.targetClass}) are also available.
*
Method arguments can be accessed by index. For instance the second argument
* can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
* can also be accessed by name if that information is available.
*
* @since 3.2
*/
String unless() default "";
/**
* Synchronize the invocation of the underlying method if several threads are
* attempting to load a value for the same key. The synchronization leads to
* a couple of limitations:
*
*
{@link #unless()} is not supported
*
Only one cache may be specified
*
No other cache-related operation can be combined
*
* This is effectively a hint and the actual cache provider that you are
* using may not support it in a synchronized fashion. Check your provider
* documentation for more details on the actual semantics.
* @since 4.3
* @see org.springframework.cache.Cache#get(Object, Callable)
*/
boolean sync() default false;
}
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface CacheEvict {
/**
* Alias for {@link #cacheNames}.
*/
@AliasFor("cacheNames")
String[] value() default {};
/**
* Names of the caches to use for the cache eviction operation.
*
Names may be used to determine the target cache (or caches), matching
* the qualifier value or bean name of a specific bean definition.
* @since 4.2
* @see #value
* @see CacheConfig#cacheNames
*/
@AliasFor("value")
String[] cacheNames() default {};
/**
* Spring Expression Language (SpEL) expression for computing the key dynamically.
*
Default is {@code ""}, meaning all method parameters are considered as a key,
* unless a custom {@link #keyGenerator} has been set.
*
The SpEL expression evaluates against a dedicated context that provides the
* following meta-data:
*
*
{@code #result} for a reference to the result of the method invocation, which
* can only be used if {@link #beforeInvocation()} is {@code false}. For supported
* wrappers such as {@code Optional}, {@code #result} refers to the actual object,
* not the wrapper
*
{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
* references to the {@link java.lang.reflect.Method method}, target object, and
* affected cache(s) respectively.
*
Shortcuts for the method name ({@code #root.methodName}) and target class
* ({@code #root.targetClass}) are also available.
*
Method arguments can be accessed by index. For instance the second argument
* can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
* can also be accessed by name if that information is available.
*
*/
String key() default "";
/**
* The bean name of the custom {@link org.springframework.cache.interceptor.KeyGenerator}
* to use.
*
Mutually exclusive with the {@link #key} attribute.
* @see CacheConfig#keyGenerator
*/
String keyGenerator() default "";
/**
* The bean name of the custom {@link org.springframework.cache.CacheManager} to use to
* create a default {@link org.springframework.cache.interceptor.CacheResolver} if none
* is set already.
*
Mutually exclusive with the {@link #cacheResolver} attribute.
* @see org.springframework.cache.interceptor.SimpleCacheResolver
* @see CacheConfig#cacheManager
*/
String cacheManager() default "";
/**
* The bean name of the custom {@link org.springframework.cache.interceptor.CacheResolver}
* to use.
* @see CacheConfig#cacheResolver
*/
String cacheResolver() default "";
/**
* Spring Expression Language (SpEL) expression used for making the cache
* eviction operation conditional.
*
Default is {@code ""}, meaning the cache eviction is always performed.
*
The SpEL expression evaluates against a dedicated context that provides the
* following meta-data:
*
*
{@code #root.method}, {@code #root.target}, and {@code #root.caches} for
* references to the {@link java.lang.reflect.Method method}, target object, and
* affected cache(s) respectively.
*
Shortcuts for the method name ({@code #root.methodName}) and target class
* ({@code #root.targetClass}) are also available.
*
Method arguments can be accessed by index. For instance the second argument
* can be accessed via {@code #root.args[1]}, {@code #p1} or {@code #a1}. Arguments
* can also be accessed by name if that information is available.
*
*/
String condition() default "";
/**
* Whether all the entries inside the cache(s) are removed.
*
By default, only the value under the associated key is removed.
* 默认只删除对应的key的数据
*
Note that setting this parameter to {@code true} and specifying a
* {@link #key} is not allowed.
* 需要注意的是#key与该属性设置为true不允许同时存在
*/
boolean allEntries() default false;
/**
* Whether the eviction should occur before the method is invoked.
* 是否在方法调用前清楚缓存
*
Setting this attribute to {@code true}, causes the eviction to
* occur irrespective of the method outcome (i.e., whether it threw an
* exception or not).
* 如果设置为true,则无论方法是否正常执行,都会在方法执行前清除缓存
*
Defaults to {@code false}, meaning that the cache eviction operation
* will occur after the advised method is invoked successfully (i.e.,
* only if the invocation did not throw an exception).
* 默认在方法正常成功执行后,清除缓存
*/
boolean beforeInvocation() default false;
}
【10】@Caching注解
该注解用来构建复杂规则的缓存用例,源码如下:
/**
* Group annotation for multiple cache annotations (of different or the same type).
*
*
This annotation may be used as a meta-annotation to create custom
* composed annotations with attribute overrides.
*
* @author Costin Leau
* @author Chris Beams
* @since 3.1
*/
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Caching {
Cacheable[] cacheable() default {};
CachePut[] put() default {};
CacheEvict[] evict() default {};
}
*@CacheConfig在类级别上提供了共享与缓存相关的公共设置的机制
当类上面添加了该注解,就意味着提供了一系列默认设置关于该类上定义的缓存操作
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CacheConfig {
/**
* Names of the default caches to consider for caching operations defined
* in the annotated class.
*
If none is set at the operation level, these are used instead of the default.
*
May be used to determine the target cache (or caches), matching the
* qualifier value or the bean names of a specific bean definition.
*/
String[] cacheNames() default {};
* 使用在该类上面的KeyGenerator类型的bean 实例名字
*
If none is set at the operation level, this one is used instead of the default.
*
The key generator is mutually exclusive with the use of a custom key. When such key is
* defined for the operation, the value of this key generator is ignored.
*/
String keyGenerator() default "";
/**
* The bean name of the custom {@link org.springframework.cache.CacheManager} to use to
* create a default {@link org.springframework.cache.interceptor.CacheResolver} if none
* is set already.
*
If no resolver and no cache manager are set at the operation level, and no cache
* resolver is set via {@link #cacheResolver}, this one is used instead of the default.
* @see org.springframework.cache.interceptor.SimpleCacheResolver
*/
String cacheManager() default "";
/**
* The bean name of the custom {@link org.springframework.cache.interceptor.CacheResolver} to use.
*
If no resolver and no cache manager are set at the operation level, this one is used
* instead of the default.
*/
String cacheResolver() default "";
}
// 多态, 在JAVA中是这样用的, 其实在PHP当中可以自然消除, 因为参数是动态的, 你传什么过来都可以, 不限制类型, 直接调用类的方法
abstract class Tiger {
public abstract function climb();
}
class XTiger extends Tiger {
public function climb()
jQuery.extend({
handleError: function( s, xhr, status, e ) {
// If a local callback was specified, fire it
if ( s.error ) {
s.error.call( s.context || s, xhr, status, e );
}
always 总是
rice 水稻,米饭
before 在...之前
live 生活,居住
usual 通常的
early 早的
begin 开始
month 月份
year 年
last 最后的
east 东方的
high 高的
far 远的
window 窗户
world 世界
than 比...更
最近使用mybatis.3.1.0时无意中碰到一个问题:
The errors below were detected when validating the file "mybatis-3-mapper.dtd" via the file "account-mapper.xml". In most cases these errors can be d