全注解SSH

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

全注解SSH一,hibernateannotationClass注解:1.@Entity:表明当前类是一个持久化类2.@Table(name=team,catalog=NBA):映射一个表team,所对应的数据库是NBA,可以省略字段属性注解:1.@GenericGenerator(name=generator,strategy=increment)@Id@GeneratedValue(generator=generator)@Column(name=id,unique=true,nullable=false)解释:表明该字段是主键,自增长,不为空而且是唯一的2.@Column(name=description,length=500)解释:映射表中的description字段,长度是5003.@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy=category)解释:级联操作:cascade=CascadeType.ALL,延迟加载:fetch=FetchType.LAZY,映射:mappedBy=category,一对多方式4.@ManyToOne(fetch=FetchType.LAZY)@JoinColumn(name=category_id)解释:延迟加载:多对一方式,关联信息:外键name=category_idOneToMany事例代码:数据库:mysqlcategory表:id,name,description(Pkid)product表:id,name,price,description,category_id(pkid,fkcategory_id)Category.javapackagecom.b510.examples;importjava.util.HashSet;importjava.util.Set;//标准注解importjavax.persistence.CascadeType;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.FetchType;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;importjavax.persistence.OneToMany;importjavax.persistence.Table;//增加的注解importorg.hibernate.annotations.GenericGenerator;//当前的类是一个持久化类,是Category这个类。他映射了一个表category。所对应的数据库是users//这句:@Table(name=category,catalog=users)可以省略@Entity@Table(name=category,catalog=users)publicclassCategoryimplementsjava.io.Serializable{privatestaticfinallongserialVersionUID=3240281547213597385L;privateIntegerid;privateStringname;privateStringdescription;privateSetProductproducts=newHashSetProduct(0);publicCategory(){}publicCategory(Stringname,Stringdescription,SetProductproducts){this.name=name;this.description=description;this.products=products;}//主键:@Id主键生成方式:strategy=increment//映射表中id这个字段,不能为空,并且是唯一的@GenericGenerator(name=generator,strategy=increment)@Id@GeneratedValue(generator=generator)@Column(name=id,unique=true,nullable=false)publicIntegergetId(){returnthis.id;}publicvoidsetId(Integerid){this.id=id;}//映射表中name这个字段,长度是500@Column(name=name,length=500)publicStringgetName(){returnthis.name;}publicvoidsetName(Stringname){this.name=name;}//映射表中description这个字段,长度是500@Column(name=description,length=500)publicStringgetDescription(){returnthis.description;}publicvoidsetDescription(Stringdescription){this.description=description;}//级联操作:cascade=CascadeType.ALL//延迟加载:fetch=FetchType.LAZY//映射:mappedBy=category//一对多方式@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy=category)publicSetProductgetProducts(){returnthis.products;}publicvoidsetProducts(SetProductproducts){this.products=products;}}Product.java代码:packagecom.b510.examples;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.FetchType;importjavax.persistence.GeneratedValue;importjavax.persistence.Id;importjavax.persistence.JoinColumn;importjavax.persistence.ManyToOne;importjavax.persistence.Table;importorg.hibernate.annotations.GenericGenerator;@Entity@Table(name=product,catalog=users)publicclassProductimplementsjava.io.Serializable{privatestaticfinallongserialVersionUID=-1546206493725028472L;privateIntegerid;privateCategorycategory;privateStringname;privateStringprice;privateStringdescripton;publicProduct(){}publicProduct(Categorycategory,Stringname,Stringprice,Stringdescripton){this.category=category;this.name=name;this.price=price;this.descripton=descripton;}@GenericGenerator(name=generator,strategy=increment)@Id@GeneratedValue(generator=generator)@Column(name=id,unique=true,nullable=false)publicIntegergetId(){returnthis.id;}publicvoidsetId(Integerid){this.id=id;}//延迟加载:多对一方式//关联信息:外键name=category_id@ManyToOne(fetch=FetchType.LAZY)@JoinColumn(name=category_id)publicCategorygetCategory(){returnthis.category;}publicvoidsetCategory(Categorycategory){this.category=category;}@Column(name=name,length=500)publicStringgetName(){returnthis.name;}publicvoidsetName(Stringname){this.name=name;}@Column(name=price,length=10)publicStringgetPrice(){returnthis.price;}publicvoidsetPrice(Stringprice){this.price=price;}@Column(name=descripton,length=500)publicStringgetDescripton(){returnthis.descripton;}publicvoidsetDescripton(Stringdescripton){this.descripton=descripton;}}ManyToMany事例代码:你可以通过@ManyToMany注解可定义的多对多关联.同时,你也需要通过注解@JoinTable描述关联表和关联条件.如果是双向关联,其中一段必须定义为owner,另一端必须定义为inverse(在对关联表进行更新操作时这一端将被忽略):@EntitypublicclassEmployerimplementsSerializable{@ManyToMany(targetEntity=org.hibernate.test.metadata.manytomany.Employee.class,cascade={CascadeType.PERSIST,CascadeType.MERGE})@JoinTable(name=EMPLOYER_EMPLOYEE,joinColumns=@JoinColumn(name=EMPER_ID),inverseJoinColumns=@JoinColumn(name=EMPEE_ID))publicCollectiongetEmployees(){returnemployees;}...}@EntitypublicclassEmployeeimplementsSerializable{@ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},mappedBy=employees,targetEntity=Employer.class)publicCollectiongetEmployers(){returnemployers;}}二,Spring注释@Component注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service和@Controller。在目前的Spring版本中,这3个注释和@Component是等效的,但是从注释类的命名上,很容易看出这3个注释分别和持久层、业务层和控制层(Web层)相对应。虽然目前这3个注释和@Component相比没有什么新意,但Spri

1 / 9
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功