1. 程式人生 > >SSH全註解搭建

SSH全註解搭建

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>