各位大神:
我按贴子中,有人说的经验配置了多个数据源,但是没有成功。我贴出来看看
配置单:
var ioc = {
dataSource : {
type : "com.mchange.v2.c3p0.ComboPooledDataSource",
fields : {
driverClass : 'com.mysql.jdbc.Driver',
jdbcUrl : 'jdbc:mysql://127.0.0.1:3306/sales?useUnicode=true&characterEncoding=UTF8',
user : 'root',
password : '123456',
//password : '123!@#abc',
minPoolSize : '20',
maxPoolSize : '40',
initialPoolSize : '20',
maxIdleTime : '25200'
}
},
dataSource1 : {
type : "com.mchange.v2.c3p0.ComboPooledDataSource",
fields : {
driverClass : 'com.mysql.jdbc.Driver',
jdbcUrl : 'jdbc:mysql://127.0.0.1:3306/sales1?useUnicode=true&characterEncoding=UTF8',
user : 'root',
password : '123456',
minPoolSize : '20',
maxPoolSize : '40',
initialPoolSize : '20',
maxIdleTime : '25200'
}
},
fileSqlManager : {
type : "org.nutz.dao.impl.FileSqlManager",
args : ['sqls/all.sqls']
},
dao : {
type : 'org.nutz.dao.impl.NutDao',
args : [ {
refer : 'dataSource'
},{
refer : 'fileSqlManager'
} ]
},
dao1 : {
type : 'org.nutz.dao.impl.NutDao',
args : [ {
refer : 'dataSource1'
},{
refer : 'fileSqlManager'
} ]
}
};
使用方法:
@IocBean(fields = { "dao"})//第一个数据源
@InjectName("mainService")
public class MainServiceimpl extends AbstractService implements MainService {
@Inject("dao1")
public Dao dao1;//需要使用的数据源
@Override
public String test() {
String sql_sb = "select * from tb_log where id = 1 ";
Sql sql = Sqls.create(sql_sb.toString());
sql.setCallback(Sqls.callback.entities());
sql.setEntity(this.dao1.getEntity(Log.class));
this.dao1.execute(sql);
sql.getList(Log.class);
return null;
}
}
报错提示为:
2015-11-06 12:54:23,480 [923219673@qtp-1836797772-0] INFO org.nutz.lang.inject.InjectByField - Fail to set value by field
org.nutz.castor.FailToCastObjectException: Fail to cast from <java.lang.String> to <org.nutz.dao.Dao> for {dao1} because:
NullPointerException:null