Hibernate入门(第一天)

注意:一定要把hibernate.hbm.xml放置在src目录下,因为系统默认位置是在src下,否则就会出现加载不到配置文件
sql语句:

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_phone` varchar(64) DEFAULT NULL COMMENT '固定电话',
  `cust_mobile` varchar(16) DEFAULT NULL COMMENT '移动电话',
  PRIMARY KEY (`cust_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

实体类:

    public class Customer {
        private long customerId;
        private String customerName;
        private String customerSource;
        private String customerIndusrtry;
        private String customerLevel;
        private String customerPhone;
        private String customerMobile;
        }

导入JAR包,我们首先建个lib的文件夹,把从官网下载的hibernate中lib中的required中的文件全部导入eclipse中的lib文件夹,以及日志jar包和数据库链接jar包
这里写图片描述
这里写图片描述
导入所有jar包后,记得点击所有jar包,buildpath一下
核心配置文件:hibernate.hbm.xml必须这样命名,源码中加载的文件名,不可以随意起名。
配置我们参考我们从官网下载下来的文件:在上图project文件夹中ect文件的hibernate.properties和hibernate.cfg.xml文件
hibernate.cfg.xml的配置参考下载下来的hibernate.properties中的片段和hibernate.cfg.xml文件

## MySQL
#hibernate.dialect org.hibernate.dialect.MySQLDialect
#hibernate.dialect org.hibernate.dialect.MySQLInnoDBDialect
#hibernate.dialect org.hibernate.dialect.MySQLMyISAMDialect
#hibernate.connection.driver_class com.mysql.jdbc.Driver
#hibernate.connection.url jdbc:mysql:///test
#hibernate.connection.username gavin
#hibernate.connection.password
-----------------------------------------------------
<hibernate-configuration>
    <session-factory name="foo">
        <property name="show_sql">true</property>
        <mapping resource="org/hibernate/test/legacy/Simple.hbm.xml"/>
        <class-cache
            class="org.hibernate.test.legacy.Simple"
            region="Simple"
            usage="read-write"/>
    </session-factory>
</hibernate-configuration>

真正配置文件:hibernate.cfg.xml文件

<hibernate-configuration>
    <session-factory >
    <!-- 数据库配置(必选):基本参数配置 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url"> jdbc:mysql:///test1</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">123456</property>
        <!-- 数据库方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>    
         <!--可选配置,打印sql语句和格式化sql语句  -->
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <!-- 引进映射文件-->
        <mapping resource="com/unistrong/demo/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Customer.hbm.xml文件配置

<hibernate-mapping>
    <!--name实体类,table数据库表名,把实体类和数据表进行映射 -->
    <class name="com.unistrong.demo.Customer" table="cst_customer">
    <!-- 主键进行映射以及生成策略 -->
        <id name="customerId" column="cust_id">
            <generator class="native" />
        </id>
        <property name="customerName" column="cust_name" />
        <property name="customerSource" column="cust_source" />
        <property name="customerIndusrtry" column="cust_industry" />
        <property name="customerLevel" column="cust_level" />
        <property name="customerPhone" column="cust_phone" />
        <property name="customerMobile" column="cust_mobile" />
        <!-- <property name="cust_name" column="cust_name"/> 如果实体类的属性和数据库中的字段一样,就可以不写column,比如这条 -->
    </class>
</hibernate-mapping>

测试方法:

@Test
    @Ignore
    public void save() {
        // 1.加载核心配置文件Hibernate.cfg.xml文件
        Configuration configuration = new Configuration().configure();
        // 2.得到SessionFactory相当于数据库连接池
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        // 3.得到session对象,相当于开启数据库连接池
        Session session = sessionFactory.openSession();
        // 4.手动开启事务
        Transaction ts = session.beginTransaction();
        // 5.添加数据
        Customer customer = new Customer();
        customer.setCustomerName("李四");
        session.save(customer);
        // 6.提交事务
        ts.commit();
        // 7.关闭资源
        session.close();

    }
    @Test
    @Ignore
    public void query() {
        // 1.加载核心配置文件Hibernate.cfg.xml文件
                Configuration configuration = new Configuration().configure();
                // 2.得到SessionFactory相当于数据库连接池
                SessionFactory sessionFactory = configuration.buildSessionFactory();
                // 3.得到session对象,相当于开启数据库连接池
                Session session = sessionFactory.openSession(); 
                // 4.查询数据,get查询及时查询,不使用也会查询出来,loader延时查询只有使用的时候,才会去查询
                Customer customer = session.get(Customer.class, 1l);
                Customer customer1 = session.load(Customer.class, 1l);
                System.out.println(customer);
                System.out.println(customer1);
                // 5.关闭资源
                session.close();
    }
    @Test
    public void update() {
        // 1.加载核心配置文件Hibernate.cfg.xml文件
        Configuration configuration = new Configuration().configure();
        // 2.得到SessionFactory相当于数据库连接池
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        // 3.得到session对象,相当于开启数据库连接池
        Session session = sessionFactory.openSession();
        // 4.手动开启事务
        Transaction ts = session.beginTransaction();
        // 5.添加数据
        Customer customer =session.get(Customer.class, 1l);
        customer.setCustomerName("外星人");
        session.update(customer);
        // 6.提交事务
        ts.commit();
        // 7.关闭资源
        session.close();

    }
}

猜你喜欢

转载自blog.csdn.net/qq_39380737/article/details/82706048