hibernate(一)第一个例子
一、创建一个java project名为HibernateDemo1
然后导入hibernate包,eclipse中具体操作:
点击菜单栏windows->preferences->java->bulid path->User Libraries
然后new->输入hibernate(不勾选下边的选择框)->add external jars这时候就要选择包了,将下载的hibernate4.3.11目录lib下required中的jar文件全部加载。然后在项目上右键选择bulid path->add libraries->user libraries 然后选择刚才创建的hibernate。这时hibernate就加载进来了
然后再加载mysql驱动,项目上右键bulid path->add external archives 选择下载的mysql JDBC驱动包
二、在mysql数据库中创建测试表student
create database hibernate; use hibernate; create table student( id int auto_increment primary key, name varchar(20), age int );
三、写代码
创建包cn.orlion.hibernate.model,然后创建一个Student类:
package cn.orlion.hibernate.model; public class Student { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
然后创建hibernate配置文件hibernate.cfg.xml,在src目录下创建这个文件,然后将参考文档中的配置copy进去(如下:)修改mysql配置,注释掉暂时用不到的项
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "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"></property> <!-- hibernate自带JDBC 连接池,暂时不用注释掉 --> <!-- <property name="connection.pool_size">1</property> --> <!-- 改成MySqlDialect(mysql的sql语句) --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- Enable Hibernate's automatic session context management --> <!-- <property name="current_session_context_class">thread</property> --> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- 打印出所有的sql --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <!-- <property name="hbm2ddl.auto">update</property> --> <mapping resource="cn/orlion/hibernate/model/Student.hbm.xml"/> </session-factory> </hibernate-configuration>
然后在cn.orlion.hibernate.model(跟实体类放一起)下创建文件Student.hbn.xml,从参考文档中copy修改
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="cn.orlion.hibernate.model"> <class name="Student" table="student"><!-- 如果表名与类名相同可以不用写table属性 --> <!-- 用id映射主键 --> <id name="id" column="id"> <!-- 列名相同可以不指定column属性 --> </id> <property name="name" column="name"></property> <property name="age"></property> </class> </hibernate-mapping>
然后创建一个包cn.orlion.test创建类StudentTest
package cn.orlion.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import cn.orlion.hibernate.model.Student; public class StudentTest { public static void main(String[] args){ Student s = new Student(); s.setId(1); s.setName("test1"); s.setAge(1); Configuration cfg = new Configuration(); SessionFactory sf = cfg.configure().buildSessionFactory();// 这里会提示deprecated,但是按着参考文档会报错 Session session = sf.openSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); session.close(); sf.close(); } }
右键然后运行就可以看到数据库中存入了一条数据: