c:\workspace\My1stHibernate\ c:\workspace\My1stHibernate\src c:\workspace\My1stHibernate\classes c:\workspace\My1stHibernate\lib c:\workspace\My1stHibernate\lib\hibernate c:\workspace\My1stHibernate\lib\db |
CREATE TABLE CUSTOMER ( CID INTEGER NOT NULL PRIMARY KEY, USERNAME VARCHAR(12) NOT NULL, PASSWORD VARCHAR(12) ); 编写Java文件 public class Customer { private int id; private String username; private String password; public int getId() { return id; } public String getPassword() { return password; } public String getUsername() { return username; } public void setId(int id) { this.id = id; } public void setPassword(String password) { this.password = password; } public void setUsername(String username) { this.username = username; } } |
import net.sf.hibernate.*; import net.sf.hibernate.cfg.*; public class Test { public static void main(String[] args) { try { SessionFactory sf = new Configuration() .configure().buildSessionFactory(); Session session = sf.openSession(); Transaction tx = session.beginTransaction(); for (int i = 0; i < 200; i++) { Customer customer = new Customer(); customer.setUsername("customer" + i); customer.setPassword("customer"); session.save(customer); } tx.commit(); session.close(); } catch (HibernateException e) { e.printStackTrace(); } } } |
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net /hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="Customer" table="CUSTOMER"> <id name="id" column="CID"> <generator class="increment" /> </id> <property name="username" column="USERNAME" /> <property name="password" column="PASSWORD" /> </class> </hibernate-mapping> |
<?xml version="1.0" ?> <project name="My1stHibernate" default="build" basedir="."> <property name="base.dir" value="." /> <property name="src.dir" value="src" /> <property name="lib.dir" value="lib" /> <property name="build.dir" value="classes" /> <path id="myclasspath"> <fileset dir="${lib.dir}"> <include name="**/*.jar" /> </fileset> <pathelement location="${build.dir}" /> </path> <target name="init"> <mkdir dir="${build.dir}" /> </target> <target name="build" depends="init" description="compile the source files"> <javac classpathref="myclasspath" srcdir="${src.dir}" destdir="${build.dir}" /> <copy todir="${build.dir}" > <fileset dir="${src.dir}" > <exclude name="**/*.java"/> </fileset> </copy> </target> <target name="run" depends="build"> <java classpathref="myclasspath" classname="Test" fork="true" /> </target> <target name="clean"> <delete includeEmptyDirs="true"> <fileset dir="${build.dir}" /> </delete> </target> </project> |
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net /hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory name="java:/hibernate/HibernateFactory"> <property name="show_sql">true</property> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver <!-- 这里是Oracle 9i的JDBC driver class名 --> </property> <property name="connection.url"> jdbc:oracle:oci8:@hibernate_test <!--这里是Oracle的hibernate_test数据库URL --> </property> <property name="connection.username"> 你的数据库用户名 </property> <property name="connection.password"> 你的数据库密码 </property> <property name="dialect"> net.sf.hibernate.dialect.Oracle9Dialect <!-- 这里是Oracle 9i的Dialect --> </property> <mapping resource="Customer.hbm.xml" /> <!-- 指定Customer的映射文件 --> </session-factory> </hibernate-configuration> |
[java] log4j: WARN No appenders could be found for logger (net.sf.hibernate.cfg.Environment). [java] log4j:WARN Please initialize the log4j system properly. [java] Hibernate: insert into CUSTOMER (USERNAME, PASSWORD, CID) values (?, ?, ?) BUILD SUCCESSFUL |