您的位置:首页 > 编程语言 > Java开发

hibernate.cfg.xml 配置及注意问题详解

2016-07-26 00:00 746 查看
摘要: 简单介绍hibernate的用法和注意一些问题

一 :导入的包

hibernate3.jar 核心包

hibernate-3.6.0 Final\hibernate\lib\required 目录下面的所有的包

c3p0-0.9.1.jar 采用c3p0数据源连接的包

hibernate-jpa-2.0-api-1.0.0.Final.jar

二:创建 一个JOPO对象(一个实体类对象)

public class News {

private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
private String title;
private String content;

}

三:生成News.hbm.xml 映射文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!-- Generated 2010-11-9 18:15:04 by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="hibernate.News" table="news_table">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="title" type="java.lang.String">
<column name="title" />
</property>
<property name="content" type="java.lang.String">
<column name="content" />
</property>
</class>
</hibernate-mapping>

注意此处 为了避免和mysql原生态字段同名 需要手动修改部分字段名称

四:配置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>
<!-- 数据库驱动 -->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 数据库密码 -->
<property name="hibernate.connection.password">123456</property>
<!-- 数据库地址 -->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<!-- 数据库用户名 -->
<property name="hibernate.connection.username">root</property>
<!-- 数据库方言 -- 注意mysql数据库需要改此处 否则报错 >
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 字段显示sql语句 -->
<property name="show_sql">true</property>
<!-- sql语句格式化 -->
<property name="hibernate.format_sql">true</property>
<!-- 自动创建数据表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 映射的xml 注意此处生成的News.hbm.xml和实体类在一起 注意此处的路径 包名/类名.hbm.xml-->
<mapping resource="hibernate/News.hbm.xml"/>

</session-factory>
</hibernate-configuration>


五:利用框架插入一条数据demo

public static void main(String[] args)
throws Exception {
//实例化Configuration,
Configuration conf = new Configuration()
//下面方法默认加载hibernate.cfg.xml文件
.configure();
//以Configuration创建SessionFactory
SessionFactory sf = conf.buildSessionFactory();
//创建Session
Session sess = sf.openSession();
//开始事务
Transaction tx = sess.beginTransaction();
//创建消息实例
News n = new News();
//设置消息标题和消息内容
n.setTitle("疯狂Java联盟成立了");
n.setContent("疯狂Java联盟成立了,"
+ "网站地址http://www.crazyit.org");
//保存消息
sess.save(n);
//提交事务
tx.commit();
//关闭Session
sess.close();
sf.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java hibernate