一:数据库
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"%>Insert title here
四: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列表 ListpersonList = 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; } }