NutzCN Logo
问答 nutZ启用双数据源导致DaoSupport的诸多属性为空
发布于 1669天前 作者 qq_08e851a2 1129 次浏览 复制 上一个帖子 下一个帖子
标签:

第一个连接

@Configuration
public class DruidConfiguration {

    @Primary
    @Bean(name = "druidDataSource")
    public DataSource druidDataSource(@Value("${druid1.spring.datasource.driverClassName}") String driver, @Value("${druid1.spring.datasource.url}") String url, @Value("${druid1.spring.datasource.username}") String username, @Value("${druid1.spring.datasource.password}") String password) {

        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        //        druidDataSource.setDefaultAutoCommit(false);
        //        try {
        //           druidDataSource.setFilters("stat, wall");
        //        } catch (SQLException e) {
        //            e.printStackTrace();
        //        }
        return druidDataSource;
    }
}

第二个连接

@Configuration
public class SecondDruidConfiguration {
	
    @Bean(name = "secondDruidDataSource")
    public DataSource druidDataSource(@Value("${druid2.spring.datasource.driverClassName}") String driver, @Value("${druid2.spring.datasource.url}") String url, @Value("${druid2.spring.datasource.username}") String username, @Value("${druid2.spring.datasource.password}") String password) {

        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        //        druidDataSource.setDefaultAutoCommit(false);
        //        try {
        //           druidDataSource.setFilters("stat, wall");
        //        } catch (SQLException e) {
        //            e.printStackTrace();
        //        }
        return druidDataSource;
    }

}

执行查询的代码

	/**
	 * 	(entity有扩展字段的情况)自定义sql分页查询 
	 * @param sqlStr sql主体
	 * @param tableMap 表名
	 * @param parameterMap 参数
	 * @param pager 分页对象
	 * @return 返回map
	 */
    public List<Map> customSqlQueryMapPager(String sqlStr,Map<String,Object> parameterMap, Pager pager) {
        Sql sql = Sqls.create(sqlStr); // 创建一个自定义Sql对象, 还有各种形式哦 
        if(null!=pager) {
        	pager.setRecordCount(getCount(sqlStr, parameterMap));
        	sql.setPager(pager);//设置分页 提醒一下, 用于分页的sql对象,不可重复使用!!
        }
        this.setParams(sql, parameterMap);
        sql.setCallback(Sqls.callback.maps());//返回map 会根据key的名称自动对应pojo里的属性 数据库字段的‘_’以驼峰命名自动对应
        log.info("sql : "+sql);
        secondDao.execute(sql); // 注意, execute是没有返回值的, 所需要的值都通过callback来获取
                          // 不抛出异常就是执行成功
        List<Map> datas = sql.getList(Map.class);
        return datas;
    }
java.lang.NullPointerException
	at org.nutz.dao.impl.DaoSupport.execute(DaoSupport.java:235)
	at org.nutz.dao.impl.NutDao.execute(NutDao.java:973)
	at com.xhj.xpcollege.dao.impl2.SecondDaoImpl.customSqlQueryMapPager(SecondDaoImpl.java:430)
	at com.xhj.xpcollege.dao.impl2.SecondDaoImpl.customSqlQueryMapPager(SecondDaoImpl.java:486)
	at com.xhj.xpcollege.dao.impl2.UserProfileDaoImpl.select(UserProfileDaoImpl.java:68)
	at com.xhj.xpcollege.dao.impl2.UserProfileDaoImpl$$FastClassBySpringCGLIB$$4c75c35c.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747)
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689)
	at com.xhj.xpcollege.dao.impl2.UserProfileDaoImpl$$EnhancerBySpringCGLIB$$481af74c.select(<generated>)
	at com.xhj.xpcollege.service.impl.UserProfileServiceImpl.select(UserProfileServiceImpl.java:39)
	at com.xhj.xpcollege_service.AppTest.userProfileSelect(AppTest.java:32)
	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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
	at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
	at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
	at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
	at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
	at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
	at org.junit.vintage.engine.execution.RunnerExecutor.execute(RunnerExecutor.java:40)
	at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
	at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
	at java.util.Iterator.forEachRemaining(Iterator.java:116)
	at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
	at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
	at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
	at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
	at org.junit.vintage.engine.VintageTestEngine.executeAllChildren(VintageTestEngine.java:80)
	at org.junit.vintage.engine.VintageTestEngine.execute(VintageTestEngine.java:71)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:229)
	at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:197)
	at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:211)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:191)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:137)
	at org.eclipse.jdt.internal.junit5.runner.JUnit5TestReference.run(JUnit5TestReference.java:89)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)


DaoSupport.java:235行代码

233    public void execute(final Sql... sqls) {
234        for (Sql sql : sqls)
235            expert.formatQuery(sql);
236        _exec(sqls);
237    }
5 回复

不知道啥原因

已解决。谢谢

secondDao的声明有问题

@wendal 是的,已经解决,非常感谢。但是有个新的问题就是,连接双数据源的时候要怎么用事务呢?如果我不加@Primary启事务就报错,加了就不能在一个方法里面跨数据源。

你需要seata之类的分布式事务解决方案吧

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