gpt4 book ai didi

java - 从maven spring mvc创建表时出现问题

转载 作者:行者123 更新时间:2023-12-02 01:45:07 25 4
gpt4 key购买 nike

我无法从 maven spring mvc 自动创建表。

我正在使用maven做一个spring mvc项目。到目前为止,我没有遇到任何错误,但是当我尝试使用 applicationconfig.xml 在数据库中创建表时,它不起作用。我已经在互联网上搜索过,但我的 applicationconfig.xml 对我来说似乎很好。我没有任何错误。仍然没有创建表..

applicationconfig.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.milan.entities" /><!--scans model/entity/domain(name 3 but same) and registers-->
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/inventorymanagement" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManager" />
</bean>

<tx:annotation-driven />

<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
</beans>

用户类别

package com.milan.entities;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

public class User implements Serializable{
@Id
@Column(name = "USER_ID")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long userId;

@Column(name = "USERNAME")
private String userName;

@Column(name = "PASSWORD")
private String password;

@Column(name = "IS_ENABLEd")
private boolean isEnabled;

public long getUserId() {
return userId;
}

public void setUserId(long userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public boolean isIsEnabled() {
return isEnabled;
}

public void setIsEnabled(boolean isEnabled) {
this.isEnabled = isEnabled;
}
}

运行程序时没有错误,但不会自动创建表。

最佳答案

@Entity 放在您的 User 类上,如果表仍未创建,请尝试在类上添加以下注释:

@Entity
@Table(name = "`user`")

可能会发生,因为 User 是数据库中的保留字。

关于java - 从maven spring mvc创建表时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53790356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com