hibernate 搭建框架

需要用的包

Hibernate的日志记录:

* Hibernate日志记录使用了一个slf4j:

* SLF4J,即简单日志门面(Simple Logging Facade for Java),不是具体的日志解决方案,它只服务于各种各样的日志系统。

 

引入log4j.properties.

 

创建一个数据表

create database hibernate_day01;

use hibernate_day01;

create table customer(

id int primary key auto_increment,

name varchar(20),

age int

);

 

 

 创建实体类和映射

public class Customer {

private int id;

private String name;

private int age;

...

}

 

创建映射:

* 在实体类所在的包下创建一个与类同名.hbm.xml(但是映射文件只要是XML格式文件即可)

* 引入映射文件的约束:

* hibernate3.jar/org/hibernate/hibernate-mapping-3.0.dtd

<!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="cn.itcast.hibernate.demo1.Customer" table="customer">

<!--

使用id标签完成类中属性与表中的主键对应

* name :类中属性名

* column :表中的字段名

-->

<id name="id" column="id">

<!-- 主键生成策略 -->

<generator class="native"/>

</id>

 

<!--

使用property标签完成类中普通属性与表中的字段映射

* name :类中属性名

* column :表中的字段名

-->

<property name="name" column="name"/>

<property name="age" column="age"/>

</class>

</hibernate-mapping>

 

 

创建hibernate的核心xml配置文件

src下新建一个hibernate.cfg.xml

* 引入约束:* hibernate3.jar/org/hibernate/hibernate-configuration-3.0.dtd

<!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">123</property>

<!-- Hibernate的方言: -->

<property name="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</property>

 

<!-- 配置Hibernate可选的属性 -->

<!-- 显示SQL -->

<property name="hibernate.show_sql">true</property>

<!-- 格式化SQL -->

<property name="hibernate.format_sql">true</property>

<!-- hbm2ddl -->

<property name="hibernate.hbm2ddl.auto">update</property>

 

<!-- 引入映射文件: -->

<mapping resource="cn/itcast/hibernate/demo1/Customer.hbm.xml" />

 

</session-factory>

</hibernate-configuration>

 

 

 编写一个测试类

@Test

/**

 * 向数据库中插入一条记录.

 */

public void demo1(){

// 1.加载核心配置文件:加载src下的hibernate.cfg.xml

Configuration configuration = new Configuration().configure();

// 2.创建一个SessionFactory对象.

SessionFactory sessionFactory = configuration.buildSessionFactory();

// 3.通过SessionFactory生成一个Session(Connection)对象.

Session session = sessionFactory.openSession();

// 4.开启事务.

Transaction tx = session.beginTransaction();

// 5.执行操作.

// 插入记录.

Customer customer = new Customer();

customer.setName("任童");

customer.setAge(38);

 

session.save(customer);

 

// 6.提交事务.

tx.commit();

// 7.释放资源

session.close();

sessionFactory.close();

}

 

 查看结果

猜你喜欢

转载自www.cnblogs.com/0307lmy/p/11685330.html
今日推荐