拦截器与过滤器(三)拦截自定义Annotation注解

使用步骤:

(1)定义注解:先创建自定义注解AnnotationDemo.java文件,类型为@interface,

       文件头加@Retention(RetentionPolicy.RUNTIME)和 @Target注解, 其中,

      ①如果是作用于方法,申明为@Target(ElementType.METHOD);

      ②如果是作用于类,申明为@Target(ElementType.TYPE);

      ③如果即可以作用于类也可以作用于方法,申明为

          @Target(ElementType.METHOD,ElementType.TYPE);

    再按需定义AnnotationDemo.java的类型,其支持的类型有:

   普通对象、数组(不支持集合)、枚举、字符串等

(2)业务接口添加注解:根据@Target给其他方法或类加上@AnnotationDemo注解,注解的

     参数即为AnnotationDemo.java的类型,如果类型加了default默认值,则可以不赋值,其他类型

     都需要赋值。

(3)拦截注解:常用来进行日志记录、权限校验。

   以下为几个demo:

一、日志记录:拦截器记录用户访问模块:

1、例1:方法注解 

(1)注解定义:

package com.demo.interceptor;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 声明注解作用于方法
@Target(ElementType.METHOD)
// 声明注解运行时有效
@Retention(RetentionPolicy.RUNTIME)
public @interface LogModule {
    String moduleCode();
}

 (2)拦截器定义:

package com.demo.interceptor;

import com.demo.enums.LogModuleEnums;
import com.demo.service.LogModuleService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Component
public class LogModuleInterceptor implements HandlerInterceptor{

    private LogModuleService logModuleService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            //方法有无加注解
            LogModule checkVer = method.getAnnotation(LogModule.class);
            //1、没有加注解的接口直接放行
            if(	checkVer == null ){
                return true;
            }
            // 2、加了注解,获取模块编码并记录
            String moduleCode = checkVer.moduleCode();
            BeanFactory factory = WebApplicationContextUtils
                    .getRequiredWebApplicationContext(request.getServletContext());
            logModuleService = (LogModuleService) factory.getBean("logModuleService");
            logModuleService.insertLogModuleRecord(moduleCode);
        }
        return true;
    }
}

配置

package com.demo.config;

import com.demo.interceptor.LogModuleInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LogModuleInterceptor());
    }
}

 (3)服务:

package com.demo.service.impl;

import com.demo.service.LogModuleService;
import org.springframework.stereotype.Service;

@Service("logModuleService")
public class LogModuleServiceImpl implements LogModuleService {
    @Override
    public void insertLogModuleRecord(String moduleCode) {
        System.out.println("访问模块"+moduleCode);
    }
}

(4)接口拦截注解:

package com.demo.controller;

import com.demo.dto.RoleDTO;
import com.demo.enums.LogModuleEnums;
import com.demo.interceptor.LogModule;
import com.demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/role")
@RestController
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/listAllRoles")
    @LogModule(moduleCode = "role")
    public List<RoleDTO> listAllRoles(){
        return roleService.listAllRoles();
    }

    @RequestMapping("/getRoleByRoleCode")
    @LogModule(moduleCode = "role")
    public RoleDTO getRoleByRoleCode(String roleCode){
        return roleService.getRoleByRoleCode(roleCode);
    }
}
package com.demo.controller;

import com.demo.dto.UserDTO;
import com.demo.interceptor.LogModule;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/listAllUsers")
    @LogModule(moduleCode = "user")
    public List<UserDTO> listAllUsers(){
        return userService.listAllUsers();
    }

    @RequestMapping("/getUserByUserAccount")
    @LogModule(moduleCode = "user")
    public UserDTO getUserByUserAccount(String userAccount){
        return userService.getUserByUserAccount(userAccount);
    }
}

 依次访问接口,后台打印:

 

2、例2:同时作用于类和方法注解:

(1)定义注解:

package com.demo.interceptor;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// 声明注解作用于类、方法
@Target({ElementType.TYPE,ElementType.METHOD})
// 声明注解运行时有效
@Retention(RetentionPolicy.RUNTIME)
public @interface LogModule {
    String moduleCode();
}

 (2)拦截器:

package com.demo.interceptor;

