3、以User为例进行CRU操作
Id.java //Model的父类,为了该死的JSTL我得加上get/set
User.java //用户信息
DoUser.java //CRU操作,同时处理HTTP请求
页面代码就不贴了,全文完
Id.java //Model的父类,为了该死的JSTL我得加上get/set
public abstract class Id<T extends Id<T>> {
@EncDec
public long id;
public long id() {
return id;
}
@SuppressWarnings("unchecked")
public T id(long id_) {
id = id_;
return (T)this;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
User.java //用户信息
public class User extends Id<User> {
@EncDec
public String name;
@EncDec
public int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
DoUser.java //CRU操作,同时处理HTTP请求
public class DoUser extends Do {
@Service
@Transaction.Any //只是跳转用,没有数据操作
public String create_() throws Exception {
return "/create";
}
@Service
public String create(String name, int age) throws Exception {
User u = new User();
u.name = name;
u.age = age;
data.put(u);
return "redirect:/";
}
@Service
@Transaction.Any
public String update_(long id, ModelMap map) throws Exception {
User u = data.get(User.class, id);
map.put("user", u);
return "/update";
}
@Service
public String update(long id, String name, int age) throws Exception {
User u = new User().id(id);
u.name = name;
u.age = age;
data.put(u);
return "redirect:/";
}
@Service
public String get(long id, ModelMap map) throws Exception {
User u = data.get(User.class, id);
map.put("user", u);
return "/index";
}
}
页面代码就不贴了,全文完