时间:2021-07-01 10:21:17 帮助过:45人阅读
一、Hello World Hibernate搭建环境 1、建立一个Project,导入数据库驱动程序,Hibernate所需的jar包 2、创建model层,创建Student类,分别设定id,name,age属性及setter()和getter()方法 3、编写hibernate.cfg.xml ?xml version=1.0 encoding=utf-8?!DOCTYPE
Hibernate搭建环境
1、建立一个Project,导入数据库驱动程序,Hibernate所需的jar包
2、创建model层,创建Student类,分别设定id,name,age属性及setter()和getter()方法
3、编写hibernate.cfg.xml
com.mysql.jdbc.Driver jdbc:mysql://localhos【本文来自鸿网互联 (http://www.68idc.cn)】t:3306/hibernate root root org.hibernate.dialect.MySQLDialect org.hibernate.cache.internal.NoCacheProvider true
4、编写Student.hbm.xml
5、连接本地数据库,创建数据库hibernate和表student
create database hibernate; use hibernate; create table student(id int primary key, name varchar(20), age int);
6、创建测试类StudentTest.java
package com.zgy.hibernate.model; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class StudentTest { public static void main(String[] args){ Student s = new Student(); s.setId(1); s.setName("s1"); s.setAge(1); Configuration cfg = new Configuration(); SessionFactory sf = cfg.configure().buildSessionFactory(); Session session = sf.openSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); session.close(); sf.close(); } }
7、运行,测试成功