1.在官网去下载,解压后在lib/required目录下的所有jar包都是必要的。
2.为了之后配置方便,建议在eclipse里面安装一个东东,操作步骤是在eclipse->Help->Install New Software:如图
在Work with输入http://download.jboss.org/jbosstools/neon/stable/updates/,找到与Hibernate相关的勾选,由于我已经安装了所以这里没有出现相关勾选项目。
安装成功后新建都会出现这个。
3.导入必要的jar包,在eclipse->Window->Preferences->Java->Buid Path->User Libraries
经过两步,第一是New,然后再Add External JARs。
4.新建一个Dynamic Web Project,然后右键新建的项目Build Path->Configuration Build Path,
并且把mysql的连接包导进来。
5.在项目的src目录右键new创建hibernate.cfg.xml,然后next,填写必要信息:
代码如下:
<?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="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/db_jiuye</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <mapping resource="model/Admin.hbm.xml"/> </session-factory> </hibernate-configuration>
6.src下新建类
1 package model; 2 3 public class Admin { 4 private int userId; 5 private String userName; 6 private String userPwd; 7 public int getUserId() { 8 return userId; 9 } 10 public void setUserId(int userId) { 11 this.userId = userId; 12 } 13 public String getUserName() { 14 return userName; 15 } 16 public void setUserName(String userName) { 17 this.userName = userName; 18 } 19 public String getUserPwd() { 20 return userPwd; 21 } 22 public void setUserPwd(String userPwd) { 23 this.userPwd = userPwd; 24 } 25 }
在model包下面新建Admin类的Admin.hbm.xml文件,一定要注意Admin.hbm.xml文件的table="t_admin"是对应数据库中的一个表名,然后column对应的name同样是t_admin表对应字段。
1 <?xml version="1.0"?> 2 <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 3 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 4 <!-- Generated 2017-6-13 8:59:34 by Hibernate Tools 3.5.0.Final --> 5 <hibernate-mapping> 6 <class name="model.Admin" table="t_admin"> 7 <id name="userId" type="int"> 8 <column name="userId" /> 9 <generator class="assigned" /> 10 </id> 11 <property name="userName" type="java.lang.String"> 12 <column name="userName" /> 13 </property> 14 <property name="userPwd" type="java.lang.String"> 15 <column name="userPw" /> 16 </property> 17 </class> 18 </hibernate-mapping>
接下来修改hibernate.cfg.xml在session-factory下添加<mapping resource="model/Admin.hbm.xml"/>。
7.写一个测试类:
1 package dao; 2 3 import org.hibernate.Session; 4 import org.hibernate.SessionFactory; 5 import org.hibernate.Transaction; 6 import org.hibernate.cfg.Configuration; 7 8 import model.Admin; 9 10 public class AdminManager { 11 12 public static void main(String[] args) { 13 // TODO Auto-generated method stub 14 // 获取配置文件信息 15 Configuration configuration = new Configuration().configure(); 16 // 创建SessionFactory,生成Session 17 SessionFactory sessionFactory = configuration.buildSessionFactory(); 18 // 生成session 19 Session session = sessionFactory.openSession(); 20 // 开始事务 21 Transaction transaction = session.beginTransaction(); 22 // 创建进行持久化对象 23 Admin admin = new Admin(); 24 admin.setUserName("张三"); 25 admin.setUserPwd("123"); 26 // 保存 27 session.save(admin); 28 // 提交事务 29 transaction.commit(); 30 // 关闭session 31 session.close(); 32 // 关闭sessionFactory 33 sessionFactory.close(); 34 } 35 36 }
测试效果