NutzCN Logo
问答 如何默认生成字段值问题
发布于 2708天前 作者 bb3239 2066 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

我想在插入数据库时,把主键字段采用插入默认值,比如TT20170724001,再插入时是TT20170724002,该怎么写啊?

34 回复

这不叫 默认值 ,这是自定义主键的生成策略

http://nutzam.com/core/dao/primary_key.html

@wendal 看了,关键是后面的001、002等如何实现自增

要么查出最大值然后加一,要么专门找个地方存最大值,例如redis

@wendal 如果采用TT+日期+时分秒的形式,不要后面的001等,直接采用@SQL注解就可以了,但是我又不能保证,插入的字段值绝对唯一

要么本地存储然后加锁自增,要么redis的incr自增

@wendal 参照你的逐渐生成策略,插入时提示 org.nutz.el.ELException:no such key =ig

[DEBUG] 2017-07-24 11:29:14,225 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - // NOT SQL // ElFieldMacro=ig(view.tableName)
[DEBUG] 2017-07-24 11:29:14,225 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:110) - Exception when parser SQL sql, skip cache detect!! SQL=RUN 

没注册到EL

@wendal 在setup的init方法中注册了

public void init(NutConfig config) {
        try {
            
            // 环境检查
            if (!Charset.defaultCharset().name().equalsIgnoreCase(Encoding.UTF8)) {
                log.warn("This project must run in UTF-8, pls add -Dfile.encoding=UTF-8 to JAVA_OPTS");
            }
            Ioc ioc = config.getIoc();

            CustomMake.me().register("ig", ioc.get(keyGeneratorService.class));

是不是在注册之前就执行了相关的dao操作??

@wendal 肯定不是,我是在界面中点击按钮后执行的dao操作,一定是在注册之后执行的

贴完整报错信息

    |          1 |         2 |   3 |                                                                 4 |               5 |                6 |                                7 |          8 |    9 |
    |------------|-----------|-----|-------------------------------------------------------------------|-----------------|------------------|----------------------------------|------------|------|
    | NormalUser | aop.after | Add | cn.wizzer.app.tunnel.modules.controllers.OpeApplyController#addDo | 0:0:0:0:0:0:0:1 | Add:Ope_approval | 58ceb84167c54008864ef0b86b0a2800 | 1500866895 | NULL |
  For example:> "INSERT INTO sys_log(username,type,tag,src,ip,msg,opBy,opAt,delFlag) VALUES('NormalUser','aop.after','Add','cn.wizzer.app.tunnel.modules.controllers.OpeApplyController#addDo','0:0:0:0:0:0:0:1','Add:Ope_approval','58ceb84167c54008864ef0b86b0a2800',1500866895,'NULL') "
[DEBUG] 2017-07-24 11:28:15,022 cn.wizzer.app.web.commons.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) - [POST]URI=/platform/operate/apply/addDo 46624ms
[DEBUG] 2017-07-24 11:28:57,601 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) - Found mapping for [POST] path=/platform/operate/apply/addDo : OpeApplyController.addDo(OpeApplyController.java:50)
[DEBUG] 2017-07-24 11:28:57,601 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'shiroUtil'<class cn.wizzer.framework.util.ShiroUtil>
[DEBUG] 2017-07-24 11:28:57,601 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'dateUtil'<class cn.wizzer.framework.util.DateUtil>
[DEBUG] 2017-07-24 11:28:57,601 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'stringUtil'<class cn.wizzer.framework.util.StringUtil>
[DEBUG] 2017-07-24 11:28:57,602 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'opeApplyController'<class cn.wizzer.app.tunnel.modules.controllers.OpeApplyController>
[DEBUG] 2017-07-24 11:29:14,225 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - // NOT SQL // ElFieldMacro=ig(view.tableName)
[DEBUG] 2017-07-24 11:29:14,225 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:110) - Exception when parser SQL sql, skip cache detect!! SQL=RUN 

