如何使用MyBatis-Spring在MyBatis中配置2个数据库(How to configure 2 databases in MyBatis using MyBatis-Spring)
我正在尝试在我的应用程序中配置2个不同的数据源,因为它是必需的。
我在AppContext.xml中的配置如下:
<bean id="dataSourceA" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/> <property name="url" value="datasourceAURL"/> <property name="username" value="aaaa"/> <property name="password" value="pppp"/> </bean> <bean id="dataSourceB" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/> <property name="url" value="datasourceBURL"/> <property name="username" value="bbbb"/> <property name="password" value="cccc"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com/ex/myBatis/mappings" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSourceA" /> <property name="typeAliasesPackage" value="com.ex.myBatis.entities"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="configDataSource" /> <property name="typeAliasesPackage" value="com.ex.myBatis.entities"/> </bean> <bean id="sqlSession1" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory1" /> </bean> <bean id="callService" class="com.ex.myBatis.Service.callService"> <property name="sqlSession" ref="sqlSession1" /> </bean>
但是在访问bean时我得到了以下异常
org.springframework.beans.factory.BeanCreationException:创建名为'callService'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配方法:public final void org.mybatis.spring.support.SqlSessionDaoSupport.setSqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate); 嵌套异常是org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义[org.mybatis.spring.SqlSessionTemplate]类型的限定bean:期望的单个匹配bean但找到2:sqlSession,sqlSession1
请有人帮我弄清问题。 如果还有其他方法可以配置2个数据源,请建议我。
I am trying to configure 2 different datasources in my application as it is required.
My configuration in the AppContext.xml is as follows:
<bean id="dataSourceA" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/> <property name="url" value="datasourceAURL"/> <property name="username" value="aaaa"/> <property name="password" value="pppp"/> </bean> <bean id="dataSourceB" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver"/> <property name="url" value="datasourceBURL"/> <property name="username" value="bbbb"/> <property name="password" value="cccc"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com/ex/myBatis/mappings" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSourceA" /> <property name="typeAliasesPackage" value="com.ex.myBatis.entities"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="configDataSource" /> <property name="typeAliasesPackage" value="com.ex.myBatis.entities"/> </bean> <bean id="sqlSession1" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory1" /> </bean> <bean id="callService" class="com.ex.myBatis.Service.callService"> <property name="sqlSession" ref="sqlSession1" /> </bean>
But while accessing the bean I am getting the below Exception
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'callService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public final void org.mybatis.spring.support.SqlSessionDaoSupport.setSqlSessionTemplate(org.mybatis.spring.SqlSessionTemplate); nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.mybatis.spring.SqlSessionTemplate] is defined: expected single matching bean but found 2: sqlSession,sqlSession1
Please somebody help me in figuring out the issue. Also please suggest me if there is someother way to configure 2 Datasources.
原文:
最满意答案
您的if语句涵盖所有逻辑分支。 因此,以下代码块将以任一方式执行。 所以没有理由复制它。 只需将它移到if语句下面即可。
for username in $list do #echo $username sleep $sleep VAL=`curl -s "$host$username" | jq -r ".id" | grep null` #echo $VAL if [[ $VAL == null ]] then echo -e "$username is available" echo "$username" >> username.free else echo -e "$username is taken" fi done
以下是为减少重复而重新编写的代码:
#!/bin/bash # variables host='https://api.example.com/api/users/' sleep=1 # check if jq install if ! jq_loc="$(type -p "jq")" || [ -z "$jq_loc" ]; then echo "no jq installed." sleep 1 echo "we're going to install it now." sleep 2 sudo apt-get install jq fi if [ -n "$1" ] ; then # specified list list=`cat $1` else # no list specified list=`echo {a..z}{a..z}{a..z}` fi for username in $list do #echo $username sleep $sleep VAL=`curl -s "$host$username" | jq -r ".id" | grep null` #echo $VAL if [[ $VAL == null ]] then echo -e "$username is available" echo "$username" >> username.free else echo -e "$username is taken" fi done
Your if statements cover all logical branches. Thus, the following code block will be executed either way. So there is no reason to duplicate it. Simply move it below the if statements.
for username in $list do #echo $username sleep $sleep VAL=`curl -s "$host$username" | jq -r ".id" | grep null` #echo $VAL if [[ $VAL == null ]] then echo -e "$username is available" echo "$username" >> username.free else echo -e "$username is taken" fi done
Here is the code reworked to reduce the duplication:
#!/bin/bash # variables host='https://api.example.com/api/users/' sleep=1 # check if jq install if ! jq_loc="$(type -p "jq")" || [ -z "$jq_loc" ]; then echo "no jq installed." sleep 1 echo "we're going to install it now." sleep 2 sudo apt-get install jq fi if [ -n "$1" ] ; then # specified list list=`cat $1` else # no list specified list=`echo {a..z}{a..z}{a..z}` fi for username in $list do #echo $username sleep $sleep VAL=`curl -s "$host$username" | jq -r ".id" | grep null` #echo $VAL if [[ $VAL == null ]] then echo -e "$username is available" echo "$username" >> username.free else echo -e "$username is taken" fi done
相关问答
更多-
如何在具有AWK语句的bash脚本中使用--enable-switch(How to use --enable-switch in a bash script having AWK statement)[2022-09-21]
你正在使用一个非常旧版本的gawk,默认情况下从4.0开始启用switch语句,我们现在使用4.1.something。 你错过了很多非常有用的功能(比switch语句更有用,请参阅http://www.gnu.org/software/gawk/manual/gawk.html#Feature-History ),所以得到一个新的gawk。 话虽如此,你可以在安装一个新gawk之后继续使用switch(),但恕我直言,不值得让你的脚本专门用于gawk,如果这是你正在使用的唯一gawk特定功能,只需将其重写 ... -
这是你最后一个case中的“读”语句。使用 case $case in c) echo "Temp. F : "$Fahrenheit;; f) echo "Temp. C : "$Celsius;; esac It's the "read" statements in that last case that are doing you in. Use case $case in c) echo "Temp. F : "$Fahrenheit;; f) echo "Temp. C : "$Celsi ...
-
Bash if语句行为(Bash if statement behavior)[2022-06-17]
bash碰巧对空间有点特别。 在运算符周围添加空格: if [ "$baseSystem" == "$testme" ]; then ... if [ "$baseSystem" != "$testme" ]; then 以下不相同: [ "$a"="$b" ] [ "$a" = "$b" ] 你的第一次测试基本上与说if [ "testdir1==NA" ]; then if [ "testdir1==NA" ]; then将永远是真的。 bash happens to be somewhat p ... -
如何在bash脚本中使用小数的if语句进行计算(How to make calculations in if statement with decimals in bash script)[2022-03-25]
bash仅限于整数运算。 如果您有GNU工具(例如Linux),可以使用bc进行浮点比较,如下所示: if echo "$wpm > 0.6" | bc -l | grep -q 1 then echo speed is greater than 0.6 else echo speed is less than or equal to 0.6 fi 使用非GNU bc ,可以使用稍长的语句来完成相同的操作: if echo "if ($wpm > 0.6) print 1 else pri ... -
第一个if不是良构。 这将工作: if [ -z "$message" ] && [ -z "$predefined" ]; then 或这个: if test -z "$message" && test -z "$predefined"; then 或者这个特定于bash的,简单但是肮脏的方式: if [[ -z "$message" ]] && [[ -z "$predefined" ]]; then 或者这个特定于bash的正确方式: if [[ -z $message && -z $prede ...
-
哦,我没有这个。 "scripts": { "info": "echo 'npm as a build tool'", "installBashPlugin": "echo 'source ~/.bash_plugins/.my_plugin' >> ~/.bash_profile" 目标文件周围的单引号: .bash_profile是它出错的地方。 谢谢。 Oh sh*t I got this. "scripts": { "info": "echo 'npm as a build ...
-
使用if语句时,bash脚本给出命令not found错误(bash script gives command not found error when using if statement)[2023-07-13]
在[ :]之后添加一个空格: if [ `echo $(s3cmd ls s3://my-bucket/) | grep -c "file.txt" ` -gt 0 ] 你的反引号命令扩展为1 ,所以现在你有 if [1 -gt 0 ] 这意味着shell正在尝试运行[1作为命令,并且不存在这样的命令。 Add a space after the [: if [ `echo $(s3cmd ls s3://my-bucket/) | grep -c "file.txt" ` -gt 0 ] Your ... -
你只是缺少一些空间...... #!/bin/bash a="ACC_A" b="ACC_B" c="ACC_C" d="ACC_D" USERS="ACC_A ACC_B ACC_C ACC_X ACC_Y ACC_Z" for ACCOUNT in $USERS; do if [ "$ACCOUNT" = "$a" ] || [ "$ACCOUNT" = "$b" ] || [ "$ACCOUNT" = "$c" ] || [ "$ACCOUNT" = "$d" ]; then ...
-
您的if语句涵盖所有逻辑分支。 因此,以下代码块将以任一方式执行。 所以没有理由复制它。 只需将它移到if语句下面即可。 for username in $list do #echo $username sleep $sleep VAL=`curl -s "$host$username" | jq -r ".id" | grep null` #echo $VAL if [[ $VAL == null ]] then echo -e "$username is a ...
-
您可以使用引号并写入"$(launchctl list ...)" ,但它可能更干净: #!/bin/bash if ! launchctl list | grep -q '^\d.*RemoteDesktop.*'; then sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -users admin -access - ...