ssh无法自动建表
ssh小白,这个问题已经困扰我好几天了,tomcat启动没有报错,欢迎页面可以正常出来,感觉是不执行src下面代码造成的。TestHibernate.java中只写了一个输出语句,控制台也没有打出来。
软件环境:jdk1.8
eclipse-jee-luna-SR2-win32-x86_64
oracle11g
tomcat8.0
工程结构:
jar包导入到lib下(下面只是hibernate3.6.0的jar包):其中clsses12.jar换成了odbc6.jar(无论换不换都不能自动建表)
代码
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>120</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.xml
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 配置bean的自动扫描与装配 -->
<context:component-scan base-package="com.ssh"></context:component-scan>
<!-- 配置SessionFactory(整合Hibernate) -->
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库连接信息 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></property>
<property name="password" value="${password}"></property>
<!-- 其他配置 -->
<property name="initialPoolSize" value="30"></property>
<property name="maxPoolSize" value="50"></property>
<property name="minPoolSize" value="30"></property>
<property name="acquireIncrement" value="20"></property>
<property name="maxStatements" value="80"></property>
<property name="maxStatementsPerConnection" value="50"></property>
<property name="maxIdleTime" value="200"></property>
</bean>
</property>
<!-- 指定映射文件所在的目录 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:com/ssh/system/domain/</value>
</list>
</property>
<!-- 指定hibernate的配置文件的位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 配置声明式事务管理 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 使用基于注解的声明式事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
jdbc.properties
jdbcUrl=jdbc:oracle:thin:@localhost:1521:SSH
driverClass=oracle.jdbc.driver.OracleDriver
username=scottssh
password=1234
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- hibernate dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property>
<!-- 显示sql语句 -->
<property name="show_sql">true</property>
<!-- sql语句格式化 -->
<property name="format_sql">true</property>
<property name="hibernate.connection.release_mode">after_transaction</property>
</session-factory>
</hibernate-configuration>
Sys_User.java
package com.ssh.system.domain;
import java.io.Serializable;
/**
* 绯荤粺鐢ㄦ埛琛ㄧ殑瀹炰綋绫�
*
* @author 钖勫皬姘�
*/
public class Sys_User implements Serializable {
private static final long serialVersionUID = 1L;
private long id;// 涓婚敭ID
private String loginer_Username;// 鐧诲綍鑰呯敤鎴峰悕
private String loginer_Password;// 鐧诲綍鑰呭瘑鐮�
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getLoginer_Username() {
return loginer_Username;
}
public void setLoginer_Username(String loginer_Username) {
this.loginer_Username = loginer_Username;
}
public String getLoginer_Password() {
return loginer_Password;
}
public void setLoginer_Password(String loginer_Password) {
this.loginer_Password = loginer_Password;
}
}
Sys_User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ssh.system.domain">
<class name="Sys_User" table="ssh_sys_user">
<id name="id">
<generator class="native" />
</id>
<property name="loginer_Username"></property>
<property name="loginer_Password"></property>
</class>
</hibernate-mapping>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题解决了。重写了一遍hibernate.cfg.xml并且放在了src下面
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost :1521:SSH</property>
<property name="hibernate.connection.username">scottssh</property>
<property name="hibernate.connection.password">1234</property>
<!-- hibernate dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- 自动更新表结构 -->
<property name="hbm2ddl.auto">update</property>
<!-- 显示sql语句 -->
<property name="show_sql">true</property>
<!-- sql语句格式化 -->
<property name="format_sql">true</property>
<property name="hibernate.connection.release_mode">after_transaction</property>
<mapping resource="com/ssh/system/domain/Sys_User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
一般这个在configure设置,只用过jpa。原理类似。
TestHibernate.java 中替换了如下代码依然不好用,有大侠可以帮忙看一下到底什么问题吗?
package com.ssh.system.domain;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class TestHibernate {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
tx.commit();
session.close();
sessionFactory.close();
}
}