我对春博特+金塔瓦利卡塔基斯纳塔基斯加基斯群岛的基本处理有问题。
将所有表格的栏目与QueryDSL相提并重,并将试验清单退回
QRoles roles = QRoles.roles;
List<Roles> roleList = sqlQueryFactory.select(roles)
.from(roles)
.fetch();
logger.debug(roleList.toString());
2023/06/29 22:38:43 ERROR [http-nio-8080-exec-10] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.jdbc.BadSqlGrammarException: null; bad SQL grammar [select roles
from roles]] with root cause
java.sql.SQLSyntaxErrorException: Unknown column roles in field list
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:121)
SQLError.java:121
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
SQLExceptionsMapping.java:122
at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:916)
ClientPreparedStatement.java:916
at com.mysql.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:972)
ClientPreparedStatement.java:972
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeQuery(ProxyPreparedStatement.java:52)
ProxyPreparedStatement.java:52
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeQuery(HikariProxyPreparedStatement.java)
at com.querydsl.sql.AbstractSQLQuery.fetch(AbstractSQLQuery.java:445)
AbstractSQLQuery.java:445
at com.example.demo.service.LoginUserDetailsService.listing(LoginUserDetailsService.java:88)
<>strong>bad-> 选定角色
the SQL I expected -> select roles.* from roles
pom.xml >>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<classifier>jakarta</classifier>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<classifier>jakarta</classifier>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-sql</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-sql-spring</artifactId>
<version>${querydsl.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
pom.xml <<
bash-4.4# echo $JAVA_HOME
/usr/java/openjdk-17
bash-4.4# java -version
openjdk version "17.0.2" 2022-01-18
OpenJDK Runtime Environment (build 17.0.2+8-86)
OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)
ash-4.4# mysql --version
mysql Ver 8.0.33 for Linux on aarch64 (MySQL Community Server - GPL)
bash-4.4# "/app/demo/mvnw" spring-boot:run -f "/app/demo/pom.xml"
2023/06/29 22:38:21 INFO [restartedMain] - Starting DemoApplication using Java 17.0.2 with PID 35542 (/app/demo/target/classes started by root in /app)
2023/06/29 22:38:21 DEBUG [restartedMain] - Running with Spring Boot v3.1.1, Spring v6.0.10
2023/06/29 22:38:21 INFO [restartedMain] - The following 1 profile is active: "dev"
2023/06/29 22:38:22 INFO [restartedMain] - Bootstrapping Spring Data JPA repositories in DEFAULT mode.
mysql> show create table roles;
CREATE TABLE `roles` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_by` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_by` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
作用。
package com.example.demo.Entity;
import java.time.LocalDateTime;
import com.querydsl.core.annotations.QueryEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Data;
@Entity
@Table(schema = Roles.SCHEMA_NAME, name = Roles.TABLE_NAME)
@Data
@QueryEntity
public class Roles {
public static final String SCHEMA_NAME = "sample_schema";
public static final String TABLE_NAME = "roles";
@Id
private Integer id;
private String name;
private String createdBy;
private LocalDateTime createdAt;
private String updatedBy;
private LocalDateTime updatedAt;
}
作用。
Q作用。
package com.example.demo.Entity;
import static com.querydsl.core.types.PathMetadataFactory.*;
import com.querydsl.core.types.dsl.*;
import com.querydsl.core.types.PathMetadata;
import javax.annotation.processing.Generated;
import com.querydsl.core.types.Path;
/**
* QRoles is a Querydsl query type for Roles
*/
@Generated("com.querydsl.codegen.DefaultEntitySerializer")
public class QRoles extends EntityPathBase<Roles> {
private static final long serialVersionUID = 14135660L;
public static final QRoles roles = new QRoles("roles");
public final DateTimePath<java.time.LocalDateTime> createdAt = createDateTime("createdAt", java.time.LocalDateTime.class);
public final StringPath createdBy = createString("createdBy");
public final NumberPath<Integer> id = createNumber("id", Integer.class);
public final StringPath name = createString("name");
public final DateTimePath<java.time.LocalDateTime> updatedAt = createDateTime("updatedAt", java.time.LocalDateTime.class);
public final StringPath updatedBy = createString("updatedBy");
public QRoles(String variable) {
super(Roles.class, forVariable(variable));
}
public QRoles(Path<? extends Roles> path) {
super(path.getType(), path.getMetadata());
}
public QRoles(PathMetadata metadata) {
super(Roles.class, metadata);
}
}
Q作用。
QuerydslSql QueryConfig.java >>
package com.example.demo.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import com.querydsl.jpa.JPQLQueryFactory;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.querydsl.sql.Configuration;
import com.querydsl.sql.MySQLTemplates;
import com.querydsl.sql.SQLQueryFactory;
import com.querydsl.sql.SQLTemplates;
import com.querydsl.sql.spring.SpringConnectionProvider;
import com.querydsl.sql.spring.SpringExceptionTranslator;
import jakarta.persistence.EntityManager;
@org.springframework.context.annotation.Configuration
public class QuerydslSqlQueryConfig {
@Autowired
private DataSource dataSource;
@Autowired
private EntityManager entityManager;
@Bean
public Configuration configuration() {
SQLTemplates templates = MySQLTemplates.builder().build();
Configuration configuration = new Configuration(templates);
configuration.setExceptionTranslator(new SpringExceptionTranslator());
return configuration;
}
@Bean
public JPQLQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}
@Bean
public SQLQueryFactory sqlQueryFactory() {
return new SQLQueryFactory(configuration(), new SpringConnectionProvider(dataSource));
}
}
QuerydslSql QueryConfig.java <<
LoginUserDetailsservice.java >>
@Service
public class LoginUserDetailsService {
@Autowired
SQLQueryFactory sqlQueryFactory;
@Transactional
public AccountListingResponseModel listing() {
QRoles roles = QRoles.roles;
List<Roles> roleList = sqlQueryFactory.select(roles)
.from(roles)
.fetch(); // <-- error line
logger.debug(roleList.toString());
...
}
b. Log。
Do you know what is causing this? please help me
Uploaded sample source to GitHub https://github.com/Elendira/hello_querydsl/tree/v.1.0.1/demo/src/main/java/com/example/demo https://github.com/Elendira/hello_querydsl/releases/tag/v.1.0.1
-- Elen