1.首先我们需要去拷贝我们的hibernate所需的jar包
2. 编写我们的关系映射文件Customer.cfg.xml(这里的Customer.cfg.xml和Customer实体类同级)
<?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="com.itheima.entity.Customer" table="cst_customer">
<id name="custId" column="cust_id">
<generator class="native"></generator>
</id>
<property name="custName" column="cust_name"></property>
<property name="custSource" column="cust_source"></property>
<property name="custIndustry" column="cust_industry"></property>
<property name="custLevel" column="cust_level"></property>
<property name="custAddress" column="cust_address"></property>
<property name="custPhone" column="cust_phone"></property>
</class>
</hibernate-mapping>
</hibernate-mapping>
3.编写我们的hibernate的核心配置文件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.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///ssh_280</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 配置C3P0连接池 -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 显示sql语句 show_sql true|false(默认值) -->
<property name="hibernate.show_sql">true</property>
<!-- 格式化sql format_sql true|false(默认值) -->
<property name="hibernate.format_sql">true</property>
<!-- 获取与当前线程绑定的session-->
<property name="hibernate.current_session_context_class">thread</property>
<!-- 映射文件的位置
class:指定配置了jpa注解的实体类的全限定类名
resource:xml格式的映射文件
-->
<mapping resource="com/itheima/entity/Customer.hbm.xml" />
</session-factory>
</hibernate-configuration>
4.创建我们的数据库ssh_280
/*创建客户表*/
CREATE TABLE `cst_customer` (
`cust_id` BIGINT(32) NOT NULL AUTO_INCREMENT COMMENT ‘客户编号(主键)‘,
`cust_name` VARCHAR(32) NOT NULL COMMENT ‘客户名称(公司名称)‘,
`cust_source` VARCHAR(32) DEFAULT NULL COMMENT ‘客户信息来源‘,
`cust_industry` VARCHAR(32) DEFAULT NULL COMMENT ‘客户所属行业‘,
`cust_level` VARCHAR(32) DEFAULT NULL COMMENT ‘客户级别‘,
`cust_address` VARCHAR(128) DEFAULT NULL COMMENT ‘客户联系地址‘,
`cust_phone` VARCHAR(64) DEFAULT NULL COMMENT ‘客户联系电话‘,
PRIMARY KEY (`cust_id`)
) ENGINE=INNODB AUTO_INCREMENT=94 DEFAULT CHARSET=utf8;
/*加入数据*/
INSERT INTO `cst_customer` VALUES (‘1‘, ‘传智播客集团‘, ‘6‘, ‘IT培训‘, ‘23‘, ‘北京市昌平区北七家镇修正大厦‘, ‘010-66668888‘);
INSERT INTO `cst_customer` VALUES (‘2‘, ‘黑马训练营‘, ‘6‘, ‘IT培训‘, ‘23‘, ‘北京市昌平区北七家镇修正大厦‘, ‘010-66668888‘);
INSERT INTO `cst_customer` VALUES (‘3‘, ‘京西集团‘, ‘7‘, ‘电商‘, ‘23‘, ‘京西玉泉山‘, ‘010-65085588‘);
INSERT INTO `cst_customer` VALUES (‘4‘, ‘修正药业‘, ‘7‘, ‘医药‘, ‘22‘, ‘北京市昌平区北七家镇‘, ‘010-68909090‘);
5.写我们的测试类
public class HibernateTest {
@Test
public void addCustomerTest(){
//加载配置hibernate的核心配置文件
Configuration cfg = new Configuration();
cfg.configure();
//创建SessionFactory
SessionFactory factory = cfg.buildSessionFactory();
//获取session对象
Session session = factory.getCurrentSession();
//开启事务
Transaction tx = session.beginTransaction();
//执行操作crud
Customer customer = new Customer();
customer.setCustName("张飞");
session.save(customer);
//提交事务
tx.commit();
}
}
最后,我们看一下运行结果
希望自己每天都能进步一点点!