import com.demo.enums.LogModuleEnums;
import com.demo.service.LogModuleService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Component
public class LogModuleInterceptor implements HandlerInterceptor{

    private LogModuleService logModuleService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            //方法有无加注解
            LogModule annotation = method.getAnnotation(LogModule.class);
            //1、没有加注解,判断所在类有没有加注解
            if(	annotation == null ){
                System.out.println(method.getName()+"方法无注解");
                annotation = method.getDeclaringClass().getAnnotation(LogModule.class);
                if(annotation == null){
                    System.out.println(method.getName()+"所在类无注解");
                    return true;
                }
                System.out.println(method.getName()+"所在类有注解");
            }
            // 2、加了注解,获取模块编码并记录
            String moduleCode = annotation.moduleCode();
            BeanFactory factory = WebApplicationContextUtils
                    .getRequiredWebApplicationContext(request.getServletContext());
            logModuleService = (LogModuleService) factory.getBean("logModuleService");
            logModuleService.insertLogModuleRecord(moduleCode);
        }
        return true;
    }
}

(3)接口注解情况:

package com.demo.controller;

import com.demo.dto.UserDTO;
import com.demo.interceptor.LogModule;
import com.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/listAllUsers")
    public List<UserDTO> listAllUsers(){
        return userService.listAllUsers();
    }

    @RequestMapping("/getUserByUserAccount")
    @LogModule(moduleCode = "user")
    public UserDTO getUserByUserAccount(String userAccount){
        return userService.getUserByUserAccount(userAccount);
    }
}
package com.demo.controller;

import com.demo.dto.RoleDTO;
import com.demo.enums.LogModuleEnums;
import com.demo.interceptor.LogModule;
import com.demo.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/role")
@RestController
@LogModule(moduleCode = "role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/listAllRoles")
    public List<RoleDTO> listAllRoles(){
        return roleService.listAllRoles();
    }

    @RequestMapping("/getRoleByRoleCode")
    public RoleDTO getRoleByRoleCode(String roleCode){
        return roleService.getRoleByRoleCode(roleCode);
    }
}

依次访问接口,后台打印:

3、例3:自定义注入枚举,同样同时作用于类和方法:

(1)定义枚举

package com.demo.enums;

public enum  LogModuleEnums {
    USER("user","用户"),
    ROLE("role","角色");

    public final String moduleCode;

    public final String moduleName;

    LogModuleEnums(String moduleCode,String moduleName){
        this.moduleCode = moduleCode;
        this.moduleName = moduleName;
    }
}

(2)定义注解:

package com.demo.interceptor;

import com.demo.enums.LogModuleEnums;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// 声明注解作用于类、方法
@Target({ElementType.TYPE,ElementType.METHOD})
// 声明注解运行时有效
@Retention(RetentionPolicy.RUNTIME)
public @interface LogModule {
    LogModuleEnums module();
}

(3)拦截器修改:

LogModuleEnums module = annotation.module();
            BeanFactory factory = WebApplicationContextUtils
                    .getRequiredWebApplicationContext(request.getServletContext());
            logModuleService = (LogModuleService) factory.getBean("logModuleService");
            logModuleService.insertLogModuleRecord(module);

(4)接口拦截:

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/listAllUsers")
    public List<UserDTO> listAllUsers(){
        return userService.listAllUsers();
    }

    @RequestMapping("/getUserByUserAccount")
    @LogModule(module = LogModuleEnums.USER)
    public UserDTO getUserByUserAccount(String userAccount){
        return userService.getUserByUserAccount(userAccount);
    }
}
@RequestMapping("/role")
@RestController
@LogModule(module = LogModuleEnums.ROLE)
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/listAllRoles")
    public List<RoleDTO> listAllRoles(){
        return roleService.listAllRoles();
    }

    @RequestMapping("/getRoleByRoleCode")
    public RoleDTO getRoleByRoleCode(String roleCode){
        return roleService.getRoleByRoleCode(roleCode);
    }
}

(5)服务:

package com.demo.service.impl;

import com.demo.enums.LogModuleEnums;
import com.demo.service.LogModuleService;
import org.springframework.stereotype.Service;

