如何配置SpringMVC + Hibernate?

时间:2016-07-02 16:54:33

标签: hibernate spring-mvc

我尝试使用web.xml创建一个带有hibernate的springmvc项目配置

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

这是我的代码:

文件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);
}

File 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;
}

}

这是我的日志:

  

02-Jul-2016 23:46:57.342 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log服务器版本:Apache Tomcat / 9.0.0.M4   02-Jul-2016 23:46:57.343 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log服务器内置:2016年3月12日11:39:59 UTC   02-Jul-2016 23:46:57.343 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log服务器编号:9.0.0.0   02-Jul-2016 23:46:57.343 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log操作系统名称:Windows 7   02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log操作系统版本:6.1   02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log架构:x86   02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:C:\ Program Files(x86)\ Java \ jdk1.8.0_60 \ jre   02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM版本:1.8.0_60-b27   02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM供应商:Oracle Corporation   02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:C:\ Users \ hunghip.IntelliJIdea15 \ system \ tomcat \ Unnamed_spring-soap-hibernate-xml_2   02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:D:\ Setup \ tomcat \ apache-tomcat-9.0.0.M4   02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log命令行参数:-Dcom.sun.management.jmxremote =   02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log命令行参数:-Dcom.sun.management.jmxremote.port = 1099   02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log命令行参数:-Dcom.sun.management.jmxremote.ssl = false   02-Jul-2016 23:46:57.344 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log命令行参数:-Dcom.sun.management.jmxremote.authenticate = false   02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log命令行参数:-Djava.rmi.server.hostname = 127.0.0.1   02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log命令行参数:-Djava.util.logging.config.file = C:\ Users \ hunghip.IntelliJIdea15 \ system \ tomcat的\ Unnamed_spring皂,休眠,xml_2 \的conf \ logging.properties   02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log命令行参数:-Djava.util.logging.manager = org.apache.juli.ClassLoaderLogManager   02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log命令行参数:-Dcatalina.base = C:\ Users \ hunghip.IntelliJIdea15 \ system \ tomcat \ Unnamed_spring-soap -HIBERNATE-xml_2   02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log命令行参数:-Dcatalina.home = D:\ Setup \ tomcat \ apache-tomcat-9.0.0.M4   02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log命令行参数:-Djava.io.tmpdir = D:\ Setup \ tomcat \ apache-tomcat-9.0.0 .M4 \ TEMP   02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent使用APR版本1.5.2加载基于APR的Apache Tomcat Native库1.2.7。   02-Jul-2016 23:46:57.345 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR功能:IPv6 [true],sendfile [true],接受过滤器[false],随机[true]。   02-Jul-2016 23:46:58.376 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL已成功初始化(OpenSSL 1.0.2h 2016年5月3日)   02-Jul-2016 23:46:58.606 INFO [main] org.apache.coyote.AbstractProtocol.init初始化ProtocolHandler [&#34; http-nio-8080&#34;]   02-Jul-2016 23:46:58.625 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector使用共享选择器进行servlet写入/读取   02-Jul-2016 23:46:58.630 INFO [main] org.apache.coyote.AbstractProtocol.init初始化ProtocolHandler [&#34; ajp-nio-8009&#34;]   02-Jul-2016 23:46:58.631 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector使用共享选择器进行servlet写入/读取   02-Jul-2016 23:46:58.632 INFO [main] org.apache.catalina.startup.Catalina.load初始化在2099 ms内处理   02-Jul-2016 23:46:58.674 INFO [main] org.apache.catalina.core.StandardService.startInternal开始服务Catalina   02-Jul-2016 23:46:58.674 INFO [main] org.apache.catalina.core.StandardEngine.startInternal启动Servlet引擎:Apache Tomcat / 9.0.0.M4   02-Jul-2016 23:46:58.682 INFO [main] org.apache.coyote.AbstractProtocol.start启动ProtocolHandler [http-nio-8080]   02-Jul-2016 23:46:58.690 INFO [main] org.apache.coyote.AbstractProtocol.start启动ProtocolHandler [ajp-nio-8009]   02-Jul-2016 23:46:58.692 INFO [main] org.apache.catalina.startup.Catalina.start服务器启动时间为59毫秒   连接到服务器   [2016-07-02 11:46:59,006]神器spring-soap-hibernate-xml:war:正在部署神器,请稍候......   02-Jul-2016 23:47:02.461 INFO [RMI TCP连接(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars至少有一个JAR被扫描用于尚未包含TLD的TLD。为此记录器启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表。在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间。   02-Jul-2016 23:47:02.520 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext根WebApplicationContext:初始化已启动   02-Jul-2016 23:47:02.625 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh刷新根WebApplicationContext:启动日期[星期六02月23日23:47: 02 ICT 2016];上下文层次结构的根   02-Jul-2016 23:47:02.673 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions从ServletContext资源加载XML bean定义[/ WEB-INF / config /spring-config.xml]   02-Jul-2016 23:47:03.109 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor。 JSR-330&#39; javax.inject.Inject&#39;找到并支持自动装配的注释   02-Jul-2016 23:47:03.357 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory为持久性单元构建JPA容器EntityManagerFactory&#39;默认&#39;   02-Jul-2016 23:47:03.385 INFO [RMI TCP连接(3)-127.0.0.1] org.hibernate.jpa.internal.util.LogHelper.logPersistenceUnitInformation HHH000204:处理PersistenceUnitInfo [       名称:默认       ...]   02-Jul-2016 23:47:03.507 INFO [RMI TCP连接(3)-127.0.0.1] org.hibernate.Version.logVersion HHH000412:Hibernate Core {5.2.1.Final}   02-Jul-2016 23:47:03.509 INFO [RMI TCP连接(3)-127.0.0.1] org.hibernate.cfg.Environment。 HHH000206:找不到hibernate.properties   02-Jul-2016 23:47:03.511 INFO [RMI TCP连接(3)-127.0.0.1] org.hibernate.cfg.Environment.buildBytecodeProvider HHH000021:字节码提供者名称:javassist   02-Jul-2016 23:47:03.583 INFO [RMI TCP连接(3)-127.0.0.1] org.hibernate.annotations.common.reflection.java.JavaReflectionManager。 HCANN000001:Hibernate Commons Annotations {5.0.1.Final}   02-Jul-2016 23:47:04.038 INFO [RMI TCP连接(3)-127.0.0.1] org.hibernate.dialect.Dialect。 HHH000400:使用方言:org.hibernate.dialect.MySQLDialect   02-Jul-2016 23:47:04.484 INFO [RMI TCP连接(3)-127.0.0.1] org.hibernate.tool.schema.internal.SchemaCreatorImpl.applyImportSources HHH000476:执行导入脚本&#39; org.hibernate.tool .schema.internal.exec.ScriptSourceInputNonExistentImpl @ 93c19f&#39;   02-Jul-2016 23:47:04.495 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.buildNativeEntityManagerFactory初始化JPA EntityManagerFactory用于持久性单元&#39;默认&#39;   02-Jul-2016 23:47:04.781 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache寻找@ControllerAdvice:Root WebApplicationContext:启动日期[Sat Jul 02 23:47:02 ICT 2016];上下文层次结构的根   02-Jul-2016 23:47:04.831 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache寻找@ControllerAdvice:Root WebApplicationContext:启动日期[Sat Jul 02 23:47:02 ICT 2016];上下文层次结构的根   02-Jul-2016 23:47:04.906 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler映射URL路径[/ resources / ]到处理程序& #39; org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0&#39;   02-Jul-2016 23:47:05.023 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext根WebApplicationContext:初始化在2503 ms完成   02-Jul-2016 23:47:05.056 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet&#39;服务&#39;:初始化已启动   02-Jul-2016 23:47:05.059 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh刷新名称空间&#39; services-servlet&#39;的WebApplicationContext :启动日期[Sat Jul 02 23:47:05 ICT 2016]; parent:Root WebApplicationContext   02-Jul-2016 23:47:05.060 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions从ServletContext资源加载XML bean定义[/ WEB-INF / config /spring-config.xml]   02-Jul-2016 23:47:05.116 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor。 JSR-330&#39; javax.inject.Inject&#39;找到并支持自动装配的注释   02-Jul-2016 23:47:05.144 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory为持久性单元构建JPA容器EntityManagerFactory&#39;默认&#39;   02-Jul-2016 23:47:05.144 INFO [RMI TCP连接(3)-127.0.0.1] org.hibernate.jpa.internal.util.LogHelper.logPersistenceUnitInformation HHH000204:处理PersistenceUnitInfo [       名称:默认       ...]   02-Jul-2016 23:47:05.161 INFO [RMI TCP连接(3)-127.0.0.1] org.hibernate.dialect.Dialect。 HHH000400:使用方言:org.hibernate.dialect.MySQLDialect   02-Jul-2016 23:47:05.170 INFO [RMI TCP连接(3)-127.0.0.1] org.hibernate.tool.schema.internal.SchemaCreatorImpl.applyImportSources HHH000476:执行导入脚本&#39; org.hibernate.tool .schema.internal.exec.ScriptSourceInputNonExistentImpl @ 93c19f&#39;   02-Jul-2016 23:47:05.174 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.buildNativeEntityManagerFactory初始化JPA EntityManagerFactory用于持久性单元&#39;默认&#39;   02-Jul-2016 23:47:05.226 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache寻找@ControllerAdvice:WebApplicationContext for namespace&# 39; services-servlet&#39;:启动日期[星期六02月23日23:47:05 ICT 2016]; parent:Root WebApplicationContext   02-Jul-2016 23:47:05.243 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache寻找@ControllerAdvice:WebApplicationContext for namespace&# 39; services-servlet&#39;:启动日期[星期六02月23日23:47:05 ICT 2016]; parent:Root WebApplicationContext   02-Jul-2016 23:47:05.289 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler映射URL路径[/ resources / ]到处理程序&#39; org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0&#39;   02-Jul-2016 23:47:05.380 INFO [RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet&#39;服务&#39;:初始化完成时间为323毫秒   [2016-07-02 11:47:05,399] Artifact spring-soap-hibernate-xml:war:神器已成功部署   [2016-07-02 11:47:05,400] Artifact spring-soap-hibernate-xml:war:Deploy花了6,394毫秒   02-Jul-2016 23:47:08.688 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory部署Web应用程序目录D:\ Setup \ tomcat \ apache-tomcat-9.0.0.M4 \的webapps \经理   02-Jul-2016 23:47:08.872 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory部署Web应用程序目录D:\ Setup \ tomcat \ apache-tomcat-9.0.0.M4 \ webapps \ manager已经完成了184毫秒

1 个答案:

答案 0 :(得分:1)

似乎您没有添加jpa注释来为User类建模。 你可以尝试使用以下模型类吗?

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;
    }
}