第一步:
在test包中创建一个生成表的java类:
package com.test;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class 生成表 {
/**
* @param args
*/
public static void main(String[] args) {
Configuration cfg = new Configuration().configure();
SchemaExport ex = new SchemaExport(cfg);
ex.create(true, true);
}
}
第二步:
写一个创建session的类:
package com.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateSessionFactory {
private static Configuration cfg = new Configuration().configure();
private static SessionFactory factory = cfg.buildSessionFactory();
private static ThreadLocal<Session> local = new ThreadLocal<Session>();
public static Session getSession(){
Session session = local.get(); //取
if (session==null || session.isOpen()==false){
session = factory.openSession();
local.set(session); //存
}
return session;
}
}
第三步:
修改hibernate的配置文件相关属性、驱动。
<?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>
<property name="myeclipse.connection.profile">【这里是什么数据库就写什么】mysql</property>
<property name="connection.url">
【数据库连接是什么数据库写什么数据库test
】jdbc:mysql://localhost:3306/test
</property>
<property name="dialect">
【是mysql就写mysql orc就orc】org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="connection.driver_class">
【驱动要改 】com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/pojos/TSaleformDetail3.hbm.xml" />
<mapping resource="com/pojos/TSaleform3.hbm.xml" />
</session-factory>
</hibernate-configuration>
第四步 改pojo映射文件:
<hibernate-mapping>
<class name="com.pojos.TSaleform3" table="T_SALEFORM3" schema="test">【这地方的schema=“数据库名字”
】
分享到:
相关推荐
最专业的用于hiberante的源码分析
JPA注解创建数据库表 和hibernate.cfg.xml 建表
NULL 博文链接:https://alpha2009.iteye.com/blog/1074750
NULL 博文链接:https://jsonwood.iteye.com/blog/2229105
Struts2.0+hibernate+spring代码生成器 是用.net开发的 需要先安装.net环境才能运行
http://blog.csdn.net/e421083458/article/details/8794127 该源码为Hibernate教程配套源码
无法带图发表,请下载附件的word文档,里边有详细的截图! 博文链接:https://hanyou.iteye.com/blog/134799
Struts2+Hibernate+Freemarker实现【增删改查】生成静态页面!这是一个相对网上较全的案例。源码内含数据库文件sql.sql及效果图!本人致力于完整的Demo。欢迎关注!回头补上分页。敬请期待!
基于JCF的代码生成工具 博文链接:https://onecan.iteye.com/blog/83136
hibernate3.0 和数据库生成pojo类的工程方法
NULL 博文链接:https://zljava.iteye.com/blog/301130
rar中包含整个项目的源码和数据库生成脚本,采用jax-ws发布Web Service服务,支持java客户端和.Net客户端调用,数据库采用oracle10g,里面有创建数据库脚本文件createTable_Oracle10g.sql,由于Hibernate映射表中会...
NULL 博文链接:https://bbpbbf.iteye.com/blog/827305
使用Hibernate编写通用数据库操作代码
NULL 博文链接:https://lizhw.iteye.com/blog/733567
NULL 博文链接:https://skiof007.iteye.com/blog/1796910
struts,hibernate和spring