@Service("logModuleService")
public class LogModuleServiceImpl implements LogModuleService {
    @Override
    public void insertLogModuleRecord(LogModuleEnums module) {
        System.out.println("访问模块"+module.moduleCode+" "+module.moduleName);
    }
}

依次访问接口,打印:

以上使用过滤器实现:

 二、token校验:拦截器对必须要传入token的接口做拦截:

对项目目录做调整:

 1、定义注解:

package com.demo.interceptor.tokencheck;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// 声明注解作用于类、方法
@Target({ElementType.TYPE,ElementType.METHOD})
// 声明注解运行时有效
@Retention(RetentionPolicy.RUNTIME)
public @interface TokenCheck {
}

2、定义拦截器:

package com.demo.interceptor.tokencheck;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Component
public class TokenCheckInterceptor implements HandlerInterceptor{

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method = handlerMethod.getMethod();
            //方法有无加注解
            TokenCheck annotation = method.getAnnotation(TokenCheck.class);
            //1、没有加注解,判断所在类有没有加注解
            if(	annotation == null ){
                annotation = method.getDeclaringClass().getAnnotation(TokenCheck.class);
                if(annotation == null){
                    return true;
                }
            }
            // 2、加了注解,校验请求头是否加了token
            String token = request.getHeader("token");
            if(StringUtils.isEmpty(token)){
                System.out.println("头部未传入token");
                return false;
            }
        }
        return true;
    }
}

3、配置拦截器:

package com.demo.config;

import com.demo.interceptor.logmodule.LogModuleInterceptor;
import com.demo.interceptor.tokencheck.TokenCheckInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TokenCheckInterceptor());
        registry.addInterceptor(new LogModuleInterceptor());
    }
}

4、接口加注解:

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/listAllUsers")
    @TokenCheck
    public List<UserDTO> listAllUsers(){
        return userService.listAllUsers();
    }

    @RequestMapping("/getUserByUserAccount")
    @LogModule(module = LogModuleEnums.USER)
    public UserDTO getUserByUserAccount(String userAccount){
        return userService.getUserByUserAccount(userAccount);
    }
}
@RequestMapping("/role")
@RestController
@LogModule(module = LogModuleEnums.ROLE)
@TokenCheck
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/listAllRoles")
    public List<RoleDTO> listAllRoles(){
        return roleService.listAllRoles();
    }

    @RequestMapping("/getRoleByRoleCode")
    public RoleDTO getRoleByRoleCode(String roleCode){
        return roleService.getRoleByRoleCode(roleCode);
    }
}

测试:

(1)访问http://localhost:7777/filterDemo/user/listAllUsers    不加token

 后台打印:头部未传入token

加上token:

 后台打印:token校验通过 

(2)访问http://localhost:7777/filterDemo/user/getUserByUserAccount?userAccount=zhangsan 

后台打印:无需校验token,不拦截

(3)访问/role下接口,情况同(1)。

三、角色权限校验:判断用户是否有访问功能接口的角色或权限,防止越权:

越权有两种:

1)水平越权:指相同权限用户之间在未经授权的情况下,可以访问到一方的资源。比如:同是一个网站的普通用户A和B,A通过越权操作访问了B的信息。这种越权可以按用户维度去控制数据权限,比如查询中带上用户的区域范围等。

2)垂直越权:指没有做好不同角色之间的权限控制,或仅仅在菜单上做了权限控制,低权限用户实现了高权限用户的功能。比如普通用户通过越权登录到了管理员页面,实现管理员才能的操作。下面用自定义拦截的方式解决垂直越权问题。

1、自定义拦截:从接口中获取两个参数,菜单类型(菜单or按钮)、接口对应的权限编码,菜单类型默认为菜单,因为一个接口可能在多个菜单页面都有调用,所以这里权限编码是数组

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AuthVerify {
    /**
     * 操作类型,MenuTypeEnums
     */
    MenuTypeEnums menuType() default MenuTypeEnums.MENU_TYPE_MENU;

    /**
     * 权限编码,MenuCodeEnums
     */
    MenuCodeEnums[] authMenuCodes();
}

对应的枚举:

public enum MenuTypeEnums {

