How do I set a mybatis property in spring boot application.properties file? - spring

I am working on a spring boot application which uses mybatis. My mybatis mapper xml has a SQL query which contains a database schema name that needs to be set from the application properties. I have a schema name and I want to reference it in my mapper.xml as ${schema-name}. I can do this in mybatis-config.xml - something like this:
<configuration>
<PropertiesSource url='my properties file"/>
<properties>
<property name='schema-name' value='${database.schema}'/>
</properties>
</configuration>
But I don't want to use mybatis-config.xml. I want to use the spring boot application.properties for mybatis configuration.
Is there a way to set the mybatis configuration property in spring boot application.properties? Can someone help me with this?

If you use the mybatis-spring-boot-starter 1.2.0, you can specify as follows:
mybatis.configuration-properties.schema-name=abc
or
mybatis.configuration.variables.schema-name=abc
If you use the mybatis-spring-boot-starter 1.1.1, you can specify as follow:
mybatis.configuration.variables.schema-name=abc
If you use the mybatis-spring-boot-starter 1.0.x, you cannot specify on application.properties. (please update to 1.1.1+)
Please try it.
Thanks.

In the application.properties file put:
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://host/table_name
spring.datasource.username=user
spring.datasource.password=password

Related

Spring boot application with Spring-boot-starter-data-jpa and spring-boot-starter-test makes spring does not find application properties

I have created a small application with spring boot that access to a database (mySql) using spring data:
spring-boot-starter-parent: 2.4.2
spring-boot-starter-web: 2.4.2
spring-boot-starter-data-jpa: 2.4.2
mysql-connector-java: 8.0.22
spring-boot-starter-test: 2.4.2
org.junit.jupiter: 5.7.0
junit-jupiter-engine: 5.7.0
I have configured my application.properties, inside src/main/resources, and I have configured the properties for url, username, password, driver, ..., like this:
spring.datasource.url=jdbc:mysql://localhost:3306/BBDD
spring.datasource.username=XXX
spring.datasource.password=XXXX
spring.datasource.driver=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect:org.hibernate.dialect.MySQL5Dialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
I have an interface for my repository for access to my database:
#Repository
public interface ArticleRepository extends PagingAndSortingRepository<ArticuloEntity, Integer> {
}
also, I have defined my entity.
The issue consists on when I start my webapp, I get an error because it does not read the parameters for configuring the datasource from the application.properties:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and
no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following: If you want an embedded database (H2, HSQL or
Derby), please put it on the classpath. If you have database settings
to be loaded from a particular profile you may need to activate it (no
profiles are currently active).
If I remove the dependencies for spring-boot-starter-test and junit, the application loads fine, and it access to database. But if I add the dependency for adding my junits, it does not read my application.properties file and I get the previous error.
How can I have my application configured with junits and reading the configuration for my application with the application.properties file?
Thank you in advance!
Your test/application.properties needs to be configured as well. Try adding this to your test application properties file
spring.datasource.url=jdbc:h2:mem:BBDD;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
and this to your pom if you don't already support h2.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
check your application.properties
changing
spring.datasource.driver=com.mysql.jdbc.Driver
to
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

How to get data for a particular log4j2 property from an alternative source (application.properties)

Having a standard configuration for log4j2 and spring property-file on classpath application.property.
log4j2.xml
<Properties>
...
<Property name="APP_LOG_ROOT">${bundle:application:log.root.dir}</Property>
...
</Properties>
application.properties
...
log.root.dir=/opt/tomcat/logs
...
The data is read into the log4j2.xml correctly, but what if I want to get an alternative property when creating an artifact with maven and put diferent application.property:
mvn clean install -Dapplication.properties.path=file:/some_path/application.properties
?
After that, I can correctly read the new properties.
#Value("${log.root.dir}")
private String ololo;
but the log4j2 cannot do this on its own.
If you want to use any value from Spring's Environment in a Log4j2 configuration file, you need to use the Spring Boot Lookup.
After adding log4j-spring-boot to your classpath, you just need to replace
${bundle:application:log.root.dir}
with
${spring:log.root.dir}

How to set a spring datasource url to a resources folder?

I have a hsqldb file in my project resources folrder "src/main/resources/mydb.data"
In my application.properites I need to set the path with spring.datasource.url=
How I can achieve that?
spring.datasource.url=.... ?
One solution would be to use Maven resource filtering. Just use something like this in your application.properties:
spring.datasource.url=jdbc:hsqldb:file:#basedir#/src/main/resources/mydb.data
Instead of using basedir you can define something like darasource-url in your pom.xml and use this property in your application.properties:
pom.xml
<project>
<!-- ... -->
<properties>
<datasource-url>jdbc:hsqldb:file:${basedir}/src/main/resources/mydb.data</datasource-url>
</properties>
<!-- ... -->
</project>
application.properties:
spring.datasource.url=#datasource-url#
Notice: Using the spring-boot-starter-parent as parent, you have to use #..# instead of ${..}, see 2.1.1. Automatic Property Expansion Using Maven in the Spring Boot How-to-Guides.

