1.添加jar文件:request, core, SLF4J-nop,jpa, mysql
2.建表sql语句: create tablestudent(id int,name varchar(20),age int);
3.建立一个hibernate.cfg.xml,从document中寻找
4.注释掉暂时还不需要的内容<current_sessiong_context_class>、<cache.provider_class>
5.建立Class和数据库隐射类,hbm = hibernatemapping..Student.hbm.xml
修改包名、
<hibernate-mapping>
<class name="com.vemitriq.hibernate.Student">
<id name="id"/>
<property name="name" />
<property name="age"/>
</class>
</hibernate-mapping>
public static void main(String[] args) {
Student s = new Student();
s.setId(1);
s.setName("vemitriq");
s.setAge(1);
//读取配置文件
//导入package为org.
Configuration cfg = new Configuration();
//解析hibernate.cfg.xml
//产生Connectiong的工厂
SessionFactory sf = cfg.configure().buildSessionFactory();
//导入hibernate.sessiong
Session session = sf.openSession();
//操作一定要放在事务当中(transaction)
session.beginTransaction();
session.save(s);
//提交数据请求
session.getTransaction().commit();
//记得关闭
session.close();
sf.close();
}