import java.io.Serializable;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="neo_district")
public class District implements Serializable{
//定义主键id
@Id
@GeneratedValue(generator="d_id",strategy=GenerationType.SEQUENCE)
@SequenceGenerator(name="d_id",sequenceName="did_seq")
private int id;
private String name;
//一对多关系(mappedBy=关联类的对应字段,放弃维护关系,让维护关系由对方维护)
@OneToMany(mappedBy="distric")
private Set<Street> streets;
@Entity
@Table(name="neo_street")
public class Street implements Serializable{
@Id
@GeneratedValue(generator="s_id",strategy=GenerationType.SEQUENCE)
@SequenceGenerator(name="s_id",sequenceName="sid_seq")
private Integer id;
private String name;
@ManyToOne
@JoinColumn(name="d_id")//定义外键名
private District distric;
@Transient
private String dname;
上面是hibernate实体类注解创建
spring对javabean对象的管理都和原来一样这里就省略了
struts2对应action类的注解声明如下
@Controller
@Scope("propotype")
@Namespace("/")
@ParentPackage(value="ajax")//parentPackage需要在struts.xml配置对应主要是需要在struts.xml中声明拦截器
这里主要是进行ajax请求处理所有继承了json-default
public class TypeAction extends ActionSupport{
@Autowired
private Typeservice ts;
private List<Typedto> litype;
public Typeservice getTs() {
return ts;
}
public void setTs(Typeservice ts) {
this.ts = ts;
}
public List<Typedto> getLitype() {
return litype;
}
public void setLitype(List<Typedto> litype) {
this.litype = litype;
}
//业务开始(这里action的注解方式和struts.xml中的声明方式类似)
/*struts2配置中指定了result的name=”root”,所谓root的含义,是指返回的json数据的根为root中指定的bean类
,例如此处为litype,如果没有指定root属性,则默认使用TypeAction作为返回的json数据的根*/
@Action(value="alltype",results={@Result(type="json",name="success",params={"root","litype"})})
public String findallType(){
litype=ts.findallType();
for(Typedto t:litype){
System.out.println(t.getId()+","+t.getName());
}
return SUCCESS;
}
<struts>
<package name="user" namespace="/" extends="struts-default">
<interceptors>
<interceptor name="login" class="com.neo.interceptor.LoginInterceptor"></interceptor>
</interceptors>
</package>
<package name="house" namespace="/" extends="struts-default">
</package>
<package name="ajax" namespace="/" extends="json-default">
</package>
</struts>