How to externalize application.yml in spring boot

Hi Currently my project is using application.yml from src/main/resources by default. I want to use the application.yml file in a different location, so as I can edit the properties whenever I want. Please suggest any idea
Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration. Property values can be injected directly into your beans by using the #Value annotation, accessed through Spring’s Environment abstraction, or be bound to structured objects through #ConfigurationProperties.
Detailed Information: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-yaml
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Set JAVA_OPTS environment variable example
-Dspring.profiles.active=dev -Dspring.config.location=file:C:/application-external.yml
This will allow you to have provide multiple profiles inside of a YML file and let spring do the heavy lifting of evaluating the correct properties:
spring:
profiles: dev
someproperty: devproperty
---
spring:
profiles: test
someproperty: testproperty
To use external configuration files in your Maven build : configure the maven surefire plugin like this in your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dspring.config.location=file:${home}/conf/application-external.yml
</configuration>
</plugin>

Netbeans 8 won't reload static Thymeleaf files

I am using Spring Boot and Thymeleaf via Maven. I can't seem to get Netbeans to automatically re-deploy any of my Thymeleaf template files when I make changes. In order to see the changes I need to do a full clean/build/run. This takes way too long.
The templates are in src/main/resources/templates. I have an application.properties file in src/main/resources/ with spring.thymeleaf.cache=false and spring.template.cache=false.
I have "Compile on save", "Copy resources on save" and "Deploy on save" turned on in the project settings.
My maven build produces a war file that Netbeans deploys to Tomcat and I am using the annotation #EnableAutoConfiguration.
Netbeans does hot deploy changes to the Java classes but not for any of the static files in src/main/resources/.
Software in use:
Mac OS X 10.9.4
Java 1.8
Netbeans 8.0.1
Tomcat 8.0.12
Spring Boot 1.1.7
Thymeleaf 2.1.3 (via Spring Boot)
Any guidance is much appreciated.
An option would be to look into configuring Thymeleaf's FileTemplateResolver
To do that with Spring Boot, define a bean implementing the ITemplateResolver interface with the name defaultTemplateResolver, when present, Spring Boot would take it instead of its default, here is how that would be done, and assuming you have component scanning active so this configuration class will be picked up automatically:
#Configuration
public class ThymeleafConfiguration {
#Bean
public ITemplateResolver defaultTemplateResolver() {
TemplateResolver resolver = new FileTemplateResolver();
resolver.setSuffix(".html");
resolver.setPrefix("path/to/your/templates");
resolver.setTemplateMode("HTML5");
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(false);
return resolver;
}
}
The prefix should be a relative path that when added to your runtime working directory (cwd), would resolve to the templates directory. If you are unsure, set that to the full absolute path, but then there would be no point of the above bean. Since setting the spring.thymeleaf.prefix property to an absolute path would probably have the same effect.
Have been looking for a solution to my eclipse+thymeleaf+sprint boot reloading templates dynamically for a log while....
Finally I found this question here and spring.thymeleaf.cache=false and spring.template.cache=false fixed my issue.
Besides setting the Thymeleaf views as non-cacheable by ie. spring.thymeleaf.cache=false in your application.properties,
try explicitly defining the resource directory in your pom.xml:
<build>
...
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
...
</build>
To deal with that, the spring-boot-maven-plugin in the pom.xml should seems like this:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.0.RELEASE</version>
</dependency>
</dependencies>
</plugin>
And add this to your application properties:
spring.thymeleaf.cache=false
It usually works to Spring beans too.
Just to say this works nicely for me using an external instance of Tomcat:
Run Tomcat with JRebel or Spring Loaded javaagent as a VM option
Turn off "Compile on save", "Copy resources on save" and "Deploy on save"
Add a custom action in Netbeans that executes the compile goal
Run that when you want to see an update
http://wiki.netbeans.org/MavenBestPractices#Binding_Maven_goals_to_IDE_actions
https://github.com/spring-projects/spring-loaded
https://zeroturnaround.com/software/jrebel/quickstart/standalone/
Or you can you use the embedded tomcat with spring-boot-maven-plugin and Spring Loaded instead, then you won't need the compile action:
https://docs.spring.io/spring-boot/docs/current/reference/html/howto-hotswapping.html
I had this problem too. I notice Netbeans reload automaticaly webpages that are in
/src/main/webapp/
You have to move all your templates from /src/main/resources/templates to this directory.
Also you have to change the spring boot property on application.properties file:
spring.thymeleaf.prefix=templates/
That works for me
I had this same issue on Netbeans 8.0.2 and Windows. I was building a WAR to be deployed to Tomcat, but I wanted to try out Spring Boot. It looks like newer versions of Netbeans might resolve this with the Spring Boot plugin or using Eclipse. It seemed nutty to swap IDEs over something tiny like this. I tried all of the suggestions I could find; spring loaded, caching properties, extending the TemplateResolver...I couldn't get any of them to work. I finally stumbled on this blog and following these instructions solved my issue.

Resources