我正在使用Spring和Hibernate编写Web应用程序,但第一次遇到这种问题。每当我在服务器上运行我的应用程序时,它都会说**“java.sql.SQLSyntaxErrorException:表'restaurantapp.users'不存在。**我不明白的是,我的数据库中甚至没有一个名为“users”的表,而且我从未在我的应用程序中使用过表“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.InitialPooleSize”);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 . spring framework . 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[] {"/"};
}
}
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
登录-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>
还有我的错误日志
这就是Spring提供的魔力。当您使用< code > JDBC authentic ation()配置Spring Security性时,Spring将使用默认的< code > DaoAuthenticationProvider 来验证传入的请求。您已经创建了自己的< code>User类,但是Spring无法检测到它,所以它使用< code>JdbcDaoImpl作为< code>UserDetailsService的默认实现。
仔细查看源代码可获得以下信息:
因此,为了使用您自己的实现,您必须提供< code>UserDetailsService的自定义实现,它从您自己的表中加载用户。
这是从spring security的用户服务中搜索用户表。您需要提供与Spring Security相关的正确配置,或者您可以在内存数据库中使用一些虚拟用户和角色。所以显然在这一点上是Spring Security配置问题。希望有帮助..我现在不在我的笔记本电脑前,稍后将调试和发布更珍贵的。
我有javaSpring启动应用程序。我想对经常读取的数据使用缓存。为此,我在我的jar中包含了以下依赖项 我还使用了@EnableCaching注释 使用@Cacheable注释和返回要缓存的数据的函数 但我仍然无法缓存数据。有什么我遗漏的吗?
我们正在尝试将我们的微服务迁移到Spring Boot 2,目前我们正在使用Spring Boot 1.5.6。释放 在迁移过程中,我们发现我们的微服务部分损坏,在日志文件中我们发现了以下错误: com.mysql.jdbc.exceptions.jdbc:表'acme_ms.hibernate_sequence'不存在 目前我们的应用程序中只存在一个域类: 我们发现问题与策略类型有关,我们试图将
当我测试我的项目时,数据库中只创建了4个表,但没有创建其他表,我不知道为什么。创建了表、、和,但没有创建表和我没有放在本例中的其他表。有一些属性我忘记了?感谢您的帮助。以下是一些文件: hibernate.cfg.xml Position.hbm.xml 拒绝位置.hbm.xml 通知.hbm.xml Demande.java Demande.hbm.xml User.java 用户.hbm.xm
问题内容: 我删除了一些与应用程序相关的表。再试一次syncdb命令 它显示错误 models.py 我该如何获取该应用程序的表格? 问题答案: 删除表(您已经做过), 在model.py中注释掉模型, 和.. 如果Django版本> = 1.7: 其他 在models.py中注释模型 转到步骤3。 但是 这次没有 --fake
问题内容: 我删除了一些与应用程序相关的表。再试一次syncdb命令 它显示错误 models.py 我该如何获取该应用程序的表格? 问题答案: 删除表(你已经做过), 在model.py中注释掉模型, 和.. 如果Django版本> = 1.7: 其他 在models.py中注释模型 转到步骤3。但是这次没有–fake
我正在尝试将Employee实体插入数据库。我使用的是JPA,数据库是MySQL。当我尝试插入实体时,它给我的是Test.Employee不存在。我假设我不必创建表。实体将自动创建带有注释名称的表。请找到下面的异常和代码。 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd“>