    MENU_TYPE_MENU(1,"菜单"),
    MENU_TYPE_ACTION(2,"按钮"),
    SYSTEM(3,"系统资源")
    ;
    public final Integer code;

    public final String message;

    MenuTypeEnums(int code, String message ) {
        this.code = code;
        this.message = message;
    }
}
public enum MenuCodeEnums {

    USER_MANAGE("web:userManage","用户管理"),
    USER_MANAGE_ADD("web:userManage:add","新增用户")
    ;
    public final String code;

    public final String name;

    MenuCodeEnums(String code, String name) {
        this.code = code;
        this.name = name;
    }
}

2、业务接口注解:

@PostMapping(value = "/user/getPageList")
@AuthVerify(authMenuCodes = {MenuCodeEnums.USER_MANAGE,MenuCodeEnums.USER_MANAGE_ADD})
    public ResponseMessage getPageList(){
        //
    }
@GetMapping("/listPositionByName")
    @AuthVerify(menuType = MenuTypeEnums.SYSTEM,authMenuCodes = {})
    public ResponseMessage listPositionByName(@RequestParam(value = "positionName", required = false) String positionName){
       //
    }

3、拦截判断:

@Component
public class AuthCheckInterceptor implements HandlerInterceptor {

    private static Logger logger = LoggerFactory.getLogger(HandlerInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        try {
            if (handler instanceof HandlerMethod) {
                HandlerMethod handlerMethod = (HandlerMethod) handler;
                Method method = handlerMethod.getMethod();
                //方法有无加注解
                AuthVerify annotation = method.getAnnotation(AuthVerify.class);
                if (annotation == null){
                    return true;
                }
                //有注解
                //1、资源类型是否是系统,是系统均有查看权限
                Integer authMenuType = annotation.menuType().code;
                if(MenuTypeEnums.SYSTEM.code.equals(authMenuType)){
                    logger.info("AuthCheckInterceptor.preHandle 接口资源类型无需校验,放行");
                    return true;
                }
                //2、资源类型是普通菜单/按钮,控制访问权限
                MenuCodeEnums[] authMenuCodeEnums = annotation.authMenuCodes();
                JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                String userName = jwtUser.getUsername();
                List<MenuVO> userMenus = jwtUser.getMenuVOList();
                List<String> userMenuCodes =  userMenus.stream().map(MenuVO::getMenuCode).collect(Collectors.toList());
                for(MenuCodeEnums menuCodeEnum : authMenuCodeEnums){
                    String authMenuCode = menuCodeEnum.menuCode;
                    if(userMenuCodes.contains(authMenuCode)){
                        return true;
                    }
                }
                //处理请求失败返回
                logger.warn("AuthCheckInterceptor.preHandle,userName={}无权限访问",userName);
                setErrorRes(response);
                return false;
            }
        } catch (Exception e) {
            logger.error("AuthCheckInterceptor.preHandle异常", e);
            return true;
        }
        return true;
    }

    private void setErrorRes(HttpServletResponse response) {
        try{
            ResponseMessage responseMessage = ResponseMessage.error("没有权限访问");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json;charset=utf-8");
            PrintWriter out = response.getWriter();
            out.write(JSON.toJSONString(responseMessage));
        }catch (Exception e){
            logger.error("AuthCheckInterceptor.preHandle设置自定义返回异常",e);
        }
    }
}

拦截通过则正常返回数据,拦截校验失败返回错误信息:

{"code":400,"message":"没有权限访问","status":"error","success":false}

改进:因只有部分接口需要权限拦截校验,使用拦截器的话大部分接口都是走到无注解这步,可以采用AOP切面,只拦截注解的接口。

<!-- aop和aspect -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
@Aspect
@Component
public class AuthCheckAspect {
    private static Logger logger = LoggerFactory.getLogger(AuthCheckAspect.class);

