NutzCN Logo
问答 at org.nutz.service.EntityService.count
发布于 3019天前 作者 Hamming 1826 次浏览 复制 上一个帖子 下一个帖子
标签:

查询用户是否存在

        user.setPassword(passwd);
        if (create) {
            int count = userService.count(Cnd.where("user_name", "=", user.getUser_name()));
            if (count != 0) {
                return "用户名已经存在";
            }
        } else {
            if (user.getUser_id() < 1) {
                return "用户Id非法";
            }
        }

异常信息

DEBUG org.nutz.service.EntityService - Get TypeParams for self : nuoshang.bluejay.bean.User
2016-08-17 10:32:43,660 [qtp22805895-23] WARN  org.nutz.mvc.impl.processor.FailProcessor - Error@/user/save :
java.lang.NullPointerException
	at org.nutz.service.EntityService.count(EntityService.java:165)
	at nuoshang.bluejay.modules.UserModule.checkUser(UserModule.java:150)
	at nuoshang.bluejay.modules.UserModule.save(UserModule.java:87)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:25)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)

user 类

  @Id
    @Column("user_id")
    private int user_id;
    /****用户名 ****/
    @Name
    @Column("user_name")
    private String user_name;

    /****用户密码 ****/

    @Column("password")
    @ColDefine(width = 128)
    private String password;

调用的方法

    /**
     * 根据条件统计符合条件的记录数
     * 
     * @param cnd
     *            查询条件
     * @return 记录数
     */
    public int count(Condition cnd) {
        return dao().count(getEntityClass(), cnd);
    }

5 回复

贴UserService,完整。

来自炫酷的 NutzCN

@wendal

package nuoshang.bluejay.services;

import nuoshang.bluejay.base.AppConst;
import nuoshang.bluejay.bean.User;
import nuoshang.bluejay.exception.UnAuthorizedException;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.lang.random.R;
import org.nutz.service.IdNameEntityService;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;



/**
 * Created by Administrator on 2016/8/11.
 */
@IocBean
public class UserService extends IdNameEntityService<User>  {
    /**
     * login
     *
     * @param name name
     * @param password password
     * @return id of user. return -1 if invalid.
     */
    public User login(String name, String password) throws UnAuthorizedException {
        User user = fetch(name);
        if (user == null) {
            throw new UnAuthorizedException("用户名或密码不正确");
        }

        String _pass = new Sha256Hash(password, user.getSalt()).toHex();
        if(!_pass.equalsIgnoreCase(user.getPassword())) {
            throw new UnAuthorizedException("用户名或密码不正确");
        }
        return user;
    }

    public void updatePassword(long userId, String password) {
        User user = fetch(userId);
        if (user == null) {
            return;
        }
        user.setSalt(R.UU16());
        user.setPassword(new Sha256Hash(password, user.getSalt()).toHex());
        user.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        dao().update(user, "^(password|salt|updateTime)$");
//        dao.updateIgnoreNull(user);
    }

    public User save(String name, String password) {
        User user = new User();
        user.setUser_name(name.trim());
        user.setSalt(R.UU16());
        user.setPassword(new Sha256Hash(password, user.getSalt()).toHex());
        user.setCreate_time(new Timestamp(System.currentTimeMillis()));
        user.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        user.setState(User.STATE_OK);
        return dao().insert(user);
    }

    public void rm(long userId) {
        User user = fetch(userId);
        if (user == null) {
            return;
        }
        user.setState(User.STATE_RM);
        user.setUpdate_time(new Timestamp(System.currentTimeMillis()));
        dao().update(user);
    }

    public QueryResult list(String name, Pager pager) {
        Cnd cnd = Strings.isBlank(name)? null : Cnd.where("name", "like", "%"+name+"%");
        List<User> list = dao().query(User.class, cnd, pager);
        int count = dao().count(User.class, cnd);

        QueryResult qr = new QueryResult();
        qr.setList(list);
        pager.setRecordCount(count);
        pager.setPageSize(AppConst.PAGE_SIZE);
        qr.setPager(pager);

        return qr;
    }
}

@IocBean(args = {"refer:dao"})
public class UserService extends IdNameEntityService {
public UserService(Dao dao) {
super(dao);
}

@IocBean(args = {"refer:dao"})
public class UserService extends IdNameEntityService<User> {
 public UserService(Dao dao) {
 super(dao);
 }

@tonyyule 改成

@IocBean(fields="dao")

来自炫酷的 NutzCN

添加回复
请先登陆
回到顶部