mybatis spring mvc application, getting Invalid bound statement (not found) - spring

this is my first mybatis spring mvc application using spring 3.2.4, mybatis-spring-1.2.1
When i try to call my webservice i get the error::
org.springframework.web.util.NestedServletException: Request processing failed;
nested exception is org.apache.ibatis.binding.BindingException: Invalid bound
statement (not found):
org.mydomain.formulary.drugmaster.dao.DrugMasterDao.getDrugsWithAlert
I must be missing something obvious.
Thanks for any help
Here are my associated files:
applicationContext.xml
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="formularyDb" />
<property name="configLocation" value="file:/web/sites/drugformulary-spring/config/mybatis-config.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mydomain.formulary.mappers" />
</bean>
<bean id="DrugMasterDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="org.mydomain.formulary.drugmaster.dao.DrugMasterDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
mapper file --> /classes/org/mydomain/formulary/mappers/drugmasterDao.xml
<mapper namespace="org.mydomain.formulary.drugmaster.dao.DrugMasterDao">
<select id="getDrugsWithAlert" parameterType="int" resultType="org.mydomain.formulary.drug_master.model.DrugMasters">
Select drug_id,drug_name,drug_alert_date,drug_alert_source, rownum
from (select drug_id,drug_name,to_char(drug_alert_datetime,'MM/DD/YYYY') as drug_alert_date ,drug_alert_source, rownum
from drug_master
where drug_status ='A' and length(drug_alert) > 0
order by drug_alert_datetime DESC )
where
<if test="_parameter != null">
rownum < #{count}
</if>
</select>
</mapper>
mapper file --> /classes/org/mydomain/formulary/drugmaster/dao/DrugMasterDao.java
public interface DrugMasterDao {
public List<DrugMasters> getDrugsWithAlert(int count);
}
controller file --> /classes/org/mydomain/formulary/drugmaster/controller/DrugMasterController.java
#Controller
public class DrugMasterController {
#Autowired
DrugMasterService drugMasterService;
#RequestMapping(value = "/drugmaster/withalerts/count/{count}", method = RequestMethod.GET)
public String withAlerts(ModelMap model, #PathVariable int count) {
List<DrugMasters> drugs = drugMasterService.getDrugsWithAlert(count);
return null/*for now*/;
}
}
service file --> /classes/org/mydomain/formulary/drugmaster/service/DrugMasterServiceImpl.java
#Service
public class DrugMasterServiceImpl implements DrugMasterService {
#Autowired
DrugMasterDao drugMasterDao;
public List<DrugMasters> getDrugsWithAlert(int count){
return drugMasterDao.getDrugsWithAlert(count);
}
}
mybatis-configfile -->
<configuration>
<settings>
<setting name="cacheEnabled" value="false" />
<setting name="lazyLoadingEnabled" value="false" />
</settings>
</configuration>

I googled this answer when looking for my error. It's actually unrelated to OP's problem, but the exception is the same and this question's very visible in google.
In my case I forgot to change the mapper namespace
<mapper namespace="pl.my.package.MyNewMapper">
Which resulted in the same problem.

I had the same problem so after reading the configurations in this page.
https://mybatis.github.io/spring/mappers.html#scan
I saw that my configuration was correct. so debugging my application. found that my *mappers.xml files where not in the path. that expect must be.
I had the XML files in the same folder src "java" in my maven project. so when I build my applications the file were not copy to classes folder. So I have to move the xml files to folder "resources". and the fix the problem.

Because your xml not load in mybatis, MapperScannerConfigurer only scan interface, not xml.
Have two way:
<mappers>
<mapper resource="org/mydomain/formulary/mappers/drugmasterDao.xml"/>
</mappers>
or
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:org/mydomain/**/*.xml"/>
</bean>

i had similar problem for my spring-boot mybatis application.
The issue was mybatis couldn't find the configuration file. After adding
mybatis.config-location=classpath:mybatis-config.xml
in the application.properties file, issue got resolved. Looks like issue is always around configuration files/mapper files and statement names.

In my case, I had multiple DataSource and should set mappler locations for each SessionFactory.
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(mysqlDataSource);
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactory.setMapperLocations(resolver.getResources("classpath:mappers/**/*Mapper.xml"));

(There could be many reasons, my case is a bit rare & weird, it's hard to discover, so I'd like to add an answer here, just in case someone did the same thing as me.)
In my case, then reason is in IDEA, when create multi-level package for mapper file I input mybatis.mapper, which only create a single dir but with name contains ..
While I should actually input mybatis/mapper to create multi-level dir at once.
In these 2 cases, the dir are shown the same as mybatis.mapper in the project view of IDEA, so it took me quiet a while to figure out why ...

