hibernate的学习(一)

            hibernate的入门

 1.什么是框架

   框架就是一个半成品,已经完成了部分的功能,我们使用的时候只须调它里面的方法或者API

 2.Java EE最早期的三层架构

  web层(jsp servlet)业务逻辑层(EJBjavaBean  持久层(JDBC) ,servlet+jsp+javaBean+JDBC可以完成市面上所有的应用 。但是在企业中不会使用这套架构(过于底层)

企业中一般使用ssh框架 或者是ssm框架

 3.hibernate的入门

  (1)什么是hibernate

     hibernate是一个持久层的的ORM(Object Relationam Mapping对象关系映射)框架。

  (2)怎么使用    

    有三个文件夹 documentation(开发文档)lib(required必须依赖包 optional可选的jar包),project 给我们提供的测试的项目

   (3)Customer类,里面提供了get和set方法

public class Customer {
    private Long cust_id;
    private String cust_name;
    private String cust_source;
    private String cust_industry;
    private String cust_level;
    private String cust_phone;
    private String cust_mobile;
    public Long getCust_id() {
        return cust_id;
    }
    public void setCust_id(Long cust_id) {
        this.cust_id = cust_id;
    }
    public String getCust_name() {
        return cust_name;
    }
    public void setCust_name(String cust_name) {
        this.cust_name = cust_name;
    }
    public String getCust_source() {
        return cust_source;
    }
    public void setCust_source(String cust_source) {
        this.cust_source = cust_source;
    }
    public String getCust_industry() {
        return cust_industry;
    }
    public void setCust_industry(String cust_industry) {
        this.cust_industry = cust_industry;
    }
    public String getCust_level() {
        return cust_level;
    }
    public void setCust_level(String cust_level) {
        this.cust_level = cust_level;
    }
    public String getCust_phone() {
        return cust_phone;
    }
    public void setCust_phone(String cust_phone) {
        this.cust_phone = cust_phone;
    }
    public String getCust_mobile() {
        return cust_mobile;
    }
    public void setCust_mobile(String cust_mobile) {
        this.cust_mobile = cust_mobile;
    }
    
}

Customer.hbm.xml(创建mapping映射关系在这个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>
    <!-- 建立类与表的映射关系 -->
                        <!--当时这里后面少了个引号,直接导致后面的id变成蓝色 -->
    <class name="com.itheima.domain.Customer" table="cst_customer">
            <id  name="cust_id" column="cust_id">
                <!-- <generator class="native"/>     -->    
                <generator class="native"/>
            </id>
            
            <!-- 与普通字段建立对应关系 -->
            <property name="cust_name" column="cust_name"/>
            <property name="cust_source" column="cust_source"/>
            <property name="cust_industry" column="cust_industry"/>
            <property name="cust_level" column="cust_level"/>
            <property name="cust_phone" column="cust_phone"/>
            <property name="cust_mobile" column="cust_mobile"/>
            
    </class>
</hibernate-mapping>

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:///hibernate_day01</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">000000</property>
        <!-- 他的方言 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <!--少了一个mappingresource  -->
        <mapping resource="com/itheima/domain/Customer.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

HibernateDemo01(测试类,用的junit4)

public class HibernateDemo1 {
    @Test
    public void demo01(){
        Configuration configuration=new Configuration().configure();
        //创建sessionFactory对象
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        //开启事务
        Session session = sessionFactory.openSession();
        //手动开启事务
        Transaction transaction = session.beginTransaction();
        //写sql语句
        Customer customer=new Customer();
        customer.setCust_name("张三");
        session.save(customer);
        //提交事务
        transaction.commit();
        //关闭资源
        session.close();
    }
}

还需要创建一个数据库,里面由张表

成功图:

 中间遇到的问题

  一开始xml配置文件不能提示信息:

  解决方案:在window中搜素XML Catalog ,完成以下三个参数的配置。注意中间的key Type选URI

新开一个工作空间workspace需要设置什么:

 (1)配置tomcat:需要注意这两个地方

    

  (2)修改编码格式,设置成UTF-8.

    在window中找到WorkSpace左下角的text file encoding 设置成UTF-8.

猜你喜欢

转载自www.cnblogs.com/bao6/p/10333861.html
今日推荐