搭建hibernate框架
来源:互联网 发布:3m 异味抑制剂 知乎 编辑:程序博客网 时间:2024/05/22 12:59
一、
- 下载hibernate3.6.0 解压缩后将hibernate3.jar和lib下的required、jpa子目录下所有JAR包添加到应用类加载路径中
- 下载SLF4J日志工具需要包slf4j-1.6.1.zip将压缩包中的slf4j-nop-1.6.1.jar添加到系统的类加载路径下
- 下载mysql数据库连接驱动 mysql-connector-java-5.1.28-bin.jar
- 如果用C3P0连接池,则将hibernate-distribution-3.6.0.Final/lib的optional子目录下的c3p0目录下的JAR包添加到系统的类加载路径下
二、
- 实体类
package org.crazyit.app.domain;public class News { //消息类的标识属性 private Integer id; private String title; private String content; public News() { super(); } 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; }}
2.映射文件News.hbm.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-mapping SYSTEM "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" ><hibernate-mapping package="org.crazyit.app.domain"> <class name="News" table="news_table"> <id name="id"> <generator class="identity"/> </id> <property name="title"/> <property name="content"/> </class></hibernate-mapping>
3.hibernate配置文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" ><hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="hibernate.c3p0.max_size">20</property> <property name="hibernate.c3p0.min_size">1</property> <property name="hibernate.c3p0.timeout">5000</property> <property name="hibernate.c3p0.max_statements">100</property> <property name="hibernate.c3p0.idle_text_period">3000</property> <property name="hibernate.c3p0.acquire_increment">2</property> <property name="hibernate.c3p0.validate">true</property> <!-- 指定数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property> <!-- 根据需要自动创建数据表 --> <property name="hbm2ddl.auto">update</property> <!-- 罗列所有的映射文件 --> <mapping resource="org/crazyit/app/domain/News.hbm.xml"/> </session-factory></hibernate-configuration>
4.测试类
package lee;import org.crazyit.app.domain.News;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;public class NewsManager { public static void main(String[] args) { Configuration conf = new Configuration().configure(); SessionFactory sf = conf.buildSessionFactory(); Session sess = sf.openSession(); Transaction tx = sess.beginTransaction(); News n = new News(); n.setTitle("中国"); n.setContent("成立了!"); sess.save(n); tx.commit(); sess.close(); sf.close(); }}
5.看效果
阅读全文
0 0
- 【hibernate框架】搭建hibernate环境
- hibernate框架的搭建
- Hibernate框架的搭建
- Hibernate框架搭建步骤
- SpringMVC+hibernate框架搭建
- Hibernate环境框架搭建
- Hibernate框架的搭建
- Hibernate 框架的搭建
- Hibernate框架的搭建
- hibernate框架搭建
- HiberNate框架搭建--1
- Hibernate框架搭建
- Eclipse搭建Hibernate框架
- Hibernate框架搭建步骤
- eclipse搭建Hibernate框架
- 搭建hibernate框架
- hibernate框架搭建
- Struts2+Hibernate+Spring框架搭建
- tenda路由器的设置
- 欺骗词法
- 数组元素的上下移动
- 合计函数无法和where条件一起使用
- 百度地图区域边界显示
- 搭建hibernate框架
- error: expected unqualified-id before ‘decltype’
- JAVA数值保留小数点后两位,千分位加","
- Eclipse项目导入idea中
- pandas排序
- 掘金小册:Git 原理详解和实用指南
- 在学习javascript中遇到的问题
- HOG特征(Histogram of Gradient)
- POJ:2187 Beauty Contest 旋转卡壳