- 浏览: 3369448 次
- 性别:
- 来自: 珠海
文章分类
- 全部博客 (1633)
- Java (250)
- Android&HTML5 (111)
- Struts (10)
- Spring (236)
- Hibernate&MyBatis (115)
- SSH (49)
- jQuery插件收集 (55)
- Javascript (145)
- PHP (77)
- REST&WebService (18)
- BIRT (27)
- .NET (7)
- Database (105)
- 设计模式 (16)
- 自动化和测试 (19)
- Maven&Ant (43)
- 工作流 (36)
- 开源应用 (156)
- 其他 (16)
- 前台&美工 (119)
- 工作积累 (0)
- OS&Docker (83)
- Python&爬虫 (28)
- 工具软件 (157)
- 问题收集 (61)
- OFbiz (6)
- noSQL (12)
最新评论
-
HEZR曾嶸:
你好博主,这个不是很理解,能解释一下嘛//左边+1,上边+1, ...
java 两字符串相似度计算算法 -
天使建站:
写得不错,可以看这里,和这里的这篇文章一起看,有 ...
jquery 遍历对象、数组、集合 -
xue88ming:
很有用,谢谢
@PathVariable映射出现错误: Name for argument type -
jnjeC:
厉害,困扰了我很久
MyBatis排序时使用order by 动态参数时需要注意,用$而不是# -
TopLongMan:
非常好,很实用啊。。
PostgreSQL递归查询实现树状结构查询
详解Spring MVC 4常用的那些注解 http://favccxx.blog.51cto.com/2890523/1582185
SpringMVC 常见注解 http://qing.blog.sina.com.cn/3225985210/c048a0ba33003w90.html
1• @Controller
2• @Service
3• @Autowired
4• @RequestMapping
5• @RequestParam
6• @ModelAttribute
7• @Cacheable
8• @CacheFlush
9• @Resource
10• @PostConstruct
11• @PreDestroy
12• @Repository
13• @Component (不推荐使用)
14• @Scope
15• @SessionAttributes
16• @InitBinder
17• @Required
18• @Qualifier
19• @PathVariable
20• @Valid
21• @AttributeOverride
22• @Column
1• @Controller
例如
@Controller
public class SoftCreateController extends SimpleBaseController {}
或者
@Controller("softCreateController")
说明
@Controller 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写
2• @Service
例如
@Service
public class SoftCreateServiceImpl implements ISoftCreateService {}
或者
@Service("softCreateServiceImpl")
说明
@Service 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写
3• @Autowired
例如
@Autowired
private ISoftPMService softPMService;
或者
@Autowired(required=false)
private ISoftPMService softPMService = new SoftPMServiceImpl();
说明
@Autowired 根据bean 类型从spring 上线文中进行查找,注册类型必须唯一,否则报异常。与@Resource 的区别在于,@Resource 允许通过bean 名称或bean 类型两种方式进行查找@Autowired(required=false) 表示,如果spring 上下文中没有找到该类型的bean 时, 才会使用new SoftPMServiceImpl();
@Autowired 标注作用于 Map 类型时,如果 Map 的 key 为 String 类型,则 Spring 会将容器中所有类型符合 Map 的 value 对应的类型的 Bean 增加进来,用 Bean 的 id 或 name 作为 Map 的 key。
@Autowired 还有一个作用就是,如果将其标注在 BeanFactory 类型、ApplicationContext 类型、ResourceLoader 类型、ApplicationEventPublisher 类型、MessageSource 类型上,那么 Spring 会自动注入这些实现类的实例,不需要额外的操作。
4• @RequestMapping
类
@Controller
@RequestMapping("/bbtForum.do")
public class BbtForumController {
@RequestMapping(params = "method=listBoardTopic")
public String listBoardTopic(int topicId,User user) {}
}
方法
@RequestMapping("/softpg/downSoftPg.do")
@RequestMapping(value="/softpg/ajaxLoadSoftId.do",method = POST)
@RequestMapping(value = "/osu/product/detail.do", params = { "modify=false" }, method =POST)
说明
@RequestMapping 可以声明到类或方法上
参数绑定说明
如果我们使用以下的 URL 请求:
http://localhost/bbtForum.do?method=listBoardTopic&topicId=1&userId=10&userName=tom
topicId URL 参数将绑定到 topicId 入参上,而 userId 和 userName URL 参数将绑定到 user 对象的 userId 和 userName 属性中。和 URL 请求中不允许没有 topicId 参数不同,虽然 User 的 userId 属性的类型是基本数据类型,但如果 URL 中不存在 userId 参数,Spring 也不会报错,此时 user.userId 值为 0 。如果 User 对象拥有一个 dept.deptId 的级联属性,那么它将和 dept.deptId URL 参数绑定。
5• @RequestParam
参数绑定说明
@RequestParam("id")
http://localhost/bbtForum.do?method=listBoardTopic&id=1&userId=10&userName=tom
listBoardTopic(@RequestParam("id")int topicId,User user) 中的 topicId 绑定到 id 这个 URL 参数, 那么可以通过对入参使用 @RequestParam 注解来达到目的
@RequestParam(required=false):参数不是必须的,默认为true
@RequestParam(value="id",required=false)
请求处理方法入参的可选类型
* Java 基本数据类型和 String
默认情况下将按名称匹配的方式绑定到 URL 参数上,可以通过 @RequestParam 注解改变默认的绑定规则
* request/response/session
既可以是 Servlet API 的也可以是 Portlet API 对应的对象,Spring 会将它们绑定到Servlet 和 Portlet 容器的相应对象上
* org.springframework.web.context.request.WebRequest
内部包含了 request 对象
* java.util.Locale
绑定到 request 对应的 Locale 对象上
* java.io.InputStream/java.io.Reader
可以借此访问 request 的内容
* java.io.OutputStream / java.io.Writer
可以借此操作 response 的内容
* 任何标注了 @RequestParam 注解的入参
被标注 @RequestParam 注解的入参将绑定到特定的 request 参数上。
* java.util.Map / org.springframework.ui.ModelMap
它绑定 Spring MVC 框架中每个请求所创建的潜在的模型对象,它们可以被 Web 视图对象访问(如 JSP )
* 命令/ 表单对象(注:一般称绑定使用 HTTP GET 发送的 URL 参数的对象为命令对象,而称绑定使用HTTP POST 发送的 URL 参数的对象为表单对象)
它们的属性将以名称匹配的规则绑定到 URL 参数上,同时完成类型的转换。
而类型转换的规则可以通过 @InitBinder 注解或通过 HandlerAdapter 的配置进行调 整
* org.springframework.validation.Errors / org.springframework.validation.BindingResult
为属性列表中的命令/ 表单对象的校验结果,注意检验结果参数必须紧跟在命令/ 表单对象的后面
* org.springframework.web.bind.support.SessionStatus
可以通过该类型 status 对象显式结束表单的处理,这相当于触发 session 清除其中的通过@SessionAttributes 定义的属性
请求处理方法返回值的可选类型
* void
此时逻辑视图名由请求处理方法对应的 URL 确定,如以下的方法:
@RequestMapping("/welcome.do")
public void welcomeHandler() {}
对应的逻辑视图名为 “ welcome ”
* String
此时逻辑视图名为返回的字符,如以下的方法:
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("ownerId") int ownerId, ModelMap model) {
Owner owner = this.clinic.loadOwner(ownerId);
model.addAttribute(owner);
return "ownerForm";
}
对应的逻辑视图名为 “ ownerForm ”
* org.springframework.ui.ModelMap
和返回类型为 void 一样,逻辑视图名取决于对应请求的 URL ,如下面的例子:
@RequestMapping("/vets.do")
public ModelMap vetsHandler() {
return new ModelMap(this.clinic.getVets());
}
对应的逻辑视图名为 “ vets ” ,返回的 ModelMap 将被作为请求对应的模型对象,可以在 JSP 视图页面中访问到。
* ModelAndView
当然还可以是传统的 ModelAndView 。
6• @ModelAttribute
作用域:request
例如
@RequestMapping("/base/userManageCooper/init.do")
public String handleInit(@ModelAttribute("queryBean") ManagedUser sUser,Model model,){
或者
@ModelAttribute("coopMap")// 将coopMap 返回到页 面
public Map<Long,CooperatorInfo> coopMapItems(){}
说明
@ModelAttribute 声明在属性上,表示该属性的value 来源于model 里"queryBean" ,并被保存到model 里
@ModelAttribute 声明在方法上,表示该方法的返回值被保存到model 里
• @Cacheable 和@CacheFlush
7• @Cacheable :声明一个方法的返回值应该被缓 存
例如:@Cacheable(modelId = "testCaching")
8• @CacheFlush :声明一个方法是清空缓存的触发器
例如:@CacheFlush(modelId = "testCaching")
说明
要配合缓存处理器使用,参考: http://hanqunfeng.iteye.com/blog/603719
9• @Resource
例如
@Resource
private DataSource dataSource; // inject the bean named 'dataSource'
或者
@Resource(name="dataSource")
@Resource(type=DataSource.class)
说明
@Resource 默认按bean 的name 进行查找,如果没有找到会按type 进行查找,
此时与@Autowired 类 似
在没有为 @Resource 注解显式指定 name 属性的前提下,如果将其标注在 BeanFactory 类型、ApplicationContext 类型、ResourceLoader 类型、ApplicationEventPublisher 类型、MessageSource 类型上,那么 Spring 会自动注入这些实现类的实例,不需要额外的操作。此时 name 属性不需要指定 ( 或者指定为""),否则注入失败;
• @PostConstruct 和@PreDestroy
10• @PostConstruct
在方法上加上注解@PostConstruct ,这个方法就会在Bean 初始化之后被Spring 容器执 行
(注:Bean 初始化包括,实例化Bean ,并装配Bean 的属性(依赖注入))。
11• @PreDestroy
在方法上加上注解@PreDestroy ,这个方法就会在Bean 被销毁前被Spring 容器执行。
12• @Repository
与@Controller 、@Service 类似,都是向spring 上下文中注册bean ,不在赘述。
13• @Component (不推荐使用)
@Component 是所有受Spring 管理组件的通用形式,Spring 还提供了更加细化的注解形式: @Repository 、@Service 、@Controller ,它们分别对应存储层Bean ,业务层Bean ,和展示层Bean 。
目前版本(2.5 )中,这些注解与@Component 的语义是一样的,完全通用, 在Spring 以后的版本中可能会给它们追加更多的语义。 所以,我们推荐使用@Repository 、@Service 、@Controller 来替代@Component 。
14• @Scope
例如
@Scope("session")
@Repository()
public class UserSessionBean implementsSerializable {}
说明
在使用XML 定义Bean 时,可以通过bean 的scope 属性来定义一个Bean 的作用范围,
同样可以通过@Scope 注解来完成
@Scope中可以指定如下值:
singleton:定义bean的范围为每个spring容器一个实例(默认值)
prototype:定义bean可以被多次实例化(使用一次就创建一次)
request:定义bean的范围是http请求(springMVC中有效)
session:定义bean的范围是http会话(springMVC中有效)
global-session:定义bean的范围是全局http会话(portlet中有效)
15• @SessionAttributes
说明
Spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,
以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。
这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。
@SessionAttributes 只能声明在类上,而不能声明在方法上。
例如
@SessionAttributes("currUser") // 将ModelMap 中属性名为currUser 的属性
@SessionAttributes({"attr1","attr2"})
@SessionAttributes(types = User.class)
@SessionAttributes(types = {User.class,Dept.class})
@SessionAttributes(types = {User.class,Dept.class},value={"attr1","attr2"})
16• @InitBinder
说明
如果希望某个属性编辑器仅作用于特定的 Controller ,
可以在 Controller 中定义一个标注 @InitBinder 注解的方法,
可以在该方法中向 Controller 了注册若干个属性编辑器
例如
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
17• @Required
例如
@required
public setName(String name){}
说明
@ required 负责检查一个bean在初始化时其声明的 set方法是否被执行, 当某个被标注了 @Required 的 Setter 方法没有被调用,则 Spring 在解析的时候会抛出异常,以提醒开发者对相应属性进行设置。 @Required 注解只能标注在 Setter 方法之上。因为依赖注入的本质是检查 Setter 方法是否被调用了,而不是真的去检查属性是否赋值了以及赋了什么样的值。如果将该注解标注在非 setXxxx() 类型的方法则被忽略。
18• @Qualifier
例如
@Autowired
@Qualifier("softService")
private ISoftPMService softPMService;
说明
使用@Autowired 时,如果找到多个同一类型的bean,则会抛异常,此时可以使用 @Qualifier("beanName"),明确指定bean的名称进行注入,此时与 @Resource指定name属性作用相同。
19• @PathVariable
@PathVariable是用来对指定请求的URL路径里面的变量
eg: Java代码 @RequestMapping(value = "form/{id}/apply", method = {RequestMethod.PUT, RequestMethod.POST}) {id}
在这个请求的URL里就是个变量,可以使用@PathVariable来获取
20• @valid
@Valid Production production //实体设置+类+类的引用 直接将页面传过来的production对象中的信息封装到里面去了
21• @AttributeOverride
@AttributeOverride表示属性的映射,而@AttributeOverrides由多个@AttributeOverride 注释组成,每个@AttributeOverride表示属性的映射,它的定义如以下所示:
@Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME)
public @interface AttributeOverride {
String name();
Column column();
}
在使用@AttributeOverride注释应注意以下几方面的问题:
☆ name属性表示嵌入式类中的属性名称。
☆ column属性表示,所嵌入的实体类中的列定义,其中@Column标记表示的意义见第22项。
☆ 例如将tb_customer表中的customer_zip字段映射为Address中的属性zip。代码如下所示:
@AttributeOverride(name = "zip",
column = @Column(name = "customer_zip")
)
☆ 使用嵌入式类的好处是:多个实体中都可以共享一个嵌入式类,方便了对实体的操作。例如现在ContactEO也嵌入Address类,就很方便的映射为以下所示:
public class CustomerEO implements Serializable {
private Integer id;
private String name;
private String nickname;
……getter和setter方法省略
private Address address;
@Embedded
@AttributeOverrides( {
@AttributeOverride(name = "zip", column = @Column(name = "contact_zip")),
@AttributeOverride(name = "line1", column = @Column(name = "contact_line1")),
})
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
22• @Column
@Column标记表示所持久化属性所映射表中的字段,该注释的属性定义如下:
@Target({METHOD, FIELD}) @Retention(RUNTIME)
public @interface Column {
String name() default "";
boolean unique() default false;
boolean nullable() default true;
boolean insertable() default true;
boolean updatable() default true;
String columnDefinition() default "";
String table() default "";
int length() default 255;
int precision() default 0;
int scale() default 0;
}
在使用此@Column标记时,需要注意以下几个问题:
l 此标记可以标注在getter方法或属性前,例如以下的两种标注方法都是正确的:
标注在属性前:
@Entity
@Table(name = "contact")
public class ContactEO{
@Column(name=" contact_name ")
private String name;
}
标注在getter方法前:
@Entity
@Table(name = "contact")
public class ContactEO{
@Column(name=" contact_name ")
public String getName() {
return name;
}
}
提示:JPA规范中并没有明确指定那种标注方法,只要两种标注方式任选其一都可以。这根据个人的喜好来选择,笔者习惯使用第二种方法。
l unique属性表示该字段是否为唯一标识,默认为false。如果表中有一个字段需要唯一标识,则既可以使用该标记,也可以使用@Table标记中的@UniqueConstraint。
l nullable属性表示该字段是否可以为null值,默认为true。
l insertable属性表示在使用“INSERT”脚本插入数据时,是否需要插入该字段的值。
l updatable属性表示在使用“UPDATE”脚本插入数据时,是否需要更新该字段的值。insertable和updatable属性一般多用于只读的属性,例如主键和外键等。这些字段的值通常是自动生成的。
l columnDefinition属性表示创建表时,该字段创建的SQL语句,一般用于通过Entity生成表定义时使用。
l table属性表示当映射多个表时,指定表的表中的字段。默认值为主表的表名。有关多个表的映射将在本章的5.6小节中详细讲述。
l length属性表示字段的长度,当字段的类型为varchar时,该属性才有效,默认为255个字符。
l precision属性和scale属性表示精度,当字段类型为double时,precision表示数值的总长度,scale表示小数点所占的位数。
下面举几个小例子:
示例一:指定字段“contact_name”的长度是“512”,并且值不能为null。
private String name;
@Column(name="contact_name",nullable=false,length=512)
public String getName() {
return name;
}
创建的SQL语句如下所示。
CREATE TABLE contact (
id integer not null,
contact_name varchar (512) not null,
primary key (id)
)
示例二:指定字段“monthly_income”月收入的类型为double型,精度为12位,小数点位数为2位。
private BigDecimal monthlyIncome;
@Column(name="monthly_income",precision=12, scale=2)
public BigDecimal getMonthlyIncome() {
return monthlyIncome;
}
创建的SQL语句如下所示。
CREATE TABLE contact (
id integer not null,
monthly_income double(12,2),
primary key (id)
)
示例三:自定义生成CLOB类型字段的SQL语句。
private String name;
@Column(name=" contact_name ",columnDefinition="clob not null")
public String getName() {
return name;
}
生成表的定义SQL语句如下所示。
CREATE TABLE contact (
id integer not null,
contact_name clob (200) not null,
primary key (id)
)
其中,加粗的部分为columnDefinition属性设置的值。若不指定该属性,通常使用默认的类型建表,若此时需要自定义建表的类型时,可在该属性中设置。
提示:通过Entity定义生成表,还是通过表配置Entity,这两种ORM的策略。有关两种方法的映射策略好坏,将在本书的章节中“JPA工具的使用”一章进行详细的比较。
示例四:字段值为只读的,不允许插入和修改。通常用于主键和外键。
private Integer id;
@Column(name="id",insertable=false,updatable=false)
public Integer getId() {
return id;
}
SpringMVC 常见注解 http://qing.blog.sina.com.cn/3225985210/c048a0ba33003w90.html
1• @Controller
2• @Service
3• @Autowired
4• @RequestMapping
5• @RequestParam
6• @ModelAttribute
7• @Cacheable
8• @CacheFlush
9• @Resource
10• @PostConstruct
11• @PreDestroy
12• @Repository
13• @Component (不推荐使用)
14• @Scope
15• @SessionAttributes
16• @InitBinder
17• @Required
18• @Qualifier
19• @PathVariable
20• @Valid
21• @AttributeOverride
22• @Column
1• @Controller
例如
@Controller
public class SoftCreateController extends SimpleBaseController {}
或者
@Controller("softCreateController")
说明
@Controller 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写
2• @Service
例如
@Service
public class SoftCreateServiceImpl implements ISoftCreateService {}
或者
@Service("softCreateServiceImpl")
说明
@Service 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写
3• @Autowired
例如
@Autowired
private ISoftPMService softPMService;
或者
@Autowired(required=false)
private ISoftPMService softPMService = new SoftPMServiceImpl();
说明
@Autowired 根据bean 类型从spring 上线文中进行查找,注册类型必须唯一,否则报异常。与@Resource 的区别在于,@Resource 允许通过bean 名称或bean 类型两种方式进行查找@Autowired(required=false) 表示,如果spring 上下文中没有找到该类型的bean 时, 才会使用new SoftPMServiceImpl();
@Autowired 标注作用于 Map 类型时,如果 Map 的 key 为 String 类型,则 Spring 会将容器中所有类型符合 Map 的 value 对应的类型的 Bean 增加进来,用 Bean 的 id 或 name 作为 Map 的 key。
@Autowired 还有一个作用就是,如果将其标注在 BeanFactory 类型、ApplicationContext 类型、ResourceLoader 类型、ApplicationEventPublisher 类型、MessageSource 类型上,那么 Spring 会自动注入这些实现类的实例,不需要额外的操作。
4• @RequestMapping
类
@Controller
@RequestMapping("/bbtForum.do")
public class BbtForumController {
@RequestMapping(params = "method=listBoardTopic")
public String listBoardTopic(int topicId,User user) {}
}
方法
@RequestMapping("/softpg/downSoftPg.do")
@RequestMapping(value="/softpg/ajaxLoadSoftId.do",method = POST)
@RequestMapping(value = "/osu/product/detail.do", params = { "modify=false" }, method =POST)
说明
@RequestMapping 可以声明到类或方法上
参数绑定说明
如果我们使用以下的 URL 请求:
http://localhost/bbtForum.do?method=listBoardTopic&topicId=1&userId=10&userName=tom
topicId URL 参数将绑定到 topicId 入参上,而 userId 和 userName URL 参数将绑定到 user 对象的 userId 和 userName 属性中。和 URL 请求中不允许没有 topicId 参数不同,虽然 User 的 userId 属性的类型是基本数据类型,但如果 URL 中不存在 userId 参数,Spring 也不会报错,此时 user.userId 值为 0 。如果 User 对象拥有一个 dept.deptId 的级联属性,那么它将和 dept.deptId URL 参数绑定。
5• @RequestParam
参数绑定说明
@RequestParam("id")
http://localhost/bbtForum.do?method=listBoardTopic&id=1&userId=10&userName=tom
listBoardTopic(@RequestParam("id")int topicId,User user) 中的 topicId 绑定到 id 这个 URL 参数, 那么可以通过对入参使用 @RequestParam 注解来达到目的
@RequestParam(required=false):参数不是必须的,默认为true
@RequestParam(value="id",required=false)
请求处理方法入参的可选类型
* Java 基本数据类型和 String
默认情况下将按名称匹配的方式绑定到 URL 参数上,可以通过 @RequestParam 注解改变默认的绑定规则
* request/response/session
既可以是 Servlet API 的也可以是 Portlet API 对应的对象,Spring 会将它们绑定到Servlet 和 Portlet 容器的相应对象上
* org.springframework.web.context.request.WebRequest
内部包含了 request 对象
* java.util.Locale
绑定到 request 对应的 Locale 对象上
* java.io.InputStream/java.io.Reader
可以借此访问 request 的内容
* java.io.OutputStream / java.io.Writer
可以借此操作 response 的内容
* 任何标注了 @RequestParam 注解的入参
被标注 @RequestParam 注解的入参将绑定到特定的 request 参数上。
* java.util.Map / org.springframework.ui.ModelMap
它绑定 Spring MVC 框架中每个请求所创建的潜在的模型对象,它们可以被 Web 视图对象访问(如 JSP )
* 命令/ 表单对象(注:一般称绑定使用 HTTP GET 发送的 URL 参数的对象为命令对象,而称绑定使用HTTP POST 发送的 URL 参数的对象为表单对象)
它们的属性将以名称匹配的规则绑定到 URL 参数上,同时完成类型的转换。
而类型转换的规则可以通过 @InitBinder 注解或通过 HandlerAdapter 的配置进行调 整
* org.springframework.validation.Errors / org.springframework.validation.BindingResult
为属性列表中的命令/ 表单对象的校验结果,注意检验结果参数必须紧跟在命令/ 表单对象的后面
* org.springframework.web.bind.support.SessionStatus
可以通过该类型 status 对象显式结束表单的处理,这相当于触发 session 清除其中的通过@SessionAttributes 定义的属性
请求处理方法返回值的可选类型
* void
此时逻辑视图名由请求处理方法对应的 URL 确定,如以下的方法:
@RequestMapping("/welcome.do")
public void welcomeHandler() {}
对应的逻辑视图名为 “ welcome ”
* String
此时逻辑视图名为返回的字符,如以下的方法:
@RequestMapping(method = RequestMethod.GET)
public String setupForm(@RequestParam("ownerId") int ownerId, ModelMap model) {
Owner owner = this.clinic.loadOwner(ownerId);
model.addAttribute(owner);
return "ownerForm";
}
对应的逻辑视图名为 “ ownerForm ”
* org.springframework.ui.ModelMap
和返回类型为 void 一样,逻辑视图名取决于对应请求的 URL ,如下面的例子:
@RequestMapping("/vets.do")
public ModelMap vetsHandler() {
return new ModelMap(this.clinic.getVets());
}
对应的逻辑视图名为 “ vets ” ,返回的 ModelMap 将被作为请求对应的模型对象,可以在 JSP 视图页面中访问到。
* ModelAndView
当然还可以是传统的 ModelAndView 。
6• @ModelAttribute
作用域:request
例如
@RequestMapping("/base/userManageCooper/init.do")
public String handleInit(@ModelAttribute("queryBean") ManagedUser sUser,Model model,){
或者
@ModelAttribute("coopMap")// 将coopMap 返回到页 面
public Map<Long,CooperatorInfo> coopMapItems(){}
说明
@ModelAttribute 声明在属性上,表示该属性的value 来源于model 里"queryBean" ,并被保存到model 里
@ModelAttribute 声明在方法上,表示该方法的返回值被保存到model 里
• @Cacheable 和@CacheFlush
7• @Cacheable :声明一个方法的返回值应该被缓 存
例如:@Cacheable(modelId = "testCaching")
8• @CacheFlush :声明一个方法是清空缓存的触发器
例如:@CacheFlush(modelId = "testCaching")
说明
要配合缓存处理器使用,参考: http://hanqunfeng.iteye.com/blog/603719
9• @Resource
例如
@Resource
private DataSource dataSource; // inject the bean named 'dataSource'
或者
@Resource(name="dataSource")
@Resource(type=DataSource.class)
说明
@Resource 默认按bean 的name 进行查找,如果没有找到会按type 进行查找,
此时与@Autowired 类 似
在没有为 @Resource 注解显式指定 name 属性的前提下,如果将其标注在 BeanFactory 类型、ApplicationContext 类型、ResourceLoader 类型、ApplicationEventPublisher 类型、MessageSource 类型上,那么 Spring 会自动注入这些实现类的实例,不需要额外的操作。此时 name 属性不需要指定 ( 或者指定为""),否则注入失败;
• @PostConstruct 和@PreDestroy
10• @PostConstruct
在方法上加上注解@PostConstruct ,这个方法就会在Bean 初始化之后被Spring 容器执 行
(注:Bean 初始化包括,实例化Bean ,并装配Bean 的属性(依赖注入))。
11• @PreDestroy
在方法上加上注解@PreDestroy ,这个方法就会在Bean 被销毁前被Spring 容器执行。
12• @Repository
与@Controller 、@Service 类似,都是向spring 上下文中注册bean ,不在赘述。
13• @Component (不推荐使用)
@Component 是所有受Spring 管理组件的通用形式,Spring 还提供了更加细化的注解形式: @Repository 、@Service 、@Controller ,它们分别对应存储层Bean ,业务层Bean ,和展示层Bean 。
目前版本(2.5 )中,这些注解与@Component 的语义是一样的,完全通用, 在Spring 以后的版本中可能会给它们追加更多的语义。 所以,我们推荐使用@Repository 、@Service 、@Controller 来替代@Component 。
14• @Scope
例如
@Scope("session")
@Repository()
public class UserSessionBean implementsSerializable {}
说明
在使用XML 定义Bean 时,可以通过bean 的scope 属性来定义一个Bean 的作用范围,
同样可以通过@Scope 注解来完成
@Scope中可以指定如下值:
singleton:定义bean的范围为每个spring容器一个实例(默认值)
prototype:定义bean可以被多次实例化(使用一次就创建一次)
request:定义bean的范围是http请求(springMVC中有效)
session:定义bean的范围是http会话(springMVC中有效)
global-session:定义bean的范围是全局http会话(portlet中有效)
15• @SessionAttributes
说明
Spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,
以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。
这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。
@SessionAttributes 只能声明在类上,而不能声明在方法上。
例如
@SessionAttributes("currUser") // 将ModelMap 中属性名为currUser 的属性
@SessionAttributes({"attr1","attr2"})
@SessionAttributes(types = User.class)
@SessionAttributes(types = {User.class,Dept.class})
@SessionAttributes(types = {User.class,Dept.class},value={"attr1","attr2"})
16• @InitBinder
说明
如果希望某个属性编辑器仅作用于特定的 Controller ,
可以在 Controller 中定义一个标注 @InitBinder 注解的方法,
可以在该方法中向 Controller 了注册若干个属性编辑器
例如
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
17• @Required
例如
@required
public setName(String name){}
说明
@ required 负责检查一个bean在初始化时其声明的 set方法是否被执行, 当某个被标注了 @Required 的 Setter 方法没有被调用,则 Spring 在解析的时候会抛出异常,以提醒开发者对相应属性进行设置。 @Required 注解只能标注在 Setter 方法之上。因为依赖注入的本质是检查 Setter 方法是否被调用了,而不是真的去检查属性是否赋值了以及赋了什么样的值。如果将该注解标注在非 setXxxx() 类型的方法则被忽略。
18• @Qualifier
例如
@Autowired
@Qualifier("softService")
private ISoftPMService softPMService;
说明
使用@Autowired 时,如果找到多个同一类型的bean,则会抛异常,此时可以使用 @Qualifier("beanName"),明确指定bean的名称进行注入,此时与 @Resource指定name属性作用相同。
19• @PathVariable
@PathVariable是用来对指定请求的URL路径里面的变量
eg: Java代码 @RequestMapping(value = "form/{id}/apply", method = {RequestMethod.PUT, RequestMethod.POST}) {id}
在这个请求的URL里就是个变量,可以使用@PathVariable来获取
20• @valid
@Valid Production production //实体设置+类+类的引用 直接将页面传过来的production对象中的信息封装到里面去了
21• @AttributeOverride
@AttributeOverride表示属性的映射,而@AttributeOverrides由多个@AttributeOverride 注释组成,每个@AttributeOverride表示属性的映射,它的定义如以下所示:
@Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME)
public @interface AttributeOverride {
String name();
Column column();
}
在使用@AttributeOverride注释应注意以下几方面的问题:
☆ name属性表示嵌入式类中的属性名称。
☆ column属性表示,所嵌入的实体类中的列定义,其中@Column标记表示的意义见第22项。
☆ 例如将tb_customer表中的customer_zip字段映射为Address中的属性zip。代码如下所示:
@AttributeOverride(name = "zip",
column = @Column(name = "customer_zip")
)
☆ 使用嵌入式类的好处是:多个实体中都可以共享一个嵌入式类,方便了对实体的操作。例如现在ContactEO也嵌入Address类,就很方便的映射为以下所示:
public class CustomerEO implements Serializable {
private Integer id;
private String name;
private String nickname;
……getter和setter方法省略
private Address address;
@Embedded
@AttributeOverrides( {
@AttributeOverride(name = "zip", column = @Column(name = "contact_zip")),
@AttributeOverride(name = "line1", column = @Column(name = "contact_line1")),
})
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
22• @Column
@Column标记表示所持久化属性所映射表中的字段,该注释的属性定义如下:
@Target({METHOD, FIELD}) @Retention(RUNTIME)
public @interface Column {
String name() default "";
boolean unique() default false;
boolean nullable() default true;
boolean insertable() default true;
boolean updatable() default true;
String columnDefinition() default "";
String table() default "";
int length() default 255;
int precision() default 0;
int scale() default 0;
}
在使用此@Column标记时,需要注意以下几个问题:
l 此标记可以标注在getter方法或属性前,例如以下的两种标注方法都是正确的:
标注在属性前:
@Entity
@Table(name = "contact")
public class ContactEO{
@Column(name=" contact_name ")
private String name;
}
标注在getter方法前:
@Entity
@Table(name = "contact")
public class ContactEO{
@Column(name=" contact_name ")
public String getName() {
return name;
}
}
提示:JPA规范中并没有明确指定那种标注方法,只要两种标注方式任选其一都可以。这根据个人的喜好来选择,笔者习惯使用第二种方法。
l unique属性表示该字段是否为唯一标识,默认为false。如果表中有一个字段需要唯一标识,则既可以使用该标记,也可以使用@Table标记中的@UniqueConstraint。
l nullable属性表示该字段是否可以为null值,默认为true。
l insertable属性表示在使用“INSERT”脚本插入数据时,是否需要插入该字段的值。
l updatable属性表示在使用“UPDATE”脚本插入数据时,是否需要更新该字段的值。insertable和updatable属性一般多用于只读的属性,例如主键和外键等。这些字段的值通常是自动生成的。
l columnDefinition属性表示创建表时,该字段创建的SQL语句,一般用于通过Entity生成表定义时使用。
l table属性表示当映射多个表时,指定表的表中的字段。默认值为主表的表名。有关多个表的映射将在本章的5.6小节中详细讲述。
l length属性表示字段的长度,当字段的类型为varchar时,该属性才有效,默认为255个字符。
l precision属性和scale属性表示精度,当字段类型为double时,precision表示数值的总长度,scale表示小数点所占的位数。
下面举几个小例子:
示例一:指定字段“contact_name”的长度是“512”,并且值不能为null。
private String name;
@Column(name="contact_name",nullable=false,length=512)
public String getName() {
return name;
}
创建的SQL语句如下所示。
CREATE TABLE contact (
id integer not null,
contact_name varchar (512) not null,
primary key (id)
)
示例二:指定字段“monthly_income”月收入的类型为double型,精度为12位,小数点位数为2位。
private BigDecimal monthlyIncome;
@Column(name="monthly_income",precision=12, scale=2)
public BigDecimal getMonthlyIncome() {
return monthlyIncome;
}
创建的SQL语句如下所示。
CREATE TABLE contact (
id integer not null,
monthly_income double(12,2),
primary key (id)
)
示例三:自定义生成CLOB类型字段的SQL语句。
private String name;
@Column(name=" contact_name ",columnDefinition="clob not null")
public String getName() {
return name;
}
生成表的定义SQL语句如下所示。
CREATE TABLE contact (
id integer not null,
contact_name clob (200) not null,
primary key (id)
)
其中,加粗的部分为columnDefinition属性设置的值。若不指定该属性,通常使用默认的类型建表,若此时需要自定义建表的类型时,可在该属性中设置。
提示:通过Entity定义生成表,还是通过表配置Entity,这两种ORM的策略。有关两种方法的映射策略好坏,将在本书的章节中“JPA工具的使用”一章进行详细的比较。
示例四:字段值为只读的,不允许插入和修改。通常用于主键和外键。
private Integer id;
@Column(name="id",insertable=false,updatable=false)
public Integer getId() {
return id;
}
发表评论
-
Spring Boot 属性配置
2016-06-24 11:04 1151Spring Boot 属性配置和使用 http://blog ... -
Spring Boot 集成MyBatis
2016-06-24 10:55 1994Spring Boot 集成MyBatis http://bl ... -
Spring MVC防重复提交
2016-06-17 15:47 1593http://my.oschina.net/zyqjustin ... -
Spring容器加载完之后执行特定任务
2016-06-17 15:36 2247http://my.oschina.net/simpleton ... -
使用spring-session和shiro来代理session的配置
2016-06-16 11:21 12000使用spring-session和redis来代理sessio ... -
JSTL 的 if else : 有 c:if 没有 else 的处理
2016-06-14 09:52 1298http://blog.csdn.net/xiyuan1999 ... -
spring mvc 请求转发和重定向
2016-06-14 09:48 1354http://blog.csdn.net/jackpk/art ... -
mvc:view-controller
2016-05-18 10:26 1054http://blog.csdn.net/lzwglory/a ... -
spring配置事物的方式:注解和aop配置
2016-05-14 00:26 4067参考: Spring AOP中pointcut express ... -
分布式任务调度组件 Uncode-Schedule
2016-05-13 14:47 2247http://www.oschina.net/p/uncode ... -
Mybatis分库分表扩展插件
2016-05-12 15:47 1590http://fangjialong.iteye.com/bl ... -
spring+mybatis+atomikos 实现JTA事务
2016-05-11 22:00 5478sping配置多个数据源 不同用户操作不同数据库 http:/ ... -
Spring中使用注解 @Scheduled执行定时任务
2016-05-10 09:39 1538原文:http://dwf07223.blog.51cto.c ... -
Spring中配置Websocket
2016-05-05 16:55 1249spring+websocket整合(springMVC+sp ... -
redis 集群中Session解决方案之Spring Session
2016-05-04 08:54 1277集群中Session解决方案之Spring Session h ... -
使用Spring-data进行Redis操作
2016-05-04 08:54 4740使用Spring-data进行Redis操作 http://z ... -
Spring4新特性——集成Bean Validation 1.1(JSR-349)到SpringMVC
2016-05-03 13:35 1032Spring4新特性——集成Bean Validation 1 ... -
SpringMVC介绍之Validation
2016-05-03 13:10 955SpringMVC介绍之Validation http://h ... -
spring 注解方式下使用commons-validator 验证表单
2016-05-03 11:08 3042原文: http://www.programgo.com/ar ... -
Spring MVC学习详解
2016-04-28 09:13 970原文 http://blog.csdn.net/alsocod ...
相关推荐
本篇文章主要介绍了SpringMVC Controller介绍及常用注解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
资源包括了springMvc常见的例子并附有源代码 上传文件 下载文件 处理ajax请求 返回json数据 踢掉在线用户 全注解形式
Spring和SpringMVC面试问题总结 1.Spring中AOP的应用场景、Aop原理、好处? 2.Spring中IOC的作用与原理?对象创建的过程。...4.Spring常见创建对象的注解? 5.Spring中用到的设计模式 6.Spring的优点? ......
spring的mvc,注解驱动.适合有一定基础的人看.解决了一些常见的问题.
文件上传是Web应用程序中常见的功能之一,Spring MVC提供了方便的机制来处理文件上传。下面是关于Spring MVC实现文件上传的详细描述: Spring MVC文件上传的实现步骤如下: 准备MultipartResolver: 在Spring MVC...
该文档主要整理的是SSM的常见面试题,包括一下内容: 1. SpringMVC 的工作原理 (11步) 2. 谈谈你对SpringMVC的理解 3. SpringMVC 常用注解都有哪些? 4. Spring 的常用注解 5. 如何开启注解处理器和适配器? 6...
- SpringBoot的核心注解是哪个? - Java API配置的好处 - SpringBoot自动配置原理 - SpringBoot配置文件加载顺序 - SpringBoot 怎么切换生产环境和开发环境配置 - SpringBoot是如何推断应用类型和main的 <!-- ...
300M资源,微服务架构面试专题系列(MySQL,JVM,并发编程,RabbitMQ消息中间件,Spring)。 囊括的知识点非常多 ...springMVC的原理 Spring MVC工作原理 及注解说明 SpringMVC框架介绍 SpringMVC原理
10G的Java面试题视频课 囊括了JAVA基础,MySQL,Spring框架,微服务,网络等等Java程序员需要掌握的各个知识领域 ...springMVC的原理 Spring MVC工作原理 及注解说明 SpringMVC框架介绍 SpringMVC原理
Java程序员大厂面试(进大厂必看) 囊括了JAVA基础,MySQL,Spring框架,微服务,网络等等Java程序员需要掌握的各个知识领域 ...springMVC的原理 Spring MVC工作原理 及注解说明 SpringMVC框架介绍 SpringMVC原理
1.5 SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结 1.6 »Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去其他地找了) 1.7 Spring开闭原则的表现-BeanPostProcessor扩展点-2 1.8 Spring...
4.SpringMVC注解 @Controller @RestController @RequestMapping @RequestBody @ResponseBody @RequestParam @PathViriable @Component 在类定义之前添加@Component注解,他会被spring容器识别,并转为bean。 @...
包含了阿里,百度,腾讯,京东等互联网大厂的面试真题 Java程序员大厂面试(进大厂必看) 囊括了JAVA基础,MySQL,Spring框架,微服务,网络等等Java程序员...springMVC的原理 Spring MVC工作原理 及注解说明 SpringMVC框
熟悉java高级特性,比如多线程编程、网络编程、反射、注解等; 熟练掌握javaweb技术,具有独立的开发能力,熟悉http协议,EL,JSTL表达式; 熟悉html、css、javascript、jQuery等脚本语言;并且能够熟练...
常见的 J2EE 中开发框架: MyBatis 框架: MyBatis 是一个优秀的基于 java 的持久层框架, 内部封装了 jdbc,开发者只需要关注 sql 语句 本身, 而不需要处理加载驱动、创建连接、创建 statement 、关闭连接, 资源...
大二下web编程技术和数据库的综合课程设计 包含源码、数据库文件、...后端框架:SSM(Spring、SpringMVC、Mybatis) 前端框架:jsp、JavaScript、JQuery、css、Bootstrap 使用MVC思想进行设计,其中C、M采用了DAO模式。
MyBatis消除了几乎所有的JDBC代码,并允许开发者通过XML或注解将对象与存储的记录关联起来。 前端技术:可能会使用HTML、CSS、JavaScript以及诸如Vue.js或AngularJS等现代JavaScript框架来构建用户界面。 数据库:...
2、本项目springMVC框架采用了注解映射器,使用了RESTful风格的url对系统发起http请求,开发更灵活。 3、同时使用了了hibernate提供的校验框架,对客户端数据进行校验! 4、Mybati数据库DAO层采用的是Mapper代理开发...
注意:SpringBoot(SpringMVC)里的处理程序特指@Controller注解的类里的每个处理HTTP请求的一个公共方法。 使用方法 步骤1:进入目录flylib-boot-starter,执行mvn install 第2步:在自己的项目中添加flylib-boot-
1.3、SpringBoot和SpringMVC区别 1 1.4、SpringBoot和SpringCloud区别 2 1.5常见错误 2 二、快速入门 2 2.1、创建一个Maven工程 2 2.2、pom文件引入依赖 3 2.3、编写HelloWorld服务 3 2.4、@RestController 4...