执行inset时进了catch语句,提示 org.nutz.el.ELException:no such key =ig

目测register没成功,抛异常

对,我跟踪到是注册没成功,我引用的是这个: import static org.nutz.integration.jedis.RedisInterceptor.jedis;没问题吧?

Caused by: org.nutz.ioc.ObjectLoadException: Object 'jedis' without define!
	at org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:166)
	at org.nutz.ioc.impl.NutIoc.get(NutIoc.java:180)
	... 63 more

iocby加上 *jedis 才能启用jedis相关的bean

本身就加了*jedis,我用的是nutzwk4.x

贴完整报错信息

24-Jul-2017 13:05:28.071 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory G:\Web\apache-tomcat-8.0.43-windows-x64\apache-tomcat-8.0.43\webapps\manager
24-Jul-2017 13:05:28.197 信息 [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory G:\Web\apache-tomcat-8.0.43-windows-x64\apache-tomcat-8.0.43\webapps\manager has finished in 126 ms
[DEBUG] 2017-07-24 13:05:28,369 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'keyGeneratorService'<class cn.wizzer.app.tunnel.modules.services.keyGeneratorService>
[DEBUG] 2017-07-24 13:05:28,370 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) - 	 >> Load definition name=keyGeneratorService
[DEBUG] 2017-07-24 13:05:28,370 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) - Found IocObject(keyGeneratorService) in AnnotationIocLoader(packages=[cn.wizzer])
[DEBUG] 2017-07-24 13:05:28,371 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) - 	 >> Make...'keyGeneratorService'<class cn.wizzer.app.tunnel.modules.services.keyGeneratorService>
[DEBUG] 2017-07-24 13:05:28,371 cn.wizzer.app.web.commons.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) - skip -- cn.wizzer.app.tunnel.modules.services.keyGeneratorService
[DEBUG] 2017-07-24 13:05:28,372 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'jedis'<interface org.nutz.aop.MethodInterceptor>
[DEBUG] 2017-07-24 13:05:28,373 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) - 	 >> Load definition name=jedis
org.nutz.ioc.IocException: IocBean[keyGeneratorService -> jedis] For object [jedis] - type:[interface org.nutz.aop.MethodInterceptor][INFO ] 2017-07-24 13:05:36,259 org.nutz.mvc.impl.NutLoading.load(NutLoading.java:141) - Nutz.Mvc[nutz] is up in 15621ms
[INFO ] 2017-07-24 13:05:36,260 org.nutz.mvc.NutFilter._init(NutFilter.java:117) - exclusionsPrefix  = ^(/assets/|/druid/|/upload/|/apidoc/)
[2017-07-24 01:05:36,271] Artifact utilitytunnel: Artifact is deployed successfully
[2017-07-24 01:05:36,272] Artifact utilitytunnel: Deploy took 19,253 milliseconds
[DEBUG] 2017-07-24 13:05:36,957 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:110) - Search mapping for [GET] path=/sysadmin : NOT Action match

贴keyGeneratorService的代码看看

@IocBean
public class keyGeneratorService implements RunMethod {

    @Aop("jedis")
    public Object run(List<Object> fetchParam) {
        // 用redis实现自增
        
        String para=fetchParam.get(0).toString();
        return jedis().incr("ig:"+para);
    }
    

    public String fetchSelf() {
        return "ig";
    }
}

看来文档写错了,应该是

@Aop("redis")

@wendal 改过来后可以注册,但插入时提示


