Nutz4---一對一的增刪改查

nutz太過輕量,所有關聯的對象,都不會一個方法就load出來,需要兩個步驟,下面是實例。

一:數據庫
1,創建person表
-- Create table
create table T_PERSON
(
ID NUMBER not null,
NAME NVARCHAR2(60),
PASSWORD NVARCHAR2(60),
ADDRESSID NUMBER
)
tablespace SYSTEM
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table T_PERSON
add constraint PK_PERSON primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);

2,創建address表
-- Create table
create table T_ADDRESS
(
ID NUMBER not null,
NAME NVARCHAR2(60),
MEMO NVARCHAR2(60)
)
tablespace SYSTEM
pctfree 10
pctused 40
initrans 1
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);
-- Create/Recreate primary, unique and foreign key constraints
alter table T_ADDRESS
add constraint PK_ADDRESS primary key (ID)
using index
tablespace USERS
pctfree 10
initrans 2
maxtrans 255
storage
(
initial 64K
minextents 1
maxextents unlimited
);

3,創建序列
--創建序列(實現person,address主鍵自增的必要序列)
create sequence seq_nutz
increment by 1
start with 1
maxvalue 999999999;

4,創建觸發器
--給person表創建觸發器--實現主鍵自增
CREATE TRIGGER trigger_pk_person BEFORE
insert ON T_PERSON FOR EACH ROW
begin
select seq_nutz.nextval into:New.id from dual;
end;

--給person表創建觸發器--實現主鍵自增
CREATE TRIGGER trigger_pk_address BEFORE
insert ON T_ADDRESS FOR EACH ROW
begin
select seq_nutz.nextval into:New.id from dual;
end;

2,數據庫實體模型
Person實體
package com.supan.nuts.module;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.One;
import org.nutz.dao.entity.annotation.Table;

@Table("T_PERSON")
public class Person {

@Id
private Integer id;

@Column("name")
private String name;

@Column("password")
private String password;

@Column("addressId")
private Integer addressId;

@One(target = Address.class,field = "addressId")
private Address address;
//省略getter setter...
}

Address實體
package com.supan.nuts.module;

import org.nutz.dao.entity.annotation.Column;
import org.nutz.dao.entity.annotation.Id;
import org.nutz.dao.entity.annotation.Table;

@Table("T_ADDRESS")
public class Address {

@Id
private Integer id;

@Column("name")
private String name;

@Column("memo")
private String memo;
//省略getter,setter
}
三:頁面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript">

//添加person
//在頁面上構造後臺需要的複雜的Person對象。
function addPerson(){
var person = {
id:"1",
name:"chenchaoyang",
password:"woaini",
address:{
id:1,
name:"廣東省深圳市龍崗區",
memo:"有點遠"
}
}
$.ajax({
type : 'POST',
url : 'insertPerson.nut',
data : JSON.stringify(person),
success : function(){
alert("chenchaoyang");
}
});
}

//獲取Person對象
function getPerson(personId){
$.ajax({
type : 'POST',
url : 'getPerson.nut?personId=26',
success : function(){
alert("chenchaoyang");
}
});
}

//更新Person對象
function updatePerson(personId){
$.ajax({
type : 'POST',
url : 'updatePerson.nut?personId=26',
success : function(){
alert("chenchaoyang");
}
});
}

//更新Person對象
function queryPerson(personId){
$.ajax({
type : 'POST',
url : 'queryPerson.nut',
success : function(){
alert("chenchaoyang");
}
});
}

</script>
</head>
<body>
<input type="button" value="添加Person" onclick="addPerson();">
<input type="button" value="獲取Person" onclick="getPerson();">
<input type="button" value="更新Person" onclick="updatePerson();">
<input type="button" value="查詢Person列表" onclick="queryPerson();">
</body>
</html>

四:Action
package com.supan.nuts.action;
import java.util.List;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.adaptor.JsonAdaptor;
import org.nutz.mvc.adaptor.PairAdaptor;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import com.supan.nuts.module.Person;

@IocBean
public class PersonAction {

@Inject
private NutDao dao;

@At("/insertPerson")
@Ok("json")
@AdaptBy(type = JsonAdaptor.class)
public String insertPerson(@Param("..") Person person) {
// 注意下面一句話可以同時插入Person,Address表中各一條記錄
dao.insertWith(person, "address");
return "success";
}

@At("/getPerson")
@Ok("json")
@AdaptBy(type = PairAdaptor.class)
public String getPerson(@Param("personId") Integer personId) {
// 僅僅獲取Person對象,不包括含有的Address關聯對象
Person person = dao.fetch(Person.class, personId);
// 獲取Person對象,包括關聯對象address
Person person2 = dao.fetchLinks(person, "address");
// 注意nutz並沒有提供一個方法能一次獲取Person及其關聯對象address
// 如果你想一句話獲取Person及其關聯對象address可以組合上面的兩句
Person person3 = dao.fetchLinks(dao.fetch(Person.class, personId),
"address");
return "success";
}

@At("/updatePerson")
@Ok("json")
@AdaptBy(type = PairAdaptor.class)
public String updatePerson(@Param("personId") Integer personId) {
// 先根據PersonId獲取Person對象(注意包括其關聯的address對象)
Person person = dao.fetchLinks(dao.fetch(Person.class, personId),
"address");
person.setName("何勝男");
person.setPassword("heshegnnan");
// 更新Person關聯的Address對象
person.getAddress().setName("河南省");
person.getAddress().setMemo("是個貧窮的地方");
// 同時更新Person及其關聯的Address對象
dao.updateWith(person, "address");
return "success";
}

/**
* 查詢Person列表,注意nutz並不會關聯查詢其關聯的address對象 2015年5月9日
*/
@At("/queryPerson")
@Ok("json")
@AdaptBy(type = PairAdaptor.class)
public String queryPerson() {
Pager pager = dao.createPager(1, 100000);
// 僅僅查詢Person列表
List<Person> personList = dao.query(Person.class, null, pager);
// 遍歷Person列表,爲每個person查詢其關聯的address
for (Person person : personList) {
dao.fetchLinks(person, "address");
}
return "success";
}

public NutDao getDao() {
return dao;
}

public void setDao(NutDao dao) {
this.dao = dao;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章