映射文件
1.映射配置文件主要是用于描述实体类与数据表之间的映射关系。
2.位置:要与实体类在同一个包下.
3.名称:类名.hbm.xml
4.约束:hibernate核心jar包下的org.hibernate包下hibernate-mapping-3.0.dtd文件中查找
<?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>
<!-- name:实体类 table:表名 catalog:数据库名 -->
<class name="HibernateTest.Customer" table="t_customer" catalog="hibernatetest">
<!-- id描述主键 -->
<id name="id" column="id">
<!-- 主键生成策略 -->
<generator class="native"></generator>
</id>
<!-- 描述属性与字段对应关系 -->
<property name="name" column="name" length="20"></property>
<property name="address" column="address" length="20"></property>
</class>
</hibernate-mapping>
hibernate核心配置文件
1.它主要是hibernate框架所使用的,它主要包含了连接数据库相关信息,hibernate相关配置等。
2.位置:在src下创建一个hibernate.cfg.xml
3.约束文件所在位置:hiberante核心jar包下的org.hibernate包下
<?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.url">jdbc:mysql:///hibernatetest</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 可以将数据库发送的Sql显示出来 -->
<property name="hibernate.show_sql">true</property>
<!-- 模式化sql -->
<property name="hibernate.format_sql">true</property>
<!-- Hibernate的方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置Hibernate的映射文件所在位置 -->
<mapping resource="HibernateTest/Customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
增删改查测试类
hibernate工作原理:
1、通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件。
2、由hibernate.cfg.xml中的<mappingresource=“xx/xx/xxx.hbm.xml”/>读取解析映射信息。
3、通过config.buildSessionFactory();//得到sessionFactory。
4、sessionFactory.openSession();//得到session。
5、session.beginTransaction();//开启事务。
6、persistent operate;
7、session.getTransaction().commit();//提交事务
8、关闭session;
9、关闭sessionFactory;
package HibernateTest;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
public class CustomerTest1 {
//保存
@Test
public void saveCustomer() {
Customer cus = new Customer();
cus.setName("李四");
cus.setAddress("天津");
//使用Hibernate的api来完成将customer保存到MySQL中
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
//相当于获得一个连接
Session session = sessionFactory.openSession();
//开启事务
session.beginTransaction();
//操作
session.save(cus);
//事务提交
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
//查询
@Test
public void findCustomer() {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
//相当于得到一个连接
Session session = sessionFactory.openSession();
session.beginTransaction();
Customer c = session.load(Customer.class, 1);
System.out.println(c.getName());
//事务提交
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
//修改
@Test
public void updateCustomer() {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Customer c = session.get(Customer.class, 1);
c.setName("李四");
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
//删除
@Test
public void deleteCustomer() {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Customer c = session.get(Customer.class, 1);
session.delete(c);
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
//查询所有
@Test
public void searchAll() {
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Query query = session.createQuery("from Customer");
List<Customer> list = query.list();
for (Customer customer : list) {
System.out.println(customer.getName());
}
session.getTransaction().commit();
session.close();
sessionFactory.close();
}
}