[DEBUG] 2017-07-24 13:25:05,835 org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3877) - ClusterManager: Check-in complete. [DEBUG] 2017-07-24 13:25:05,836 org.nutz.dao.impl.sql.run.NutDaoExecutor.printSQL(NutDaoExecutor.java:388) - // NOT SQL // ElFieldMacro=ig(view.tableName) [DEBUG] 2017-07-24 13:25:05,837 org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:110) - Exception when parser SQL sql, skip cache detect!! SQL=RUN [DEBUG] 2017-07-24 13:25:05,843 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'jedisPool'<class redis.clients.util.Pool> [DEBUG] 2017-07-24 13:25:05,844 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) - >> Load definition name=jedisPool [DEBUG] 2017-07-24 13:25:05,844 org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67) - Loading define for name=jedisPool [DEBUG] 2017-07-24 13:25:05,849 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) - Found IocObject(jedisPool) in JedisIocLoader@1836196869 [DEBUG] 2017-07-24 13:25:05,850 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) - >> Make...'jedisPool'<class redis.clients.util.Pool> [DEBUG] 2017-07-24 13:25:05,850 cn.wizzer.app.web.commons.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) - skip -- redis.clients.jedis.JedisPool [DEBUG] 2017-07-24 13:25:05,860 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) - Load class redis.clients.jedis.JedisPool without AOP [DEBUG] 2017-07-24 13:25:05,861 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) - Save object 'jedisPool' to [app] [DEBUG] 2017-07-24 13:25:05,902 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'jedisPoolConfig'<> [DEBUG] 2017-07-24 13:25:05,902 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:177) - >> Load definition name=jedisPoolConfig [DEBUG] 2017-07-24 13:25:05,903 org.nutz.ioc.loader.map.MapLoader.load(MapLoader.java:67) - Loading define for name=jedisPoolConfig [DEBUG] 2017-07-24 13:25:05,907 org.nutz.ioc.loader.combo.ComboIocLoader.load(ComboIocLoader.java:161) - Found IocObject(jedisPoolConfig) in JedisIocLoader@1836196869 [DEBUG] 2017-07-24 13:25:05,908 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:209) - >> Make...'jedisPoolConfig'<> [DEBUG] 2017-07-24 13:25:05,909 cn.wizzer.app.web.commons.plugin.PluginAopConfigure.getInterceptorPairList(PluginAopConfigure.java:33) - skip -- redis.clients.jedis.JedisPoolConfig [DEBUG] 2017-07-24 13:25:05,910 org.nutz.ioc.aop.impl.DefaultMirrorFactory.getMirror(DefaultMirrorFactory.java:70) - Load class redis.clients.jedis.JedisPoolConfig without AOP [DEBUG] 2017-07-24 13:25:05,911 org.nutz.ioc.impl.ScopeContext.save(ScopeContext.java:64) - Save object 'jedisPoolConfig' to [app] [DEBUG] 2017-07-24 13:25:06,167 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<> [DEBUG] 2017-07-24 13:25:06,184 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<> [DEBUG] 2017-07-24 13:25:06,186 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<> [DEBUG] 2017-07-24 13:25:06,186 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<> [DEBUG] 2017-07-24 13:25:06,187 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<> [DEBUG] 2017-07-24 13:25:06,188 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<> [DEBUG] 2017-07-24 13:25:06,193 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'jedisPoolConfig'<> [DEBUG] 2017-07-24 13:25:06,193 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<> [DEBUG] 2017-07-24 13:25:06,194 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<> [DEBUG] 2017-07-24 13:25:06,195 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<> [DEBUG] 2017-07-24 13:25:06,196 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<> [DEBUG] 2017-07-24 13:25:06,197 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'conf'<> redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

这样并没有啥问题

不知道为什么会进catch

就是上面贴的最后一句,我把它输出去了

没装redis?没启动redis?

这样才是打印报错信息的正确写法


log.debug(e.getMessage(), e)

cn.wizzer.framework.ig.RedisIdGenerator

框架自带的,你按需要修改

cn.wizzer.app.web.commons.core.Setup

而且启动类里实现了 initRedisIg ,启动时把主键加载到缓存

并且框架字段的RedisIdGenerator,第二个传参就是前缀

@Column
@Name
@Comment("ID")
@ColDefine(type = ColType.VARCHAR, width = 32)
@Prev(els = {@EL("ig(view.tableName,'')")})
private String id;

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