如何配置SpringMVC + Hibernate?

洪喜

我正在尝试使用web.xml使用hibernate创建springmvc项目配置

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

这是我的代码:

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

}

这是我的日志:

log命令行参数:-Djava.util.logging.manager = org.apache.juli.ClassLoaderLogManager 2016年7月2日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 2016年7月2日23:46:57.345信息[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本机库1.2.7。2016年7月2日23:46:57。345 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR功能:IPv6 [true],sendfile [true],接受过滤器[false],随机[true]。2016年7月2日23:46:58.376信息[主要] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL已成功初始化(OpenSSL 1.0.2h 2016年5月3日)2016年7月2日23:46:58.606信息[主要] org.apache.coyote.AbstractProtocol.init初始化ProtocolHandler [“ http-nio-8080”] 2016年7月2日23:46:58.625信息[main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Servlet读写的共享选择器2016年7月2日23:46:58.630信息[主] org.apache.coyote.AbstractProtocol.init初始化ProtocolHandler [“ ajp-nio-8009”] 2016年7月2日23:46: 58.631 INFO [main] org.apache.tomcat.util.net.NioSelectorPool。战争:工件正在部署中,请稍候... 2016年7月2日23:47:02.461信息[RMI TCP连接(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars至少一个JAR已扫描TLD,但不包含TLD。为该记录器启用调试日志记录,以获取已扫描的JAR的完整列表,但未在其中找到TLD。在扫描过程中跳过不需要的JAR可以缩短启动时间和JSP编译时间。2016年7月2日23:47:02.520信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext根WebApplicationContext:初始化开始于2016年7月2日23:47:02.625 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh刷新根WebApplicationContext:启动日期[Sat Jul 02 23:47:02 ICT 2016];上下文层次结构的根2016年7月2日23:47:02.673信息[RMI TCP连接(3)-127.0.0.1] org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions从ServletContext资源[/ WEB中加载XML bean定义-INF / config / spring-config.xml] 2016年7月2日23:47:03.109信息[RMI TCP连接(3)-127.0.0.1] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor。找到并支持JSR-330'javax.inject.Inject'注释以自动装配02-Jul-2016 23:47:03.357 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory为持久性单元'默认'构建JPA容器EntityManagerFactory 2016年7月2日23:47:03.385 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.jpa.internal.util.LogHelper。logPersistenceUnitInformation HHH000204:处理PersistenceUnitInfo [名称:默认...] 2016年7月2日23:47:03.507信息[RMI TCP连接(3)-127.0.0.1] org.hibernate.Version.logVersion HHH000412:Hibernate Core {5.2。 1.Final} 2016年7月2日23:47:03.509信息[RMI TCP Connection(3)-127.0.0.1] org.hibernate.cfg.Environment。HHH000206:找不到hibernate.properties 2016年7月2日23:47:03.511信息[RMI TCP连接(3)-127.0.0.1] org.hibernate.cfg.Environment.buildBytecodeProvider HHH000021:字节码提供者名称:javassist 02-Jul- 2016 23:47:03.583信息[RMI TCP Connection(3)-127.0.0.1] org.hibernate.annotations.common.reflection.java.JavaReflectionManager。HCANN000001:Hibernate Commons注释{5.0.1.Final} 2016年7月2日23:47:04.038 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.dialect.Dialect。HHH000400:使用方言:org.hibernate。dialect.MySQLDialect 2016年7月2日23:47:04.484信息[RMI TCP连接(3)-127.0.0.1] org.hibernate.tool.schema.internal.SchemaCreatorImpl.applyImportSources HHH000476:执行导入脚本'org.hibernate.tool .schema.internal.exec.ScriptSourceInputNonExistentImpl @ 93c19f'2016年7月2日23:47:04.495信息[RMI TCP连接(3)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.buildNativeEntityManagerFactory初始化的JPA EntityManagerFactory用于持久性单位'default'2016年7月2日23:47:04.781信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache寻找@ControllerAdvice:根WebApplicationContext:启动日期[ICT 2016年7月2日星期六];上下文层次结构的根2016年7月2日23:47:04。831信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache寻找@ControllerAdvice:根WebApplicationContext:启动日期[Sat Jul 02 23:47:02 ICT 2016];上下文层次结构的根02-Jul-2016 23:47:04.906信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler映射的URL路径[/ resources /]放入处理程序'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'2016年7月2日23:47:05.023 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.ContextLoader .initWebApplicationContext根WebApplicationContext:初始化在2503毫秒内完成02-Jul-2016 23:47:05.056 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet'services':初始化开始于2016年7月2日23:47:05.059信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.context.support.XmlWebApplicationContext.prepareRefresh刷新名称空间“ services-servlet”的WebApplicationContext:启动日期[ ICT,2016年7月2日星期六23:47:05];父:Root WebApplicationContext 2016年7月2日23:47:05.060信息[RMI TCP Connection(3)-127.0.0.1]组织。springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions从ServletContext资源中加载XML bean定义[/WEB-INF/config/spring-config.xml] 2016年7月2日23:47:05.116信息[RMI TCP连接(3) -127.0.0.1] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor。找到并支持JSR-330'javax.inject.Inject'注释以自动装配02-Jul-2016 23:47:05.144 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory为持久性单元'默认'构建JPA容器EntityManagerFactory 2016年7月2日23:47:05.144 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.jpa.internal.util.LogHelper.logPersistenceUnitInformation HHH000204:处理PersistenceUnitInfo [名称:默认... ] 02-Jul-2016 23:47:05.161信息[RMI TCP Connection(3)-127.0.0.1] org.hibernate.dialect.Dialect。HHH000400:使用方言:org.hibernate.dialect.MySQLDialect 2016年7月2日23:47:05.170 INFO [RMI TCP Connection(3)-127.0.0.1] org.hibernate.tool.schema.internal.SchemaCreatorImpl.applyImportSources HHH000476:执行导入脚本'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@93c19f'2016年7月2日23:47:05.174 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.orm.jpa .LocalContainerEntityManagerFactoryBean.buildNativeEntityManagerFactory持久性单元“默认”的初始化JPA EntityManagerFactory 2016年7月2日23:47:05.226信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation。 RequestMappingHandlerAdapter。initControllerAdviceCache寻找@ControllerAdvice:名称空间'services-servlet'的WebApplicationContext:启动日期[Sat Jul 02 23:47:05 ICT 2016];父:根WebApplicationContext 2016年7月2日23:47:05.243信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache寻找@ControllerAdvice:WebApplicationContext对于命名空间“ services-servlet”:启动日期[ICT 2016年7月2日星期六];父级:Root WebApplicationContext 2016年7月2日23:47:05.289信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler映射的URL路径[/ resources / 根WebApplicationContext 2016年7月2日23:47:05.243信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache寻找@ControllerAdvice:命名空间的WebApplicationContext 'services-servlet':启动日期[ICT 2016年7月2日星期六];父级:Root WebApplicationContext 2016年7月2日23:47:05.289信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler映射的URL路径[/ resources / 根WebApplicationContext 2016年7月2日23:47:05.243信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache寻找@ControllerAdvice:命名空间的WebApplicationContext 'services-servlet':启动日期[ICT 2016年7月2日星期六];父级:Root WebApplicationContext 2016年7月2日23:47:05.289信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler映射的URL路径[/ resources / 05 ICT 2016];父级:Root WebApplicationContext 2016年7月2日23:47:05.289信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler映射的URL路径[/ resources / 05 ICT 2016];父级:Root WebApplicationContext 2016年7月2日23:47:05.289信息[RMI TCP连接(3)-127.0.0.1] org.springframework.web.servlet.handler.SimpleUrlHandlerMapping.registerHandler映射的URL路径[/ resources /]放入处理程序'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'2016年7月2日23:47:05.380 INFO [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet .initServletBean FrameworkServlet'services':初始化在323毫秒内完成[2016-07-02 11:47:05,399] Artifact spring-soap-hibernate-xml:war:Artifact已成功部署[2016-07-02 11:47:05,400 ] Artifact spring-soap-hibernate-xml:war:部署花费了6,394毫秒2016年7月2日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 \ manager 2016年7月2日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毫秒内完成

Chathuranga Tennakoon

似乎您尚未添加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;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章