博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringMVC+hibernate+mysql环境搭建后测试例中使用的注解总结
阅读量:7227 次
发布时间:2019-06-29

本文共 2157 字,大约阅读时间需要 7 分钟。

hot3.png

Controller层

@Controller

表明某类是一个controller控制器,xxx-servlet.xml文件里写有扫描语句来扫描指定包,在指定包中找到标有@Controller的类,xxx-servlet.xml文件会将web.xml中拦截的query请求送到Controller层中标有@Controller注解的类进行匹配。

@RequestMapping

用于请求与controller类、方法匹配使用

写法:写在@Controller与定义类中间

@Controller

@RequestMapping("/student.do") 

public class StudentController{

    @RequestMapping(params = "method=add")

     public 
String add(HttpServletRequest request, ModelMap modelMap) throws 
Exception{
            return 
"student_add";
     }
}

调用add()的js方法

function add(){   
    window.location.href="<%=request.getContextPath() %>/student.do?method=add";   

@RequestParam

在SpringMVC后台Controller控制层获取前台页面参数的方式主要有两种,一种是request.getParameter("name"),另外一种是用注解@RequestParam直接获取

例:

Controller层:

@RequestMapping("testRequestParam")     

public String filesUpload(@RequestParam String inputStr, HttpServletRequest request) {     

     System.out.println(inputStr);   

     int inputInt = Integer.valueOf(request.getParameter("inputInt"));   

     System.out.println(inputInt);   

     // ......省略   

    return "index";   

 } 

前端:

<form action="/gadget/testRequestParam" method="post">     

     参数inputStr:<input type="text" name="inputStr">     

     参数intputInt:<input type="text" name="inputInt">     

</form>  

测试:

152219_dXJw_2711560.png

执行结果:

test1
123

@Autowired

(在Dao层与Service层也使用了该注解)

可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。

自我总结: @Autowired注解主要使用在定义自己写得Service与dao方法但在实体类中并不需要使用

用法例controller层:

public class StudentController{     protected final transient Log log = LogFactory.getLog(StudentController.class);           @Autowired     public StudentService studentService;     //........方法体}

用法例dao层:

public class EntityDao {       @Autowired       private SessionFactory sessionFactory;     //........方法体       }

用法例Service层:

public class StudentService {     @Autowired     public EntityDao entityDao;          //........方法体       }

Dao层

@Repository

@Repository对应数据访问层Bean,按编写规范而言就是用来标注Dao层的说明类型注解,与@Controller平级用法也相同.

@Repository写在Dao类上一行后 xxx-servlet.xml文件通过扫描语句来扫描指定包从而加快扫描速度.

写法: @Repository 或 @Repository("userDao") 或 @Repository( value="userDao")

@Repository(value="userDao")注解是告诉Spring,让Spring创建一个名字叫“userDao”的UserDaoImpl实例。

用法实例:

@Repository("entityDao")public class EntityDao {    //.......方法体}

转载于:https://my.oschina.net/fal6112/blog/668141

你可能感兴趣的文章
Python学习教程(Python学习路线):Python 3—手动创建迭代器
查看>>
说说如何在 Virtual Box 中新建 CentOS 虚拟机
查看>>
Cordova + Vue 实现点击两次退出应用
查看>>
JAVA 多用户商城系统b2b2c-Spring Cloud Stream 介绍
查看>>
spring cloud构建互联网分布式微服务云平台-SpringCloud集成项目简介
查看>>
基于房源的画像分析
查看>>
80% UI 初学者走过的弯路,你走了几条?
查看>>
文档和元素的几何滚动
查看>>
php 设计模式
查看>>
Java springcloud B2B2C o2o多用户商城 springcloud架构(八)springboot整合mongodb
查看>>
3年工作经验的Java程序员面试经过
查看>>
Mysql 批量写入数据,对于这类性能问题,你是如何优化的
查看>>
MySQL无法启动几种常见问题小结
查看>>
阿里CTO:阿里所有技术和产品输出都将必须通过阿里云进行
查看>>
更好用的集群限流功能,Sentinel 发布 v1.4.2
查看>>
Python(生成执行文件)
查看>>
redis安装配置 - ttlsa教程系列之redis
查看>>
Linux --DHCP服务器配置;DHCP服务器中继
查看>>
IE版本多的可爱_已迁移
查看>>
eclipse查看jar包中class的中文注释乱码问题的解决
查看>>