Hibernate入门案例(Maven项目)

Hibernate的相关jar包:

案例:

1. 导入相关的依赖
<dependencies>
    <!--导入hibernate需要的依赖-->
    <dependency>
        <groupId>antlr</groupId>
        <artifactId>antlr</artifactId>
        <version>2.7.6</version>
    </dependency>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.0.7.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>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.11.0.GA</version>
    </dependency>

    <dependency>
        <groupId>org.apache.geronimo.specs</groupId>
        <artifactId>geronimo-jta_1.1_spec</artifactId>
        <version>1.1.1</version>
    </dependency>

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.6</version>
    </dependency>

    <!--mysql连接依赖-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.32</version>
    </dependency>
    <!--junit单元测试依赖-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

</dependencies>

2. 根据表结构创建domain/pojo下的model类(实体类)
* 表结构 :
FieldType
id            int(11)
username    varchar(32) 
birthday    datetime 
sex            char(1) 
address    varchar(256) 
* 实体类User
public class User {
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public Date getBirthday() {
    return birthday;
}

public void setBirthday(Date birthday) {
    this.birthday = birthday;
}

public String getSex() {
    return sex;
}

public void setSex(String sex) {
    this.sex = sex;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}
}

3. 创建model的映射文件

* UserModel.hbm.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>
    <!-- 类与表的映射制作在class元素上 -->
    <!-- name:全路径类名 -->
    <!-- table:表名 -->
    <class name="com.qin.domain.User" table="user">
        <!-- 主键的映射制作在id元素上 -->
        <!-- name:对象中用于作为主键的属性名 -->
        <!-- colomn:表中主键字段名 -->
        <!-- 如果name与column值相同,可以省略column -->
        <id name="id" column="id">
            <!-- 将generator元素class属性设置为"assigned" 手动生成,必须给id -->
            <generator class="identity"  />

            <!--**要注意Hibernate主键生成策略**-->

        </id>
        <!-- 属性与字段的映射制作在property元素上 -->
        <!-- name:类中的属性名 -->
        <!-- column:表中的字段名 -->
        <!-- 如果name与column值相同,可以省略column -->
        <property name="username" />
        <property name="birthday" />
        <property name="sex" />
        <property name="address"/>
    </class>
</hibernate-mapping>

4. 配置Hibernate的配置文件

* hibernate.cfg.xml. 放到classpath下.
<?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="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/mybatis</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>

        <!-- 可选配置 -->
        <!--是否支持方言  -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <!--执行CURD时是否打印sql 语句  -->
        <property name="show_sql">true</property>

        <!-- 资源注册 -->
        <mapping resource="com/qin/domain/UserModel.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

5. 测试

@Test
public void hbnTest(){
    //1.创建要测试的对象
    User user = new User();
    user.setUsername("hibernateTest");
    user.setSex("男");
    user.setBirthday(new Date());
    user.setAddress("北京");
    //2.加载配置信息
    Configuration conf = new Configuration().configure();
    //3.基于配置信息,创建SessionFactory对象
    SessionFactory sessionFactory = conf.buildSessionFactory();

    //4.打开一个与数据库相关的session对象
    Session session = sessionFactory.openSession();

    //5.开启事务,基于session得到
    Transaction transaction = session.beginTransaction();
    //6.通过session保存数据
    session.save(user);

    //7.提交事务
    transaction.commit();
    //8.操作完毕,关闭session连接对象
    session.close();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值