加了默认的适配器@AdaptBy(type=PairAdaptor.class) 还是null;
package com.celesea.nutzbook.module;
import com.celesea.nutzbook.bean.User;
import org.apache.ibatis.annotations.Param;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.adaptor.PairAdaptor;
import org.nutz.mvc.annotation.*;
import javax.servlet.http.HttpSession;
import java.util.Date;
/**
* Created by hanan on 2017-3-10.
*/
@IocBean
@At("/user")
@Ok("json")
@Fail("http:500")
public class UserModule {
@Inject
protected Dao dao;
@At
public int count(){
return dao.count(User.class);
}
@At
public Object add(@Param("name") String name,@Param("password") String password){
User user = new User();
user.setName(name);
user.setPassword(password);
// System.out.println(user.getName());
NutMap re = new NutMap();
String msg =checkUser(user,true);
if(msg!= null){
return re.setv("ok",false).setv("msg",msg);
}
user.setCreateTime(new Date());
user.setUpdateTime(new Date());
dao.insert(user);
return re.setv("ok",true).setv("data",user);
}
@At
@AdaptBy(type=PairAdaptor.class)
public Object update(@Param("..")User user) {
System.out.println(user);
NutMap re = new NutMap();
String msg = checkUser(user, false);
if (msg != null){
return re.setv("ok", false).setv("msg", msg);
}
user.setName(null);// 不允许更新用户名
user.setCreateTime(null);//也不允许更新创建时间
user.setUpdateTime(new Date());// 设置正确的更新时间
dao.updateIgnoreNull(user);// 真正更新的其实只有password和salt
return re.setv("ok", true);
}
@At
public Object delete(@Param("id") int id, @Attr("me") int me){
NutMap re = new NutMap();
if(id == me){
return re.setv("ok",false).setv("msg","不能删除当前用户");
}
dao.delete(User.class,id);
return re.setv("ok",true);
}
@At
public Object query(@Param("name") String name, Pager pager){
Cnd cnd = Strings.isBlank(name)? null : Cnd.where("name", "like", "%"+name+"%");
QueryResult qr = new QueryResult();
qr.setList(dao.query(User.class,cnd,pager));
qr.setPager(pager);
return qr;
}
@At
public Object login(@Param("username") String username, @Param("password") String password, HttpSession session){
User user = dao.fetch(User.class, Cnd.where("name","=",username).and("passwd","=",password));
if(user == null){
return false;
}else{
session.setAttribute("me",user.getId());
return true;
}
}
@At
@Ok(">>:/")
public void logout(HttpSession session){
session.invalidate();
}
public String checkUser(User user,boolean create){
if(user == null){
return "空对象";
}
if(create){
if(Strings.isBlank(user.getName()) || Strings.isBlank(user.getPassword())){
return "用户名/密码不能为空";
}
}else{
if(Strings.isBlank(user.getPassword())){
return "密码不能为空 ";
}
}
String passwd = user.getPassword().trim();
if(passwd.length()<6 || passwd.length()>12){
return "密码长度错误";
}
user.setPassword(passwd);
if(create){
int count = dao.count(User.class,Cnd.where("name","=",user.getName()));
if(count != 0){
return "用户名已存在";
}
}else{
if(user.getId()<1){
return "用户ID错误";
}
}
if(user.getName()!= null){
user.setName(user.getName().trim());
}
return null;
}
}
2017-3-10 17:21:13.395 DEBUG [http-bio-8080-exec-9] Found mapping for [GET] path=/user/update : UserModule.update(UserModule.java:55)
2017-3-10 17:21:13.395 DEBUG [http-bio-8080-exec-9] Get 'userModule'<class com.celesea.nutzbook.module.UserModule>
null