Hibernate种生成表以及一个hibernate数据库文件的配置
Hibernate类生成表以及一个hibernate数据库文件的配置
配置文件 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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <!--Examda提示:数据库用户名--> <property name="connection.username">root</property> <!--数据库URL--> <property name="connection.url"> jdbc:mysql://192.168.1.102:3306/sms?useUnicode=true&characterEncoding=utf8 </property> <!--dialect,每个数据库对应的Dialet匹配其平台特性--> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <!--数据库密码--> <property name="connection.password">root</property> <!--数据库驱动--> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <!--是否将运行产生的sql语句输出到日志--> <property name="hibernate.show_sql">True</property> <!--Examda,是否使用数据库外连接--> <property name="hibernate.use_out_join">True</property> <!--映射类的xml文件--> <mapping resource="cn/com/swansoft/sms/dao/pojo/Sms.hbm.xml" /> <mapping resource="cn/com/swansoft/sms/dao/pojo/SmsUser.hbm.xml" /> <mapping resource="cn/com/swansoft/sms/dao/pojo/SmsTemplate.hbm.xml"/> </session-factory> </hibernate-configuration>
Java引用配置生成表
import java.io.File; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; public class ClassToDB { public static void main(String[] args){ Configuration config = null; Session session = null; Transaction tx = null; try { config = new Configuration().configure(new File( "src/hibernate.cfg.xml")); System.out.println("Creating tables..."); SessionFactory sessionFactory = config.buildSessionFactory(); session = sessionFactory.openSession(); tx = session.beginTransaction(); SchemaExport schemaExport = new SchemaExport(config); schemaExport.create(true, true); System.out.println("Table created."); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); try { tx.rollback(); } catch (HibernateException e1) { e1.printStackTrace(); } } finally { } } }
POJO类举例
package cn.com.swansoft.sms.dao.pojo; // Generated 2011-11-4 8:59:15 by Hibernate Tools 3.2.1.GA import java.util.Date; /** * Sms generated by hbm2java */ public class Sms implements java.io.Serializable { private Long id; private String mobile; private String message; private Boolean send; private Date addTime; private Date sendTime; private Date orderedTime; private String username; public Sms() { } public Sms(String mobile, String message, Date addTime) { this.mobile = mobile; this.message = message; this.addTime = addTime; } public Sms(String mobile, String message, Boolean send, Date addTime, Date sendTime, String username) { this.mobile = mobile; this.message = message; this.send = send; this.addTime = addTime; this.sendTime = sendTime; this.username = username; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getMobile() { return this.mobile; } public void setMobile(String mobile) { this.mobile = mobile; } public String getMessage() { return this.message; } public void setMessage(String message) { this.message = message; } public Boolean getSend() { return this.send; } public void setSend(Boolean send) { this.send = send; } public Date getAddTime() { return this.addTime; } public void setAddTime(Date addTime) { this.addTime = addTime; } public Date getSendTime() { return this.sendTime; } public void setSendTime(Date sendTime) { this.sendTime = sendTime; } public String getUsername() { return this.username; } public void setUsername(String username) { this.username = username; } public Date getOrderedTime() { return orderedTime; } public void setOrderedTime(Date orderedTime) { this.orderedTime = orderedTime; } }
POJO的xml配置举例
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/HibernateMapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2011-11-48:59:19 by Hibernate Tools 3.2.1.GA --> <hibernate-mapping> <class name="cn.com.swansoft.sms.dao.pojo.Sms"table="sms" catalog="sms"> <id name="id" type="java.lang.Long"> <column name="id" /> <generator class="identity" /> </id> <property name="mobile" type="string"> <column name="mobile" length="30"not-null="true" /> </property> <property name="message" type="string"> <column name="message" length="200"not-null="true" /> </property> <property name="send" type="java.lang.Boolean"> <column name="send" not-null="true"/> </property> <property name="addTime" type="timestamp"> <column name="add_time" length="19"not-null="true" /> </property> <property name="sendTime" type="timestamp"> <column name="send_time" length="19"/> </property> <property name="orderedTime" type="timestamp"> <column name="ordered_time" length="19" not-null="true"/> </property> <property name="username" type="string"> <column name="username" length="30"not-null="true" /> </property> </class> </hibernate-mapping>
以上已经完成pojo通过hibernate生成表的操作.下面发布一个项目中的数据库配置。作为以后的参考:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd "> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/sms?useUnicode=true&characterEncoding=utf8" /> <property name="user" value="root" /> <property name="password" value="root" /> <property name="minPoolSize" value="2" /> <property name="maxPoolSize" value="50" /> <property name="initialPoolSize" value="10" /> <property name="maxIdleTime" value="60" /> <property name="acquireIncrement" value="2" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="mappingResources"> <list> <value>cn/com/swansoft/sms/dao/pojo/Sms.hbm.xml</value> <value>cn/com/swansoft/sms/dao/pojo/SmsUser.hbm.xml</value> <value>cn/com/swansoft/sms/dao/pojo/SmsTemplate.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop> </props> </property> </bean> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <context:component-scan base-package="cn.com.swansoft.sms.dao.impl"/> </beans>