码迷,mamicode.com
首页 > Web开发 > 详细

ssh整合二之hibernate单独搭建

时间:2018-04-06 22:29:37      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:begin   conf   运行   公司   strip   har   sql   ssh   配置文件   

 1.首先我们需要去拷贝我们的hibernate所需的jar包

技术分享图片
 这里还需要加入我们C3P0的jar包,因为我们hibernate中使用的C3P0连接池
技术分享图片

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();
    }
  }

最后,我们看一下运行结果

技术分享图片

         技术分享图片

 

  希望自己每天都能进步一点点!

 

ssh整合二之hibernate单独搭建

标签:begin   conf   运行   公司   strip   har   sql   ssh   配置文件   

原文地址:https://www.cnblogs.com/wh-share/p/ssh_hibernate.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!