u012828882
MerlinQiu
2015-11-26 12:58

初次整合hibernate和spring,不能自动生成表

  • spring
  • hibernate

package com.merlin.beans;

import org.hibernate.SessionFactory;

public class Users {

private String id;

private String name;
private String password;

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}   
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}

}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2015-11-26 19:19:37 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping  >
    <class name="com.merlin.beans.Users" table="USERS">
        <id name="id" type="java.lang.String">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" />
        </property>
    </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
default-autowire="no">


















com/merlin/beans/Users.hbm.xml




hibernate.dialect=org.hibernate.dialect.OracleDialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false







测试类代码:

package com.merlin.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.merlin.beans.Users;

public class testSpring {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
     ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");
     Users u=(Users) ac.getBean("users");
     System.out.println("name="+u.getName());
     SessionFactory sf=(SessionFactory) ac.getBean("sessionFactory");   
    Users uu=new Users();
    uu.setId("1");
    uu.setName("aaa");
    uu.setPassword("111");
    sf.openSession().save(uu);
    System.out.println("uu:"+uu.getName());
     System.out.println("session="+sf.getStatistics());
}

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

4条回答

为你推荐

换一换