I resolved the same problem with a variant of the solution of Giovanni Perea(thank you). I have the .xml mapper files in the same folder with .java mapper files and I using maven with maven-resources-plugin.
In my solution I have add an execution in maven-resources-plugin for copy all the .xml mapper file to the correct location(same folder of the .class mapper files):
<execution>
<id>copy-mappers-xml</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/com/myapplication/mapper</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/java/com/myapplication/mapper/</directory>
<filtering>false</filtering>
<includes>
<include>*.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
More examples with maven-resources-plugin: Including and excluding files and directories
If you do not use maven-resources-plugin see:
https://stackoverflow.com/a/12446666/2473158

In my case it was a typo error in the id of the mapper xml statement e.g.
<select id="getDtaa" resultType="Data">
List<T> getData()
After changing the id in the XML to the correct function name it worked.

most likely the java method name and the XML block's name mismatches
e.g mapper.getUser()
<select id="getUsee" resultMap="student">
...........
<>
getUser obviously different from getUsee

Except for #Dariusz mentioned above, I've also forgotten to add mapper location.
<property name="mapperLocations" value="classpath:mybatis/mappers/*.xml"/>

in the spring configuration, taking a xml way for example, make sure that the mapper.xml files are located at the place assigned as the value of the property named mapperLocations. Once I had it at mappers/anotherfolder/*.xml. And that causes the pain.
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="somepackage.model"/>
<property name="mapperLocations" value="classpath*:mappers/*.xml"/>
</bean>

Rename mapper/drugmasterDao.xml to mapper/DrugMasterDao.xml , the name has to match the *Dao.java file name otherwise it throws error
or
we have to create Ibatismapping.xml explictly and add mapper configuration there

Check if there is any overload method in mapper. Try to use a separate name instead.

I have also encountered this problem in my development.
In generall, if you are using xml configuration files for spring,myBatis and etc., then this problem is mostly caused by some mistake in your xml configuration files.
The most voted answer by Dariusz demonstrated that there maybe some problems in the myBatis xml configuration file, while in my case, I found that problems in spring xml configuration file can also result in this problem.
In the situation of this post, in the applicationContext.xml(which should be a configuration file of Spring), we can see a basePackage configuration for the MapperScannerConfigurer:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="org.mydomain.formulary.mappers" />
</bean>
so if the value for this property is wrong(the package name is not right), it will also result in this problem.

2 Points to cover
Check whether the method name is the same in both the XML and Java methods.
Mapper:scan covers the required package. If you are using annotation then #MapperScan(com.xxx) must be in the defined path.

In my case, using a query in an annotation, i didn't give enough credit to the fact that the XML in the annotation gets interpreted as XML.
So if you have
#Select("select * from table where id <> 'blabla'")
The <> is messing up the XML. You need to put it in a CDATA section
#Select("select * from table where id <![CDATA[ <> ]]> 'blabla'")

I think the issue is that MyBatis can't find the XML mapper configuration file. In my project using Spring Boot with MyBatis-3, I configured the mapper location in the application.properties as shown below:
mybatis.mapper-locations=classpath:mappers/*.xml
If you're using application.yml instead of .properties:
mybatis:
mapper-locations: "classpath:mappers/*.xml"

DrugMasters attributes defines must associate with the drug_id,drug_name,drug_alert_date,drug_alert_source, rownum .

Related

Can not load property file in spring-context.xml. Property file Path is given as a placeholder in dev.properties

I am loading property file in spring-context.xml and i am giving
external property file location in
${spring.profiles.active}.properties which is in classpath and using the location as a placholder in spring-context.xml. My spring-context.xml is:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="searchSystemEnvironment" value="true" />
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="locations" ref="propertyConfigurerFiles" />
</bean>
<bean id="propertyConfigurerFiles" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>/WEB-INF/properties/common.properties</value>
<!--In Developemnet Enviroenment it will be dev.properties-->
<value>/WEB-INF/properties/${spring.profiles.active}.properties</value>
<!--External Property File Location as a Placeholder-->
<value>${app.config.batch.location}</value>
</list>
</constructor-arg>
</bean>
And my dev.properties is:
app.config.batch.location=E:/project/properties/config.properties
My problem is that is ${app.config.batch.location} placeholder is not
resolved in spring-context.xml and its trying to load file
${app.config.batch.location} in place of
E:/project/properties/config.properties.
I hope I explained the problem well. Please help!
Thanks in Advance!!!
You need to create bean of class PropertyPlaceHolderConfigurer.
Not just some ArrayList bean. Why do you think you need this ArrayList bean?
It seems you are using spring profiles, instead of messing with initialization time property value binding what you can do is ...
1) read the property file(profile's)
/WEB-INF/properties/${spring.profiles.active}.properties
2) create a java class that can read these property values. (don't forget to use spring profiles interfacing class)
3) as you are trying to read a property file whose location is embedded in property file(step-1), object created at step-2 will give value for key <value>${app.config.batch.location}</value>
now you can load this property file using available file reader class.
4) create Properties object and access the values in it.
Note:: if any of your bean initialization depends on key-value read at step-4, do initialization manually or create your ***custom class(servlet) that get loaded before any other class (even spring's DispactherServlet).

Tomcat Context-Params ignored in Spring webapp when using PropertyPlaceholder

I was previously using, the now deprecated, class org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer to load a properties file from the server's filesystem. I had the following bean definied:
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="locations" value="${config}"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="searchContextAttributes" value="true"/>
<property name="contextOverride" value="false"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="searchSystemEnvironment" value="false"/>
</bean>
The config is an argument that is passed when starting Tomcat, i.e.
-Dconfig=/path/to/application.properties
For the webapp I also have a context file:
<Context docBase="/path/to/application.war">
<Parameter name="host" value="localhost" override="false"/>
<Parameter name="port" value="8080" override="false"/>
</Context>
If the .properties file, specified by the -Dconfig argument, contains the property that some other bean references then the value from the .properties file is used, otherwise the value from the the context xml file is used.
This allowed me to have a set of default properties deployed with the WAR and if required, I was able to specify a .properties file to override particular values.
Now, I'm updating to use the new property abstractions in Spring 3.1 but I can't seem to figure out what the equivalent approach to this is?
I have the same context file and war deployed in the same way, and I now have the following in the application:
<context:property-placeholder
location="${config}"
system-properties-mode="OVERRIDE"
ignore-resource-not-found="true"
ignore-unresolvable="true"/>
This finds and uses the properties from the properties file, BUT it does not use the values from the context XML file.
How do I get my application to use the context params when using this new property-placeholder?
Thanks.
To summarise the problem is that the Context Parameters from the servlet context file were not being used to resolve placeholders when using the new Property Placeholder namespace introduced in Spring 3.1.
I have figured out a solution, with the following
<context:property-placeholder location="${config}" local-override="true" ignore-resource-not-found="true"/>
I can specify one or more *.properties files on the local filesystem using a JVM arg, eg:
-Dconfig=/path/app.properties
If a placeholder property cannot be resolved after checking the app.properties file then the Servlet Context Parameters are checked.
This allows me to have default values using context params in a the web.xml and where I need to I can override these values by specifying the location of *.properties files using the config JVM arg.
The key to getting it to work this way was to include local-override="true", which is false by default. I'm not fully sure that it makes sense, since the description for that attribute is:
Specifies whether local properties override properties from files. Default
is "false": Properties from files override local defaults.
If the same property key exists in the app.properties and the web.xml the value from the app.properties is used.
Spring uses a default property file unless the user-based property file is defined. If you want to control .properties file, please follow the instructions posted here.
If you want to take advantage of application.properties there are two ways to do it.
<!-- allows for ${} replacement in the spring xml configuration from the
system.properties file on the classpath -->
<util:properties id="appProperties" location="classpath:application.properties"/>
<context:property-placeholder location="classpath:application.properties"/>
util tag lets you to use a Property class to read the properties across your application. For example:
#Autowired
public MyPropertyReader(Properties appProperties) {
String prop1 = appProperties.getProperty("my.address");
String prop2 = appProperties.getProperty("my.version");
}
If you want to use the values within your context file use the context:property-placeholder tag. Then you can use your values as
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory" p:brokerURL="${jms.primary.server}"/>
where for example jms.primary.server=172.168.10.18:6161 in application.properties.

Spring issues with JVM Arguments

I have a normal plain servlet . I am trying to load property file from the file system using Spring ReloadableResourceBundleMessageSource class.The file location is supplied by the JVM arguments. The following is my declaration of my MessageSource bean
<bean id="xmlXpathProperties" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<!-- check property file(s) every 600 second(10min) -->
<property name="cacheSeconds" value="600"/>
<property name="basenames">
<list>
<value>file:#{systemProperties.aircdrconfig}/cdr-airxml</value>
</list>
</property>
/bean>
If i am giving the JVM argument name with any special characters such as dots(.) or hyphen(-) for example : air.cdr.config, I am getting an exception like.
org.springframework.beans.factory.BeanExpressionException Field or property 'air' cannot be found on object of type 'java.util.Properties'
If i remove the dot symbol then it's working fine. Any idea to overcome this problem? Thanks in advance.
You will need to refer to the properties this way:
#{ systemProperties['air.cdr.config'] }

Can you configure velocity to use a toolbox file from the classpath?

I'm using spring with a VelocityViewResolver configured by Spring
<bean id="velocityViewResolver" class="org.springframework.web.servlet.view.velocity.VelocityViewResolver">
<property name="order" value="1" />
<property name="suffix" value=".vm" />
<property name="contentType" value="text/html;charset=UTF-8" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="toolboxConfigLocation" value="/WEB-INF/velocityToolbox.xml" />
</bean>
This works if the velocityToolbox.xml file is under WEB-INF ; however I'm trying to put it in the classpath. Is there a way to do it ?
I've tried specifying toolboxConfigLocation as "classpath:/wherever/velocityToolbox.xml", but it does not find the resource, and I end up with no toolbox configured, and a NPE at runtime (for some reason, it seems like the code expect the location to start with a '/', or add the '/' itself before looking for the resource).
Given that the resource is located using ServletContext.getResourceAsStream, with the content of the toolboxConfigLocation property prefixed by a "/", is there a way I can "define" a resource in my spring config that would somehow 'point' to an actuall classpath resource ?
Any idea is welcome.
I believe it has to be a full path, eg:
/WEB-INF/classes/com/myapp/resources/toolbox.xml
The ServletToolboxManager#getInstance(ServletContext context, String toolboxConfigLocation) method is used to generate a ToolboxManager configured with the toolbox in order to create the ChainedContext used in the VelocityToolboxView.
This method pre-appends the path with a '/' if one does not exist, and then uses ServletContext#getResourceAsStream(String path) to read it in.
With this in mind, you'll have success if you set it as a full path from the context root.

Spring application context external properties?

i have a Spring application and its working well so far. Now i want the properties file in an external config folder and not in the packed jar to change things without the need to repack. This is what i got:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- <property name="locations" value="classpath:/springcontext.properties"/> -->
<property name="locations" value ="config/springcontext.properties" />
The outcommented one is working and the other one i dont get to work :/ Can someone help?
Edit:
Thx 4 comments so far.
Maybe my question wasnt clear enough :). I perform a Maven build and everything will be packaged and i want this folder to be NOT in the package nut next to the outcomming jar and in this folder i want the properties file. possible?
You can try something like this:
<context:property-placeholder
location="${ext.properties.dir:classpath:}/servlet.properties" />
And define ext.properties.dir property in your application server / jvm, otherwise the default properties location "classpath:/" (i.e., classes dir of .jar or .war) would be used:
-Dext.properties.dir=file:/usr/local/etc/
BTW, very useful blog post.
You can use file prefix to load the external application context file some thing like this
<context:property-placeholder location="file:///C:/Applications/external/external.properties"/>
<context:property-placeholder location="classpath*:spring/*.properties" />
If you place it somewhere in the classpath in a directory named spring (change names/dirs accordingly), you can access with above
<property name="locations" value ="config/springcontext.properties" />
this will be pointing to web-inf/classes/config/springcontext.properties
This blog can help you. The trick is to use SpEL (spring expression language) to read the system properties like user.home, to read user home directory using SpEL you could use #{ systemProperties['user.home']} expression inside your bean elements. For example to access your properties file stored in your home directory you could use the following in your PropertyPlaceholderConfigurer, it worked for me.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>file:#{ systemProperties['user.home']}/ur_folder/settings.properties</value>
</property>
</bean>
This question is kind of old, but wanted to share something which worked for me. Hope it will be useful for people who are searching for some information accessing properties in an external location.
This is what has worked for me.
Property file contents:
PROVIDER_URL=t3://localhost:8003,localhost:8004
applicationContext.xml file contents: (Spring 3.2.3)
Note: ${user.home} is a system property from OS.
<context:property-placeholder system-properties-mode="OVERRIDE" location="file:${user.home}/myapp/latest/bin/my-env.properties"/>
<bean id="appsclusterJndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">${PROVIDER_URL}</prop>
</props>
</property>
</bean>
${PROVIDER_URL} got replaced with the value in the properties the file
One way to do it is to add your external config folder to the classpath of the java process. That's how I've often done it in the past.
<context:property-placeholder location="file:/apps/tomcat/ath/ath_conf/pcr.application.properties" />
This works for me.
Local development machine path is C:\apps\tomcat\ath\ath_conf and in server /apps/tomcat/ath/ath_conf
Both works for me

Resources