Я пишу веб-приложение с использованием Spring и Hibernate, но впервые сталкиваюсь с такого рода проблемами.Всякий раз, когда я запускаю свое приложение на сервере, оно говорит ** «java.sql.SQLSyntaxErrorException: таблица« restaurantapp.users »не существует». ** Я не понимаю, что у меня даже нет таблицыназывается "пользователи" в моей базе данных, а также я никогда не использовал таблицу "пользователи" в моем приложении.Части кода ниже.Нужна помощь, чтобы решить эту проблему.
Класс сущности:
package com.jafndy.Restaurant.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="admin_login")
public class RestaurantAdmin {
@Id
@Column(name="user_id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int user_id;
@Column(name="username")
private String username;
@Column(name="authority")
private String authority;
@Column(name="email")
private String email;
@Column(name="password")
private String password;
public RestaurantAdmin(int user_id, String username, String authority,
String email, String password) {
super();
this.user_id = user_id;
this.username = username;
this.authority = authority;
this.email = email;
this.password = password;
}
public RestaurantAdmin() {
}
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "RestaurantAdmin [user_id=" + user_id + ", username=" + username + ", authority=" + authority
+ ", email=" + email + ", password=" + password + "]";
}
}
Классы конфигурации:
package com.jafndy.Restaurant.config;
import java.beans.PropertyVetoException;
import java.util.Properties;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
@EnableWebMvc
@EnableTransactionManagement
@ComponentScan(basePackages="com.jafndy.Restaurant")
@PropertySource("classpath:persistence-mysql.properties")
public class AppConfig {
//set up variable to hold variables
@Autowired
private Environment env;
//define a bean for the view resolver
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new
InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/view/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
//define a bean for security datasource
@Bean
public DataSource securityDataSource() {
//create a connection pool
ComboPooledDataSource securityDataSource = new ComboPooledDataSource();
//set the jdbc driver class
try {
securityDataSource.setDriverClass(env.getProperty("jdbc.driver"));
}catch(PropertyVetoException exc){
throw new RuntimeException();
}
//set database connection properties
securityDataSource.setJdbcUrl(env.getProperty("jdbc.url"));
securityDataSource.setUser(env.getProperty("jdbc.user"));
securityDataSource.setPassword(env.getProperty("jdbc.password"));
//set connection pool properties
securityDataSource.setInitialPoolSize (getIntProperty ("connection.pool.initialPoolSize "));securityDataSource.setMinPoolSize (getIntProperty ( "connection.pool.minPoolSize"));securityDataSource.setMaxPoolSize (getIntProperty ( "connection.pool.maxPoolSize"));securityDataSource.setMaxIdleTime (getIntProperty ("connection.pool.maxIdleTime"));
return securityDataSource;
}
//define a bean for Hibernate
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(securityDataSource());
sessionFactory.setPackagesToScan("com.jafndy.Restaurant.entity");
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public PlatformTransactionManager hibernateTransactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
private final Properties hibernateProperties() {
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
hibernateProperties.setProperty("hibernate.show_sql", "true");
return hibernateProperties;
}
//helper method
//read environment property and convert it to int
private int getIntProperty(String propName) {
String propValue = env.getProperty(propName);
int intPropValue = Integer.parseInt(propValue);
return intPropValue;
}
}
package com.jafndy.Restaurant.config;
import
org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MySpringMvcDispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
// TODO Auto-generated method stub
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
// TODO Auto-generated method stub
return new Class[] {AppConfig.class};
}
@Override
protected String[] getServletMappings() {
// TODO Auto-generated method stub
return new String[] {"/"};
}
1015 *
package com.jafndy.Restaurant.config;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//add a reference to our security DataSource
@Autowired
private DataSource securityDataSource;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(securityDataSource);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/").hasAnyRole("CUSTOMER","ADMIN")
.antMatchers("/systems/**").hasRole("ADMIN")
.and()
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/authenticateUser")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.exceptionHandling().accessDeniedPage("/access-denied");
}
}
package com.jafndy.Restaurant.config;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
}
Условия контроллера:
package com.jafndy.Restaurant.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class RestaurantControllerLogin {
@GetMapping("/login")
public String loginPage() {
return "login-page";
}
}
package com.jafndy.Restaurant.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class RestaurantController {
@GetMapping("/")
public String showHome() {
return "home";
}
}
файл persistence-mysql.properties
#
# JDBC connection libraries
#
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/restaurantapp?useSSL=false
jdbc.user=restaurant
jdbc.password=restaurant_1_2_3
#
# Connection pool properties
#
connection.pool.initialPoolSize=5
connection.pool.minPoolSize=5
connection.pool.maxPoolSize=20
connection.pool.maxIdleTime=3000
#
# Setup Hibernate session factory
#
hibernate.packagesToScan=com.jafndy.Restaurant.entity
login-page.jsp
<%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<style>
.error{
color: red;
}
.logout{
color: green;
}
</style>
</head>
<body>
<h2>Restaurant Login</h2>
<form:form action="${pageContext.request.contextPath}/authenticateUser"
method="POST">
<c:if test="${param.error != null }">
<b class="error">Invalid username or password</b>
</c:if>
<c:if test="${param.logout != null }">
<i class="logout">You've been logged out</i>
</c:if>
<p>
Username: <input type="text" name="username"/>
</p>
<p>
Password: <input type="password" name="password"/>
</p>
<input type="submit" value="Log in"/>
</form:form>
</body>
</html>
home.jsp
<!DOCTYPE html>
<html>
<head>
<style>
h1{
display: none;
}
</style>
</head>
<body>
<h1 id="h1hidden"></h1>
<button
onclick="document.getElementById('h1hidden').style.display='block'">Click to
see</button>
</body>
</html>
И мой журнал ошибок
org.springframework.security.authentication.InternalAuthenticationServiceException: PreparedStatementCallback;плохая грамматика SQL [выберите имя пользователя, пароль, включенный от пользователей, где username =?];вложенным исключением является java.sql.SQLSyntaxErrorException: таблица «restaurantapp.users» не существует в org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser (DaoAuthenticationProvider.java:119) в org.springframe.work.AbstractUserDetailsAuthenticationProvider.authenticate (AbstractUserDetailsAuthenticationProvider.java:144) при org.springframework.security.authentication.ProviderManager.authenticate (ProviderManager.java:174) при org.springframework.security.authentication.ProviderManager.authenticate (ProviderManager.java:199) вorg.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:334) в оrg.springframework.security.web.authentication.logout.LogoutFilter.web.csrf.CsrfFilter.doFilterInternal (CsrfFilter.java:124) в org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRequestFilter.java:107) в org.springfrailFirFirFinFirFile.FileFirCheFJava: 334) в org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal (HeaderWriterFilter.java:66) в org.springframework.web.filter.OncePerRequestFilter.doFilter (OncePerRejrame..web.ilterChain.Java: 107) в org.springframework.security.web.FilterChainProxy $ VirtualFilterChain.doFilter (FilterChainProxy.java:334)по адресу org.springframework.security.web.FilterChainProxy.doFilterInternal (FilterChainProxy.java:215) по адресу org.springframework.security.web.FilterChainProxy.doFilter (FilterChainProxy.java:178), юридический центр(DelegatingFilterProxy.java:357) по адресу org.springframework.web.filter.DelegatingFilterProxy.doFilter (DelegatingFilterProxy.java:270) по адресу org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilg: 19. Atjg.hag).catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java:166) в org.apache.catalina.core.StandardWrapperValve.invoke (StandardWrapperValve.java:199) в org.apache.catalina.core.96) в org.apache.catalina.authenticator.AuthenticatorBase.invoke (AuthenticatorBase.java:490) в org.apache.catalina.core.StandardHostValve.invoke (StandardHostValve.java:139) в org.apache.catEror.invoke (ErrorReportValve.java:92) в org.apache.catalina.valves.AbstractAccessLogValve.invoke (AbstractAccessLogValve.java:668) в org.apache.catalina.core.StandardEngineValve.invoke (StandardEngineValve.java:74) в org.apconina.cat.service (CoyoteAdapter.java:343) в org.apache.coyote.http11.Http11Processor.service (Http11Processor.java:408) в org.apache.coyote.AbstractProcessorLight.process (AbstractProcessorLight.java:66) в org.coyote.AbstractProtocol $ ConnectionHandler.process (AbstractProtocol.java:770) в org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun (NioEndpoint.java:1415) в org.apache.tomcat.util.net.SocketProcessorBase.запустить (SocketProcessorBase.java:49) в java.util.concurrent.ThreadPoolExecutor.runWorker (неизвестный источник) в java.util.concurrent.ThreadPoolExecutor $ Worker.run (неизвестный источник) в org.apache.tomcat.util.threadkas$ WrappingRunnable.run (TaskThread.java:61) в java.lang.Thread.run (неизвестный источник). Вызывается: org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback;плохая грамматика SQL [выберите имя пользователя, пароль, включенный от пользователей, где username =?];Вложенное исключение - java.sql.SQLSyntaxErrorException: таблица «restaurantapp.users» не существует в org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate (SQLErrorCodeSQLExceptionTranslator.jallxrans.t.service.rans.t.service.pr.j.service.pr.j.service.t.service.serg.service.t.service.t.service.serg.service.t.service.t.service.t.service.t.dll.быть.tml.t.dll.t.j.serg.service.jp.tv.service.t.dll.serg.serg.serg.service.jp.set.dll.быть.tml.t.dll.t.v..dll). исключение. исключение.(AbstractFallbackSQLExceptionTranslator.java:72) в org.springframework.jdbc.core.JdbcTemplate.translateException (JdbcTemplate.java:1402) в org.springframework.jdbc.core.JdbcTemplate.base.rat.jt (jdbc.core.700) по адресу org.springframework.jdbc.core.JdbcTemplate.query (JdbcTemplate.java:751) по адресу org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl.loadUsersByUsername (Jdb.jj.jpg.jj..core.userdetails.jdbc.JdbcDaoImpl.loadUserByUsername (JdbcDaoImpl.java:184) в org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser (DaoAuthenticationProvider.java:104) ... еще 42 вызвано: java.sql.SQLStantserserEне существует в com.mysql.cj.jdbc.exceptions.SQLError.createSQLException (SQLError.java:118) в com.mysql.cj.jdbc.exceptions.SQLError.createSQLException (SQLError.java:95) в com.mysq.cj.jdbc.exceptions.SQLExceptionsMapping.translateException (SQLExceptionsMapping.java:122) по адресу com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal (ClientPreparedStatement.java:960) Client.Revice.Exp.Java: 1019)в com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery (NewProxyPreparedStatement.java:76) в org.springframework.jdbc.core.JdbcTemplate $ 1.doInPreparedStatement (JdbcT66.raj.raj.JdbcTemplate.execute (JdbcTemplate.java:605) ... еще 49