    @Around("@annotation(com.demo.common.AuthVerify)")
    public Object doAuthFilter(ProceedingJoinPoint pjp) throws Throwable {
        try{
            Signature signature = pjp.getSignature();
            MethodSignature methodSignature = (MethodSignature) signature;
            Method targetMethod = methodSignature.getMethod();
            AuthVerify annotation = targetMethod.getAnnotation(AuthVerify.class);
            //非AuthVerify权限类注解,放开
            if (annotation == null) {
                return pjp.proceed();
            }
            //有注解
            //1、资源类型是否是系统,是系统均有查看权限
            Integer authMenuType = annotation.menuType().code;
            if(MenuTypeEnums.SYSTEM.code.equals(authMenuType)){
                logger.info("AuthCheckAspect.doAuthFilter 接口资源类型无需校验,放行");
                return pjp.proceed();
            }
            //2、资源类型是普通菜单/按钮,控制访问权限
            MenuCodeEnums[] authMenuCodeEnums = annotation.authMenuCodes();
            JwtUser jwtUser = (JwtUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String userName = jwtUser.getUsername();
            List<MenuVO> userMenus = jwtUser.getMenuVOList();
            List<String> userMenuCodes =  userMenus.stream().map(MenuVO::getMenuCode).collect(Collectors.toList());
            for(MenuCodeEnums menuCodeEnum : authMenuCodeEnums){
                String authMenuCode = menuCodeEnum.menuCode;
                if(userMenuCodes.contains(authMenuCode)){
                    return pjp.proceed();
                }
            }
            //请求失败
            logger.warn("AuthCheckAspect.doAuthFilter,userName={}无权限访问",userName);
            return ResponseMessage.error("没有权限访问");
        }catch (Exception e){
            return pjp.proceed();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
1. Hibernate入门 1.1. 前言 1.2. 第一部分 - 第一个Hibernate应用程序 1.2.1. 第一个class 1.2.2. 映射文件 1.2.3. Hibernate配置 1.2.4. 用Ant构建 1.2.5. 启动和辅助类 1.2.6. 加载并存储对象 1.3. 第二部分 - 关联映射 1.3.1. 映射Person类 1.3.2. 单向Set-based的关联 1.3.3. 使关联工作 1.3.4. 值类型的集合 1.3.5. 双向关联 1.3.6. 使双向连起来 1.4. 第部分 - EventManager web应用程序 1.4.1. 编写基本的servlet 1.4.2. 处理与渲染 1.4.3. 部署与测试 1.5. 总结 2. 体系结构(Architecture) 2.1. 概况(Overview) 2.2. 实例状态 2.3. JMX整合 2.4. 对JCA的支持 2.5. 上下文相关的(Contextual)Session 3. 配置 3.1. 可编程的配置方式 3.2. 获得SessionFactory 3.3. JDBC连接 3.4. 可选的配置属性 3.4.1. SQL方言 3.4.2. 外连接抓取(Outer Join Fetching) 3.4.3. 二进制流 (Binary Streams) 3.4.4. 二级缓存与查询缓存 3.4.5. 查询语言中的替换 3.4.6. Hibernate的统计(statistics)机制 3.5. 日志 3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. 在JTA环境下使用Current Session context (当前session上下文)管理 3.8.4. JMX部署 4. 持久化类(Persistent Classes) 4.1. 一个简单的POJO例子 4.1.1. 实现一个默认的(即无参数的)构造方法(constructor) 4.1.2. 提供一个标识属性(identifier property)(可选) 4.1.3. 使用非final的类 (可选) 4.1.4. 为持久化字段声明访问器(accessors)和是否可变的标志(mutators)(可选) 4.2. 实现继承(Inheritance) 4.3. 实现equals()和hashCode() 4.4. 动态模型(Dynamic models) 4.5. 元组片断映射(Tuplizers) 5. 对象/关系数据库映射基础(Basic O/R Mapping) 5.1. 映射定义(Mapping declaration) 5.1.1. Doctype 5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences) 5.1.4.5. 程序分配的标识符(Assigned Identifiers) 5.1.4.6. 触发器实现的主键生成器(Primary keys assigned by triggers) 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-one) 5.1.11. 一对一 5.1.12. 自然ID(natural-id) 5.1.13. 组件(component), 动态组件(dynamic-component) 5.1.14. properties 5.1.15. 子类(subclass) 5.1.16. 连接的子类(joined-subclass) 5.1.17. 联合子类(union-subclass) 5.1.18. 连接(join) 5.1.19. 键(key) 5.1.20. 字段和规则元素(column and formula elements) 5.1.21. 引用(import) 5.1.22. any 5.2. Hibernate 的类型 5.2.1. 实体(Entities)和值(values) 5.2.2. 基本值类型 5.2.3. 自定义值类型 5.3. 多次映射同一个类 5.4. SQL中引号包围的标识符 5.5. 其他元数据(Metadata) 5.5.1. 使用 XDoclet 标记 5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合元素(Collection elements) 6.2.3. 索引集合类(Indexed collections) 6.2.4. 值集合于多对多关联(Collections of values and many-to-many associations) 6.2.5. 一对多关联(One-to-many Associations) 6.3. 高级集合映射(Advanced collection mappings) 6.3.1. 有序集合(Sorted collections) 6.3.2. 双向关联(Bidirectional associations) 6.3.3. 双向关联,涉及有序集合类 6.3.4. 重关联(Ternary associations) 6.3.5. 使用<idbag> 6.4. 集合例子(Collection example) 7. 关联关系映射 7.1. 介绍 7.2. 单向关联(Unidirectional associations) 7.2.1. 多对一(many to one) 7.2.2. 一对一(one to one) 7.2.3. 一对多(one to many) 7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向关联(Bidirectional associations) 7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合标识符(Components as composite identifiers) 8.5. 动态组件 (Dynamic components) 9. 继承映射(Inheritance Mappings) 9.1. 种策略 9.1.1. 每个类分层结构一张表(Table per class hierarchy) 9.1.2. 每个子类一张表(Table per subclass) 9.1.3. 每个子类一张表(Table per subclass),使用辨别标志(Discriminator) 9.1.4. 混合使用“每个类分层结构一张表”和“每个子类一张表” 9.1.5. 每个具体类一张表(Table per concrete class) 9.1.6. Table per concrete class, using implicit polymorphism 9.1.7. 隐式多态和其他继承映射混合使用 9.2. 限制 10. 与对象共事 10.1. Hibernate对象状态(object states) 10.2. 使对象持久化 10.3. 装载对象 10.4. 查询 10.4.1. 执行查询 10.4.1.1. 迭代式获取结果(Iterating results) 10.4.1.2. 返回元组(tuples)的查询 10.4.1.3. 标量(Scalar)结果 10.4.1.4. 绑定参数 10.4.1.5. 分页 10.4.1.6. 可滚动遍历(Scrollable iteration) 10.4.1.7. 外置命名查询(Externalizing named queries) 10.4.2. 过滤集合 10.4.3. 条件查询(Criteria queries) 10.4.4. 使用原生SQL的查询 10.5. 修改持久对象 10.6. 修改脱管(Detached)对象 10.7. 自动状态检测 10.8. 删除持久对象 10.9. 在两个不同数据库间复制对象 10.10. Session刷出(flush) 10.11. 传播性持久化(transitive persistence) 10.12. 使用元数据 11. 事务和并发 11.1. Session和事务范围(transaction scope) 11.1.1. 操作单元(Unit of work) 11.1.2. 长对话 11.1.3. 关注对象标识(Considering object identity) 11.1.4. 常见问题 11.2. 数据库事务声明 11.2.1. 非托管环境 11.2.2. 使用JTA 11.2.3. 异常处理 11.2.4. 事务超时 11.3. 乐观并发控制(Optimistic concurrency control) 11.3.1. 应用程序级别的版本检查(Application version checking) 11.3.2. 扩展周期的session和自动版本化 11.3.3. 脱管对象(deatched object)和自动版本化 11.3.4. 定制自动版本化行为 11.4. 悲观锁定(Pessimistic Locking) 11.5. 连接释放模式(Connection Release Modes) 12. 拦截器与事件(Interceptors and events) 12.1. 拦截器(Interceptors) 12.2. 事件系统(Event system) 12.3. Hibernate的声明式安全机制 13. 批量处理(Batch processing) 13.1. 批量插入(Batch inserts) 13.2. 批量更新(Batch updates) 13.3. StatelessSession (无状态session)接口 13.4. DML(数据操作语言)风格的操作(DML-style operations) 14. HQL: Hibernate查询语言 14.1. 大小写敏感性问题 14.2. from子句 14.3. 关联(Association)与连接(Join) 14.4. join 语法的形式 14.5. select子句 14.6. 聚集函数 14.7. 多态查询 14.8. where子句 14.9. 表达式 14.10. order by子句 14.11. group by子句 14.12. 子查询 14.13. HQL示例 14.14. 批量的UPDATE和DELETE 14.15. 小技巧 & 小窍门 15. 条件查询(Criteria Queries) 15.1. 创建一个Criteria 实例 15.2. 限制结果集内容 15.3. 结果集排序 15.4. 关联 15.5. 动态关联抓取 15.6. 查询示例 15.7. 投影(Projections)、聚合(aggregation)和分组(grouping) 15.8. 离线(detached)查询和子查询 15.9. 根据自然标识查询(Queries by natural identifier) 16. Native SQL查询 16.1. 使用SQLQuery 16.2. 别名和属性引用 16.3. 命名SQL查询 16.3.1. 使用return-property来明确地指定字段/别名 16.3.2. 使用存储过程来查询 16.3.2.1. 使用存储过程的规则和限制 16.4. 定制SQL用来create,update和delete 16.5. 定制装载SQL 17. 过滤数据 17.1. Hibernate 过滤器(filters) 18. XML映射 18.1. 用XML数据进行工作 18.1.1. 指定同时映射XML和类 18.1.2. 只定义XML映射 18.2. XML映射元数据 18.3. 操作XML数据 19. 提升性能 19.1. 抓取策略(Fetching strategies) 19.1.1. 操作延迟加载的关联 19.1.2. 调整抓取策略(Tuning fetch strategies) 19.1.3. 单端关联代理(Single-ended association proxies) 19.1.4. 实例化集合和代理(Initializing collections and proxies) 19.1.5. 使用批量抓取(Using batch fetching) 19.1.6. 使用子查询抓取(Using subselect fetching) 19.1.7. 使用延迟属性抓取(Using lazy property fetching) 19.2. 二级缓存(The Second Level Cache) 19.2.1. 缓存映射(Cache mappings) 19.2.2. 策略:只读缓存(Strategy: read only) 19.2.3. 策略:读/写缓存(Strategy: read/write) 19.2.4. 策略:非严格读/写缓存(Strategy: nonstrict read/write) 19.2.5. 策略:事务缓存(transactional) 19.3. 管理缓存(Managing the caches) 19.4. 查询缓存(The Query Cache) 19.5. 理解集合性能(Understanding Collection performance) 19.5.1. 分类(Taxonomy) 19.5.2. Lists, maps 和sets用于更新效率最高 19.5.3. Bag和list是反向集合类中效率最高的 19.5.4. 一次性删除(One shot delete) 19.6. 监测性能(Monitoring performance) 19.6.1. 监测SessionFactory 19.6.2. 数据记录(Metrics) 20. 工具箱指南 20.1. Schema自动生成(Automatic schema generation) 20.1.1. 对schema定制化(Customizing the schema) 20.1.2. 运行该工具 20.1.3. 属性(Properties) 20.1.4. 使用Ant(Using Ant) 20.1.5. 对schema的增量更新(Incremental schema updates) 20.1.6. 用Ant来增量更新schema(Using Ant for incremental schema updates) 20.1.7. Schema 校验 20.1.8. 使用Ant进行schema校验 21. 示例:父子关系(Parent Child Relationships) 21.1. 关于collections需要注意的一点 21.2. 双向的一对多关系(Bidirectional one-to-many) 21.3. 级联生命周期(Cascading lifecycle) 21.4. 级联与未保存值(Cascades and unsaved-value) 21.5. 结论 22. 示例:Weblog 应用程序 22.1. 持久化类 22.2. Hibernate 映射 22.3. Hibernate 代码 23. 示例:复杂映射实例 23.1. Employer(雇主)/Employee(雇员) 23.2. Author(作家)/Work(作品) 23.3. Customer(客户)/Order(订单)/Product(产品) 23.4. 杂例 23.4.1. "Typed" one-to-one association 23.4.2. Composite key example 23.4.3. 共有组合键属性的多对多(Many-to-many with shared composite key attribute) 23.4.4. Content based discrimination 23.4.5. Associations on alternate keys

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值