problems_springboot

2021/8/24 6:05:57

本文主要是介绍problems_springboot,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

目录
  • 1 springbooot中,整合mybatis时,在IUserMapper接口中,
  • 2 Spring Data JPA的多数据源配置时,执行sql失败
  • 3 springboot run with flyway failed
  • 4 idea中springboot项目打包多module的maven项目时报错
  • 5
  • 6
  • 7
  • 8

1 springbooot中,整合mybatis时,在IUserMapper接口中,

@Select("select * from user where name like '%李%'")
List<User> findUserByName(String name);
@Select的sql语句查不到数据。

RCA: mysql的url中,
application.properties的配置中,配置了spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring5
未指定编码,导致中文乱码问题,所以后台就查不到数据了。

solution: url中指定字符编码,配置如下:
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/spring5?useSSL=true&useUnicode=true&characterEncoding=UTF-8

2 Spring Data JPA的多数据源配置时,执行sql失败

desc: error log below:

Caused by: org.hibernate.exception.SQLGrammarException: error performing isolated work
Caused by: java.sql.SQLSyntaxErrorException: Table 'test.hibernate_sequence' doesn't exist

RCA:
need to explicitly specify the primary key's auto-increment strategy to GenerationType.IDENTITY.

solution:

@Entity
@Data
@NoArgsConstructor
public class User {
    @Id
    // 需要明确指定主键的自增策略,否则报错:SQLSyntaxErrorException: Table 'test.hibernate_sequence' doesn't exist
    //   strategy 默认值GenerationType.AUTO,需要修改为GenerationType.IDENTITY
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

3 springboot run with flyway failed

desc: error log below:

Caused by: org.flywaydb.core.api.FlywayException: Found non-empty schema(s) `test` but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history table.

RCA:
如果我们并不是在项目之初就启用flyway的话,那么在有历史数据的情况下,启用flyway后,由于数据库中,并不存在flyway所依赖的表,所以将会出现:set baselineOnMigrate to true to initialize the schema history table 的错误。
solution:
add config item spring.flyway.baselineOnMigrate=true to application.properties or application.yml
note:
Maybe it should be flyway.baselineOnMigrate=true in springboot 1.x. If the above config is invalid, we can try flyway.baselineOnMigrate=true.

4 idea中springboot项目打包多module的maven项目时报错

desc: error log below:
Could not find artifact com.mediocre:parentProject:pom:1.0-SNAPSHOT

RCA:
无法解析父级Maven的pom.
子级Maven的pom文件中,relativePath标签使用的默认值:<relativePath/>,文档中介绍默认值表示<relativePath>../pom.xml</relativePath>,但编译环境(IDEA和命令行)不认这种写法。

solution:
只有显式地写为<relativePath>../pom.xml</relativePath>,再次执行mvn clean package名称,编译通过,没有报错。
后来,测试发现只要注释掉 <relativePath>../pom.xml</relativePath> 也能正常打包了。
还有人说,多模块项目构建时,先将parent项目要先install一回,之后子项目才可以运行mvn compile命令,否则就会报如上异常。

extension: about packaging
Maven中pom.xml的packaging类型: 项目的打包类型:pom、jar、war.
父模块必须以pom打包类型,同时以给出所有的子模块。
而对于各个子项目,需要在其对应的pom文件开头申明对父级项目的引用,通过GAV(groupId, artifactId, version)实现。对于子项目自己的GAV配置,GV如果不配置,则会从父级项目的配置继承过来。子模块可通过dependencies标签来添加自己的依赖,此外子类项目的packaging值只能是war或者jar,前面已经说过,packaging默认是jar类型。如果是需要部署的项目,则需要打包成war类型,如果只是内部调用或者是作服务使用,则推荐打包成jar类型。

5

6

7

8



这篇关于problems_springboot的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程