开发者社区> 问答> 正文

jfinal调用save方法主键冲突的问题,是我使用方法不对??报错

@JFinal 你好,想跟你请教个问题:

直接看代码吧,为了方便我将业务方法『根据手机号注册』放在Customer类中

public class Customer extends Model<Customer> {
    public static final Customer dao = new Customer();

    public static final String ID = "id";
    public static final String USERNAME = "username";
    public static final String FULLNAME = "fullname";
    public static final String PASSWORD = "password";
    public static final String SALT = "salt";
    public static final String MOBILE = "mobile";
    public static final String EMAIL = "email";
    public static final String IS_ACTIVE = "is_active";
    public static final String CREATE_BY = "create_by";
    public static final String CREATE_DATE = "create_date";
    public static final String LAST_UPDATE_BY = "last_update_by";
    public static final String LAST_UPDATE_DATE = "last_update_date";
    public static final String IS_DELETE = "is_delete";

    //根据手机号注册
    public void registByMobile(String mobile, String password) {

        //。。。无关代码。。。

        new Customer().dao.
                set(Customer.USERNAME, username).
                set(Customer.MOBILE, mobile).
                set(Customer.PASSWORD, encodPwd).
                set(Customer.SALT, fakeSalt).
                set(Customer.IS_ACTIVE, "Y").
                set(Customer.CREATE_BY, 0).
                set(Customer.CREATE_DATE, DateUtil.getNow()).
                set(Customer.LAST_UPDATE_BY, 0).
                set(Customer.LAST_UPDATE_DATE, DateUtil.getNow()).
                set(Customer.IS_DELETE, "N").
                save();
    }
}



然后在controller的action中调用,如下:

//进行注册
        Customer.dao.registByMobile(mobile, password);



当我在系统中注册成功一个用户后,然后继续注册,则报主键冲突错误,请问是不能这样用嘛,报错如下:

com.jfinal.plugin.activerecord.ActiveRecordException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '11' for key 'PRIMARY'




展开
收起
爱吃鱼的程序员 2020-06-12 14:16:21 850 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

       dao对象是全局共享的,只能用于查询,不能承载数据,所以将以上有dao的代码删掉dao即可:newCustomer().dao.set(...)改成: newCustomer().set(...)。 Customer.dao.registByMobile(mobile,password);改成new  Customer().registByMobile(mobile,password); 

      jfinal手册上有红色字体明确说明过这个问题,在此下载手册: http://www.jfinal.com

    谢谢啊大大你又快又棒拿到首期款就给你赞助

    model这个写法真不好。

    先说写起来的工作量,model.set(XModel.FieldName,value);

    谁知道会重复多少遍呢,如果换成model.setFieldName这样子,至少IDE会为你省事。

    谁知道这个字段的具体数据类型呢?Model.set(Stringkey,Objectvalue),你又说要去看数据库文档或者model里面的注释?系统稍微复杂的时候,也不方便。

    我总结modelget/set写法有两种:

    一个是在model中声明属性,比如:

    publicclassUserextendsModel<User>{publicstaticfinalUserdao=newUser();////////////////////////////attributes////////////////////////////privateintid;//primarykeyprivateStringname;//用户名privateStringtelphone;//手机//如下代码使用IDE快速产生,如果表字段不一致,仅仅修改get或set的key名称!/////////////////////////getterandsetter/////////////////////////publicintgetId(){returnget("Id");}publicUsersetId(intid){set("Id",id);}//....还有更多}



    然而在IDEA中,还有个LiveTemplate技术,自己配置一个专门写modelget/set的模板,基本上可以做到秒杀的速度完成get/set的编写,有兴趣的同学自己去研究(相比上面的所有做法都要快上好几倍 )。

    不错不错谢啦回复 @JFinal:啥时候上线啊?哈哈,不错不错!其实我去年也试过用反射来实现,但感觉不爽。jfinal2.1已经完美解决这个问题,Model在保持了现有的便利性以外,同时还将拥有传统的javabean的好处,例如,拥有gettersetter方法,并且这些方法可以与数据表字段随时进行同步,无需任何人为工作
    2020-06-12 14:16:40
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多