当前位置: 首页 > 知识库问答 >
问题:

如何配置SpringMVC Hibernate?

左丘弘致
2023-03-14

我试图创建一个springmvc项目配置与Hibernate使用web.xml

我的问题是,当我运行时,它不会自动创建用户表。

这是我的代码:

文件spring-config.xml

<!-- Config for soap -->

<bean id="UserWs" class="org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition" lazy-init="true">
    <property name="schemaCollection">
        <bean class="org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection">
            <property name="inline" value="true" />
            <property name="xsds">
                <list>
                    <value>schema/schema1.xsd</value>
                </list>
            </property>
        </bean>
    </property>
    <property name="portTypeName" value="UserService"/>
    <property name="serviceName" value="UserService" />
    <property name="locationUri" value="/ws"/>
</bean>

<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<mvc:resources mapping="/resources/**" location="/resources/" />

<context:component-scan base-package="com.higgsup.internship.soap" />

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/springmvc"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.higgsup.internship.soap.model" />
    <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.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>

<tx:annotation-driven />
<bean id="transactionManager"
      class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<bean id="userDao" class="com.higgsup.internship.soap.dao.UserDAOImpl"></bean>

文件web.xml:

  <!--
    Main configuration file for this Spring web application.
-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        WEB-INF/config/spring-config.xml
    </param-value>
</context-param>
<!--
    Loads the Spring web application context, using the files defined above.
-->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!--
    Define the Spring WS Servlet. The 'transformWsldLocations' param means
    that any WSDLs generated are context-aware and contain the correct
    path to their exposed port types. The 'contextConfigLocation' param
    with an empty value means that the Spring context won't try to load
    a file called webservices-servlet.xml
-->
<servlet>
    <servlet-name>services</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>WEB-INF/config/spring-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>services</servlet-name>
    <url-pattern>/ws</url-pattern>
</servlet-mapping>

文件UserDAO:

package com.higgsup.internship.soap.dao;

import com.higgsup.internship.soap.model.User;

public interface UserDAO {

public User getUser(Integer id);
}

文件UserDaoImpl:

package com.higgsup.internship.soap.dao;

import com.higgsup.internship.soap.model.User;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

public class UserDAOImpl implements UserDAO {

@PersistenceContext
private EntityManager entityManager;

public User getUser(Integer id) {
    User user = entityManager.find(User.class, id);
    return user;
}
}

文件用户:

package com.higgsup.internship.soap.model;

public class User {

private int id;
private String username;
private String password;
private String email;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

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 String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

}

这是我的日志:

共有1个答案

仲霍英
2023-03-14

似乎您还没有添加jpa注释来建模用户类。你能试试下面的模型课吗?

package com.higgsup.internship.soap.model;

@Entity
@Table(name = "tbl_user")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    @Column(name = "username")
    private String username;

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

    @Column(name = "email")
    private String email;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    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 String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
 类似资料:
  • <filter> <filter-name>nutz</filter-name> <filter-class>org.nutz.mvc.NutFilter</filter-class> <init-param> <param-name>modules</param-name> <param-value>net.wendal.nutzbook.

  • 有一个问题,通过遵循回购 https://github.com/razorinc/redis-openshift-example 当我启动redis server时,它显示“[12010]25 Mar 20:14:53#打开端口6379:bind:权限被拒绝” 我试图将端口0更改为端口3128,但仍然得到相同的错误。。。。不知道为什么 --更新当我尝试升级到redis 2.6并使用--port参数

  • 我正在使用Quartz和Spring Framework重写一个遗留项目。原始配置是XML格式的,现在我正在将其转换为JavaConfig。xml配置使用jobDetail来设置触发器bean的作业详细信息属性。但是,当我尝试使用等效的方法,即setter: setJobDetail(simpleJobDetail)时,我收到了一个警告,即setter没有正确的类型(期望JobDetail,但得到

  • 配置代理服务器能干嘛 NEI toolkit 提供了代理服务器的功能,帮助将接口代理到NEI官网或者特定的服务器上 将接口代理到NEI官网 开启该功能只需将server.config.js中的online设为true, 那么对Mock Server的所有请求都将会代理到NEI官网上,该模式也被称为在线模式。 启用在线模式可以减少执行nei update的操作,在频繁更改官网数据的情况下能够大幅加快

  • 我注释了setIntTag(String),但Jaxb marchaller调用setIntTag(Integer)并将Integer传递给它,如果我删除setIntTag(Integer),marchaller调用字符串setter。 如何注释这些方法以保留两个setter,并告诉编组程序使用字符串setter?

  • 问题内容: 我正在尝试使用slf4j + java.util.logging。我知道如何通过和)或其他方式设置Java源代码。 但是,在slf4j中设置配置的文档在哪里? 我很困惑…我有log4j手册,并且熟悉日志适配器的基本知识,但是我不确定如何使它与slf4j + java.util.logging一起使用。 即: 我需要指定哪个.properties文件和/或JVM 命令行参数将其指向我的配

  • 问题内容: 从转到如何配置Client? 从: 至: 我正在使用javax.ws.rs-api-2.0.jar 问题答案: 我假设您正在使用。为此,您可以使用和。 例: 编辑: 我阅读了ClientConfig.property 的API文档。