#java #spring #spring-boot #mybatis
#java #spring #spring-boot #mybatis
Вопрос:
Моя цель — настроить все, используя MyBatis для операций SQL. В каждом найденном мной руководстве еще не упоминается application.properties
, что если я не настроил application.properties
его, он выдаст ошибку.
Я не хочу настраивать spring.datasource.*
, вместо этого я хочу настроить все с помощью MyBatis.
Ошибка # 1
***************************
APPLICATION FAILED TO START
***************************
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).
Process finished with exit code 1
Ошибка # 2
WARN 2560 --- [ main] o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.]' package. Please check your configuration.
==== Эта конфигурация не выдает ошибку # 1.
application.properties
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=javatest
spring.datasource.username=java
spring.datasource.password=java
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
mybatis-config.xml
<configuration xmlns="http://mybatis.org/schema/mybatis-config">
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=javatest"/>
<property name="username" value="java"/>
<property name="password" value="java"/>
</dataSource>
</environment>
</environments>
</configuration>
частичное pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Комментарии:
1. Используете ли вы
mybatis-spring-boot-starter
зависимость?2. @goldthelocks Оказывается, я его использую.
3. используете ли вы
spirng-boot-starter-data-jpa
в своей зависимости?4. @Shawrup нет, я этого не делал. Я буду psot мой pom.xml .
5. @goldthelocks оказывается, mybatis-spring-boot-starter вообще не использовался
.xml
. Я посмотрю, это уменьшает шаблон.
Ответ №1:
Попробуйте добавить свойство below к вашему application.properties
, чтобы определить местоположение вашей конфигурации mybatis:
mybatis.config-location=classpath:mybatis-config.xml
Источник: http://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/#Configuration
Комментарии:
1. Я попробовал ваш в моем текущем проекте, и он по-прежнему выдает предупреждение
o.m.s.mapper.ClassPathMapperScanner : No MyBatis mapper was found in '[com.example.bankaccount]' package. Please check your configuration.
2. Как вы в настоящее время определяете, где находятся ваши файлы mappers?
3. Попробуйте добавить это в свой класс приложения: @MapperScan(basePackages = «com.example.bankaccount»)
4. @goldthelocks Я определяю свой mapper,
.xml
напримерresources/builder/User_LoginMapper.xml
. Я настраиваю<mapper resource />
вresources/mybatis-config.xml
5. @znurglэто работает, если вы не используете
mybatis-config.xml
иUser_LoginMapper.xml
. Пожалуйста, посмотрите на разницу между этими 2 исходными кодами (один использует аннотации и вообще не использует XML) github.com/kidfrom/g2_java/tree/main/etc/mssqlserver а другой — мой текущий банковский проект (он использует XML). github.com/kidfrom/g2_java/tree/main/Bank_Account/java /…