SSH-Hibernate基础(1)

1,Hibernate

基于对JDBC的封装,ORM框架,简化Dao层编码工作。

--Hibernate目录
1.documentation:存放Hibernate的相关文件与API
lib存放编译和运行所需的jar包,其中required子目录下包含运行必须的jar包
project:存放各种源代码和资源。
在lib、required目录中,包含的jar包

2.案例

--创建实体类
public class Csutomer {
    private int id;
    private String name;
    private String address;

--创建数据库
create database hibernateTest;
User hibernateTEST;
CREATE TABLE t_customer{
id int primary key auto_increment,
name varchar(20),
address varchar(50)
}

--导入Hibernate,lib下required里面的所有Jar包
--导入日志相关的jar包,log4j,slf4j
--导入Hibernate/project/etc/lo4j.properties

--Hibernate 相关的配置文件
有两种:1,。xxx.hbm.xml : 用于描述类与数据中的表的映射关系。
       2.Hibernate.cfg.xml他是hibernate框架核心配置文件。

--映射配置文件
位置:要与实体类在同一个包下
名称:类名.hbm.xml
约束:<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
位置,Hibernate-core,下。

<class name="" table="t_csutomer" catalog="hibernateTest">
Name属性是实体类的全名,table表的名称,catalog数据库名称

<property name="name" column="name" length="20"></property>
<property name="address" column="address" length="50"></property>

--核心配置文件
主要是Hibernate框架所使用的,它主要包含了连接数据库相关信息
位置在src下,Hibernate-core,下。
约束:<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

配置连接数据:参考:C:hibernate-release-5.0.12.Final\project\etc\hibernate.properties
<hibernate-configuration>

<!-- 配置关于数据库连接的4个项:driver,url username password -->
<session-factory>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:121.199.20.141/hibernateTest</property>
<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">1234</property>
<!-- 可以将向数据库发送的SQL显示出来 -->
<property name="hibernate.show_sql">true</property>

<!-- 格式化SQl -->
<property name="hibernate.format_sql">true</property>
<!--Hibernate 方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- 配置Hibernate的映射文件所在的位置 -->
<mapping resource="domain/Csutomer.hbm.xml"/>


</session-factory>

</hibernate-configuration>

--hibernate test
public class HibernateTest {
    public void saveCsutomerTest(){
//      创建一个Csutomer
        Csutomer csutomer = new Csutomer();
        csutomer.setName("tommy");
        csutomer.setAddress("hanzghou");
//      使用Hibernate的API来完成将customer保存到mysql中。
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
//      get connection
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(csutomer);
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }
}

2.Hibernate测试

public class HibernateTest {
    @Test
    public void saveCsutomerTest(){
//      创建一个Csutomer
        Csutomer csutomer = new Csutomer();
        csutomer.setName("hans");
        csutomer.setAddress("shanghai");
//      使用Hibernate的API来完成将customer保存到mysql中。
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
//      get connection
        Session session = sessionFactory.openSession();
        session.beginTransaction();
        session.save(csutomer);
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }

    @Test
    public void FindCustomerByIdTest(){

//      使用Hibernate的API来完成将customer保存到mysql中。
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
//      get connection
        Session session = sessionFactory.openSession();
        Csutomer csutomer = session.get(Csutomer.class,1);
        System.out.println(csutomer.getName());
        session.beginTransaction();
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }

    @Test
    public void updateCustomerTest(){

//      使用Hibernate的API来完成将customer保存到mysql中。
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
//      get connection
        Session session = sessionFactory.openSession();
        Csutomer csutomer = session.get(Csutomer.class,1);

        csutomer.setName("Johnson");
        session.update(csutomer);



        session.beginTransaction();
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }

    @Test
    public void deleteCustomerTest(){

//      使用Hibernate的API来完成将customer保存到mysql中。
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
//      get connection
        Session session = sessionFactory.openSession();
        Csutomer csutomer = session.get(Csutomer.class,3);

        session.delete(csutomer);

        session.beginTransaction();
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }

    @Test
    public void findAllCustomerTest(){

//      使用Hibernate的API来完成将customer保存到mysql中。
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
//      get connection
        Session session = sessionFactory.openSession();



        session.beginTransaction();


        Query query = session.createQuery("from Csutomer");
        List<Csutomer>list = query.list();
        System.out.println(list);
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
    }

}

3.Hibernate执行原理

//      使用Hibernate的API来完成将customer保存到mysql中。
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
//      get connection
        Session session = sessionFactory.openSession();
        //开启事务
        session.beginTransaction();
        session.save(csutomer);
        //事务提交
        session.getTransaction().commit();
        session.close();
        sessionFactory.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值