zoukankan      html  css  js  c++  java
  • Spring MVC 注解

    注解:
    /********************************************************** Jsp *************************************************************************/
    /********************************************************** Controller *************************************************************************/
    @Controller    //控制器
    @RequestMapping("/mall/cartitem/*")        //映射路径
    public class CartItemController {
    @Resource                                                                 //默认按bean 的name 进行查找,如果没有找到会按type 进行查找此时与@Autowired 类 似,在没有为 @Resource 注解
          //其它情况 :                                                             //显式指定 name 属性的前提下,如果将其标注在 BeanFactory 类型、ApplicationContext 类型、ResourceLoader 类型、
     //1、@Resource(name="dataSource")                 //ApplicationEventPublisher 类型、MessageSource 类型上,那么 Spring 会自动注入这些实现类的实例,不需要额外的
          //2、@Resource(type=DataSource.class)  //操作。此时 name 属性不需要指定 ( 或者指定为""),否则注入失败;
    private IAdminLogService iadminlogservice;          
     
    }
    @InitBinder        //初始化Binder
    public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    dateFormat.setLenient(false);
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
     
    @RequestMapping(value = "delete")
    public String delete(HttpServletRequest request) {
    }
    @SuppressWarnings("unchecked")       //废弃警告信息 
    @RequestMapping(value = "addToGwc/{id}/{num}")    //映射位置
    public String addToGwc(HttpServletRequest req, Model model,
    @Valid Production production //实体设置+类+类的引用  直接将页面传过来的production对象中的信息封装到里面去了
    @PathVariable Long id, //此处id对应映射时传递过来的值
    @PathVariable int num) {                                        //此处num对应映射时传递过来的值
    /*...
     内容
     ...*/
    }
     
    @RequestMapping(value = "updateProductCommodity/{id}")
    public void updateProductCommodity(@PathVariable long id,
    @RequestParam(value = "file1", required = false) MultipartFile file1,        //请求参数,将name名为file1的value值赋予给MultipartFile file1这个属性; required = false这个表示
    //如果jsp中没有file1这个值,将不会提示,默认值为true。
    HttpServletRequest request, HttpServletResponse response)
    throws IOException {
    }
    @Autowired                                     // 自动连接
    public void setShopManager(CartItemManager cartItemManager) {
    this.cartItemManager = cartItemManager;
    }
    /********************************************************** Manager *************************************************************************/
    @Component      //成分
    @Transactional                          //事务处理
    public class PaymentInfoManager {
    /*...
     内容
     ...*/
    }
    @Autowired                //自动连接
    public void setPaymentInfoDao(PaymentInfoDao paymentInfoDao) {
    this.paymentInfoDao = paymentInfoDao;
    }
    @Transactional(readOnly=true)          //事务处理
    public PaymentInfo get(Long id){     
    return paymentInfoDao.get(id);
    }
    @SuppressWarnings("unchecked")   // 这一个类型可以来暂时把一些警告信息消息关闭
    public List getPaymentInfoList(String hql,Object...values){
    Query query=paymentInfoDao.createQuery(hql, values);
    return query.list();
    }
    @SuppressWarnings("unchecked")
    @Transactional(readOnly = true)
    public List<Production> queryLimitF(String hql, Object... values) {
    Query query = productionDao.createQuery(hql, values);
    query.setFirstResult(0);
    query.setMaxResults(5);
    return query.list();
    }
     
    /********************************************************** Dao *************************************************************************/
     @Component                                                        //成员
    /********************************************************** VO *************************************************************************/
    /*-----------第一种情况-------------*/
    @Entity                                                                    //实体
    @Table(name = "brand")                                      // 表+主键
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)    //默认的缓存策略.                   //(需要时使用)
    @Lazy                                                                                                                      //(需要时使用)
    public class Brand extends IdEntity {
    @NotEmpty                                                        // 不是空
    private String name;// 商品名称
    @NotNull                                                           //不是空
    private Float actualPrice; // 实际价格
    @Size(min=1,max=255)                                 //限制长度大小
    private String name;//品牌名称
    @NotEmpty
    @Size(min=1,max=255)
    private String name;//分类名称
    @Column(name="has_shop")
    private Long hasShop;
    private List<ImageDesc> imageDescList = Lists.newArrayList();
    //多对多定义
    @ManyToMany
    //中间表定义,表名采用默认命名规则
    @JoinTable(name = "imagedesc", joinColumns = { @JoinColumn(name = "newsImageId") }, inverseJoinColumns = { @JoinColumn(name = "id") })
    //Fecth策略定义
    @Fetch(FetchMode.SUBSELECT)
    //集合按id排序.
    @OrderBy("id")
    //集合中对象id的缓存.
    //@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)                (备选)
    public List<ImageDesc> getImageDescList() {                                               //一般对list都采用以上注解。
    return imageDescList;
    }
    private String name;
    @Column(nullable = false, unique = true)                     //字段非空且唯一, 用于提醒Entity使用者及生成DDL
    public String getName() {
    return name;
    }
    @Transient                //非持久化属性.
    @SuppressWarnings("unchecked")
    public List<Long> getAuthIds() {
    return ConvertUtils.convertElementPropertyToList(authorityList, "id");
    }
    @Override
    public String toString() {
    return ToStringBuilder.reflectionToString(this);
    }
    }
    /*-----------第二种情况-------------*/
    @MappedSuperclass
    public abstract class IdEntity {
    protected Long id;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
    return id;
    }
    public void setId(Long id) {
    this.id = id;
    }
    }
    /********************************************************** Filter *************************************************************************/
    private UserDetailsService userDetailsService;
    @Autowired
    public void setUserDetailsService(UserDetailsService userDetailsService) {
    this.userDetailsService = userDetailsService;
    }
    private UserDetailsService userDetailsService;
    @Required
    public void setDefaultUserName(String defaultUserName) {
    this.defaultUserName = defaultUserName;
    }
    /************************************************************其它*******************************************************************************/
    @Override:只能用在方法之上的,用来告诉别人这一个方法是改写父类的。 
    @Deprecated:建议别人不要使用旧的API的时候用的,编译的时候会用产生警告信息,可以设定在程序里的所有的元素上
    @Documented  @Documented的目的就是让这一个Annotation类型的信息能够显示在javaAPI说明文档上;没有添加的话,使用javadoc生成API文档的时候就会找不到这一个
        类型生成的信息. 
    @Retention(RetentionPolicy.RUNTIME)   
    @Target(ElementType.ANNOTATION_TYPE):@Target里面的ElementType是用来指定Annotation类型可以用在哪一些元素上的.说明一下:TYPE(类型), FIELD(属性),   
        METHOD( 方法), PARAMETER(参数), CONSTRUCTOR(构造函数),LOCAL_VARIABLE(局部变量), ANNOTATION_TYPE,PACKAGE(包),其中的TYPE(类型)是指可以用在
  • 相关阅读:
    Linux--shell的awk--10
    Spring Boot 整合 tk.mybatis
    pring Boot 整合 Druid
    Thymeleaf 模板布局
    Thymeleaf 内置对象
    Thymeleaf 表达式语法
    Thymeleaf 参考手册
    Thymeleaf常用语法
    Thymeleaf简介及第一个thymeleaf模板
    Docker 安装nginx
  • 原文地址:https://www.cnblogs.com/daniell003/p/3542747.html
Copyright © 2011-2022 走看看