一、数据库
1.创建数据库hibernate01-1514010311
2.创建表 customer
CREATE TABLE customer( id int(11) not null auto_increment PRIMARY KEY, name VARCHAR(20) DEFAULT NULL COMMENT '姓名', age int(11) DEFAULT NULL COMMENT '年龄', sex VARCHAR(2) DEFAULT null COMMENT '性别', city VARCHAR(20) DEFAULT NULL COMMENT '城市' );
二、创建项目并导入jar包
1.使用eclipse创建web项目,并导入hibernate所需的jar包
(1)在线下载hibernate包 地址:https://sourceforge.net/projects/hibernate/files/hibernate-orm/5.2.10.Final/hibernate-release-5.2.10.Final.zip/download
下载完成进行解压,进入lib->required,将文件夹内文件复制到工程下,导入。
(2)导入log4j日志文件包 (百度网盘链接:https://pan.baidu.com/s/1LxbT65XNE91ceOoWoSz5kA 密码:aso4)
(3)导入jdbc的mysql驱动包 (百度网盘链接:https://pan.baidu.com/s/1jEb50TpWCjvBLYAlBWw8Tg 密码:0vwe)
(4)导入完成如图(本人创建的是web项目)
2.创建实体类
package domain; public class Customer { private Integer id; private String name; private Integer age; private String sex; private String city; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + ", city=" + city + "]"; } }
3.编写映射文件
在domain包下创建Customer.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="domain.Customer" table="customer"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="name" column="name" type="string"></property> <property name="age" column="age" type="integer"></property> <property name="sex" column="sex" type="string"></property> <property name="city" column="city" type="string"></property> </class> </hibernate-mapping>
4.编写核心配置文件hibernate.cfg.xml
在src目录下创建hibernate.cfg.xml
<?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.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 数据库驱动 --> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 数据库url --> <property name="hibernate.connection.url">jdbc:mysql:///hibernate01-1514010311</property> <!-- 数据库连接用户名 --> <property name="hibernate.connection.username">root</property> <!-- 数据库连接密码 --> <property name="hibernate.connection.password">0x3137</property> <!-- 将hibernate生成的sql语句打印到控制台 --> <property name="hibernate.show_sql">true</property> <!-- 将hibernate生成的sql语句格式化(语法缩进) --> <property name="hibernate.format_sql">true</property> <mapping resource="domain/Customer.hbm.xml" /> </session-factory> </hibernate-configuration>
5.编写测试类
导入JUnit4运行环境(详情百度JUnit单元测试,小生不才)
选中项目点击鼠标右键->选中Build Path->选中Configure Build Path->选中如图,选择Add Library->选择JUnit->next->finish->apply
在test包下创建CustomerTest类
package test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; import domain.Customer; public class CustomerTest { @Test public void insertTest() { //1.加载核心配置文件 Configuration config = new Configuration().configure(); //2.获取SessionFactory SessionFactory sessionFactory = config.buildSessionFactory(); //3.得到一个session Session session = sessionFactory.openSession(); //4.开启事务 Transaction transaction = session.beginTransaction(); //5.操作 Customer customer = new Customer(); customer.setName("三哥无邪"); customer.setAge(21); customer.setSex("男"); customer.setCity("哈尔滨"); //5.2将数据存储在表中 session.save(customer); //6.提交事务 transaction.commit(); //7.关闭资源 session.close(); sessionFactory.close(); } }
选中@Test下方法名->右键->Run As->JUnit Test
测试添加方法insertTest()
查询数据。
findByIdTest方法,通过id查询Customer.
@Test public void findByIdTest() { //1.加载核心配置文件 Configuration config = new Configuration().configure(); //2.获取SessionFactory SessionFactory sessionFactory = config.buildSessionFactory(); //3.得到一个session Session session = sessionFactory.openSession(); //4.开启事务 Transaction transaction = session.beginTransaction(); //5 操作 Customer customer = session.get(Customer.class, 1); System.out.println(customer); //6.提交事务 transaction.commit(); //7.关闭资源 session.close(); sessionFactory.close(); }
测试结果
修改数据
updateTest()方法
@Test public void updateTest() { //1.加载核心配置文件 Configuration config = new Configuration().configure(); //2.获取SessionFactory SessionFactory sessionFactory = config.buildSessionFactory(); //3.得到一个session Session session = sessionFactory.openSession(); //4.开启事务 Transaction transaction = session.beginTransaction(); //5 操作 Customer customer = new Customer(); customer.setId(1); customer.setName("1514010311"); customer.setAge(21); customer.setSex("男"); customer.setCity("哈尔滨"); session.update(customer); //6.提交事务 transaction.commit(); //7.关闭资源 session.close(); sessionFactory.close(); }
测试结果
就此一个简单的hibernate demo编写完成。