hibernate的入门程序

一.pom文件

1.代码块

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.7.Final</version>
        </dependency>
        <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
        </dependency>
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-jta_1.1_spec</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.common</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>5.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.18.1-GA</version>
        </dependency>
        <dependency>
            <groupId>org.jboss</groupId>
            <artifactId>jandex</artifactId>
            <version>2.0.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.3.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-jta_1.1_spec</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.18.1-GA</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
    </dependencies>
</project>

2.范例

图片

图片

图片

二.配置mapping文件

0.属性说明

映射文件就是将类与表建立映射关系文件,这个文件只要是XML即可。通常名称:类名.hbm.xml

class标签:建立类和表的映射

name :类的全路径

table :数据库中表的名称。

catalog :数据库名称(可以省略)

id标签:建立主键和类中属性映射

name :类中的属性的名称

column :表中的字段名称。(如果类中的属性名和表中的字段名一致,column可以省略)

property标签:建立普通字段与类中属性映射

name :类中的属性的名称

column :表中的字段名称。(如果类中的属性名和表中的字段名一致,column可以省略)

length :字段的长度(自动创建表)

not-null :非空(自动创建表)

unique :唯一(自动创建表)

1.代码块

<?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">
<!-- package是对象类的所在包路径 -->
<hibernate-mapping package="cn.domain">
    <!-- name是对象类名,table是数据库的表名 -->
    <class name="Customer" table="cst_customer">
        <!-- 配置主键 -->
        <id name="custid" column="cust_id">
            <!-- generator设置主键的生成方式
             native属性值是本地数据库的自增加能力
             -->
            <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>

2.范例

图片

三.对象类

1.代码块

/**
 * 客户实体类
 */
public class Customer implements Serializable {
    private Long custid;
    private String custName;
    private String custSource;
    private String custIndustry;
    private String custLevel;
    private String custAddress;
    private String custPhone;
    @Override
    public String toString() {
        return "Customer{" +
                "custid=" + custid +
                ", custName='" + custName + '\'' +
                ", custSource='" + custSource + '\'' +
                ", custIndustry='" + custIndustry + '\'' +
                ", custLevel='" + custLevel + '\'' +
                ", custAddress='" + custAddress + '\'' +
                ", custPhone='" + custPhone + '\'' +
                '}';
    }
    public Long getCustid() {
        return custid;
    }
    public void setCustid(Long custid) {
        this.custid = custid;
    }
    public String getCustName() {
        return custName;
    }
    public void setCustName(String custName) {
        this.custName = custName;
    }
    public String getCustSource() {
        return custSource;
    }
    public void setCustSource(String custSource) {
        this.custSource = custSource;
    }
    public String getCustIndustry() {
        return custIndustry;
    }
    public void setCustIndustry(String custIndustry) {
        this.custIndustry = custIndustry;
    }
    public String getCustLevel() {
        return custLevel;
    }
    public void setCustLevel(String custLevel) {
        this.custLevel = custLevel;
    }
    public String getCustAddress() {
        return custAddress;
    }
    public void setCustAddress(String custAddress) {
        this.custAddress = custAddress;
    }
    public String getCustPhone() {
        return custPhone;
    }
    public void setCustPhone(String custPhone) {
        this.custPhone = custPhone;
    }
}

2.范例

图片

四.配置hibernate的主配置文件

0.说明

(1)配置session-factory三要素

第一要素:连接数据库的信息

第二要素:hibernate的可选配置

第三要素:映射文件的位置

(2)SQL结构化语言分类:一共6个部分

DDL:数据定义语言(定义表和表结构的创建,修改,删除)

DML:数据操作语言(数据增删改)

DQL:数据查询语言(数据查询)

DCL:数据控制语言(权限控制)

CCL:游标控制语言(指针控制)

TPL:事务处理语言(事务控制)

1.代码块

<!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
                SessionFactory的作用就是创建session-factory对象。
                Session对象是hibernate中操作数据库的核心对象
         -->
    <session-factory>
        <!-- 注意:配置session-factory三要素
            第一要素:连接数据库的信息
            第二要素:hibernate的可选配置
            第三要素:映射文件的位置
         -->
        <!-- 第一部分:连接数据库的信息 -->
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db7</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <!-- 设置数据库的方言 -->
        <property name="hibernate.dialect org.hibernate.dialect.MySQLDialect"></property>
        <!-- 第二部分:hibernate的可选配置 -->
        <!-- 2.1配置显示hibernate的自动生成sql语句 -->
        <property name="hibernate.show_sql">true</property>
        <!-- 2.2使用格式化sql语句打印到控制台 -->
        <property name="hibernate.format_sql">true</property>
        <!-- 2.3配置hibernate何种方式生成DDL语句 -->
        <!-- update表示检测是否实体类的映射配置和数据库的表结构一致,如果不一致,则更新表结构 -->
        <!--
        SQL结构化语言分类:一共6个部分
         DDL:
         DML:
         DQL:
         DCL:数据控制语言(权限控制)
         CCL:游标控制语言(指针控制)
         TPL:事务处理语言(事务控制)
         -->
        <property name="hibernate.hbm2ddl.auto">update</property>
        <!-- 第三部分:映射配置文件的位置 -->
        <mapping resource="cn/dao/CustomerMapping.xml"></mapping>
    </session-factory>
</hibernate-configuration>

2.可选配置的文件类

(1)使用说明

图片

(2)文件

hibernate.properties

3.范例

图片

图片

五.调用类

1.逻辑

步骤分析

     1.解析hibernate的主配置文件

     2.创建sessionFactory对象

     3.获取一个session对象

     4.开启事务

     5.执行sql操作

     6.提交事务

     7.释放资源

2.代码块




import cn.domain.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 * 入门案例一:
 * 使用hibernate往数据库添加一条客户记录
 */
public class HibernateTest01 {
    public static void main(String[] args) throws Exception {
        HibernateTest01 ht = new HibernateTest01();
        ht.selectTest01();
    }
    /**
     * 步骤分析
     *         1.解析hibernate的主配置文件
     *         2.创建sessionFactory对象
     *         3.获取一个session对象
     *         4.开启事务
     *         5.执行sql操作
     *         6.提交事务
     *         7.释放资源
     */
    public void selectTest01()throws Exception{
        //创建用户对象【不重要】
        Customer c = new Customer();
        c.setCustName("fengmo");
        c.setCustAddress("辽宁大连");
        c.setCustLevel("22");

//        1.解析hibernate的主配置文件
        Configuration cfg = new Configuration();
        cfg.configure("hibernateConfig.xml");//重载方法,加载hibernate主配置文件
//        2.创建sessionFactory对象
        SessionFactory sessionFactory = cfg.buildSessionFactory();
//        3.获取一个session对象
        Session session = sessionFactory.openSession();
//        4.开启事务
        Transaction transaction = session.beginTransaction();
//        5.执行sql操作
        session.save(c);
//        6.提交事务
        transaction.commit();
//        7.释放资源
        session.close();
        sessionFactory.close();
    }
}

3.范例

图片

图片

六.源码

hibernate.rar

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值