【发布时间】:2017-08-11 23:59:33
【问题描述】:
我知道这个问题可能是重复的,但这些解决方案都不适合我。
我希望自动创建表格。但是,当我将带有 JSON 格式的 USER 对象的 POST 请求发送到服务器时,出现以下异常。
例外:
{
"timestamp": 1490023621440,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.jdbc.BadSqlGrammarException",
"message": "PreparedStatementCallback; bad SQL grammar [INSERT INTO user(lastName, firstName, dob, phone, email, password) VALUES (?, ?, ?, ?, ?, ?)]; nested exception is java.sql.SQLSyntaxErrorException: Table 'master_slave.user' doesn't exist",
"path": "/main"
}
application.properties
spring.datasource.url=jdbc:mysql://${writerendpoint}:${port}/${database.name}
spring.datasource.username=root
spring.datasource.password=root
#spring.datasource.driverClassName=com.mysql.jdbc.driver
writerendpoint=localhost
port=3306
database.name=master_slave
spring.session.store-type=none
# ===============================
# = JPA / HIBERNATE
# ===============================
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.jpa.show-sql = true
# Ddl auto must be set to "create" to ensure that Hibernate will run the
# import.sql file at application startup
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto = create
# SQL dialect for genereting optimized queries
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.default_schema=master_slave
用户.java
@Entity
@Table(name="user")
public class User {
@Id
@Column
@GeneratedValue(strategy=GenerationType.AUTO)
private int userId;
@NotEmpty
@Column
@Max(value=15)
private String lastName;
@NotEmpty
@Column
@Max(value=15)
private String firstName;
@NotEmpty
@Column
private Date dob;
@Column
@Max(value=10)
private String phone;
@NotEmpty
@Column
@Email
private String email;
@NotEmpty
@Column
@Min(value=6)
@Max(value=15)
private String password;
and their getters and setters.
MainDaoImpl.java
@Repository
@Qualifier("mysql")
public class MainDaoImpl implements MainDao {
@Autowired
private JdbcTemplate jdbcTemplate;
/* (non-Javadoc)
* @see com.app.masterSlave.dao.RegistrationDao#userRegistration(com.app.masterSlave.model.User)
*/
@Override
public void userRegistration(User user) {
final String sql="INSERT INTO user(lastName, firstName, dob, phone, email, password) VALUES (?, ?, ?, ?, ?, ?)";
final String lastName=user.getLastName();
final String firstName=user.getFirstName();
final Date dob=user.getDob();
final String phone = user.getPhone();
final String email = user.getEmail();
final String password = user.getPassword();
// BCryptPasswordEncoder encodedPassword = new BCryptPasswordEncoder();
// final String password = encodedPassword.encode(user.getPassword());
int success = jdbcTemplate.update(sql, new Object[] {lastName, firstName, dob, phone, email, password});
if(success==0) {
System.out.println("There was an error while insertion.");
}
else {
System.out.println("Row with email: " + email + " inserted successfully.");
}
}
}
提前致谢。
【问题讨论】:
-
应用程序启动时是否发现异常?
-
不,我在应用程序启动时没有看到任何错误。我在下面看到一种警告:Loading class
com.mysql.jdbc.Driver'. This is deprecated. The new driver class iscom.mysql.cj.jdbc.Driver'。驱动通过SPI自动注册,一般不需要手动加载驱动类。 -
<artifactId>mysql-connector-java</artifactId>你在pom中添加了吗? -
是的,我有。
mysql mysql-connector-java 6.0.5 -
@EnableAutoConfiguration 是否存在于您的主 ApplicationClass 中?
标签: mysql hibernate jpa spring-boot