hibernate——day01

Day01

Hibernate:

定义得到session的方法:(API)

publicclass HibernateUtils {

    // sessionFactory全局只有一个就可以了。

    privatestatic SessionFactory sessionFactory;

    初始化sessionFactory

    static {

    /*  Configurationcfg = new Configuration();

      

       sessionFactory = new Configuration()//

              .configure()//

              .buildSessionFactory();*/

       Configuration cfg = new Configuration();

       cfg.configure();//不加参数默认是加载hibernate.cfg.xml 也可以加上参数加载指定位置的文件

       SessionFactory sessionFactory = cfg.buildSessionFactory();

    }

    publicstatic SessionFactory getSessionFactory() {

       returnsessionFactory;

    }   * 定义得到session的方法

    publicstatic Session openSession() {

       returnsessionFactory.openSession();

    }

}

CURD模板

publicvoid save(User user) {

       Session session = HibernateUtils.openSession();

       Transaction tx=null;

       try {

           tx = session.beginTransaction();// 开启事务

           ………………………………………………………….

           tx.commit(); // 提交事务

       } catch (RuntimeException e) {

           tx.rollback(); // 事务回滚

           throwe;

       } finally {

           session.close(); // 关闭session释放资源

       }

    }

增加用户:

session.save(user);直接调用方法等于调用了insert

修改:调用了update语句update

先通过user.set属性,定义需要修改的属性,然后调用

session.update(user);

删除:需要通过id来删除

Object user = session.get(User.class, id); // 删除用户首先需要得到user

session.delete(user);等于是调用了delete语句

通过id查询:

User user = (User) session.get(User.class,id);

查询所有:

// 方法一

//List<User> list =session.createQuery("FROM User").list();

参数是HQL语句

SQL是用来操作表以及表中的数据的

HQL是用来操作对象以及属相的。

// 方法二

Criteria criteria = session.createCriteria(User.class);

List<User> list = criteria.list();

分页查询:

public QueryResult findAll(intfirstResult, intmaxResults) {

       Session session = HibernateUtils.openSession();

       Transaction tx = null;

       try {

得到查询结果并设置开始行数以及每页最大数

           tx = session.beginTransaction();

           // Query query =session.createQuery("FROM User");

           //query.setFirstResult(firstResult);

           //query.setMaxResults(maxResults);

           //List<User> list = query.list();

           List<User> list = session.createQuery(//

                  "FROM User")//

                  .setFirstResult(firstResult)//

                  .setMaxResults(maxResults)//

                  .list();

 

           // 得到总列数

           Long count = (Long) session.createQuery(//

                  "SELECTCOUNT(*) FROM User")//

                  .uniqueResult();

           tx.commit();

          

           // 封装到一个对象中

           returnnew QueryResult(count.intValue(), list);

       } catch (RuntimeException e) {

           tx.rollback();

           throwe;

       } finally {

           session.close();

       }

    }

配置文件:

主配置文件:Hibernate.cfg.xml

数据库信息5

<hibernate-configuration>

    <session-factory name="foo">

       <!-- 配置数据库信息 -->

1      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>设置方言

2      <property name="connection.url">jdbc:mysql:///shujuku1</property>

3      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

4      <property name="connection.username">root</property>

5      <property name="hibernate.connection.password">123</property>

       <!-- 其他配置 -->

控制台显示  <property name="hibernate.show_sql">true</property>

格式化      <property name="hibernate.format_sql">false</property>

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

       <!-- 导入映射文件 -->

       <mapping resource="cn/itcast/a_helloworld/User.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

映射文件(类对应表,属性对应列)(User.hbm.xml

<hibernate-mapping package="cn.itcast.a_helloworld">

    <class name="User" table="t_user">

       <id name="id"type="int" column="id">

            <generator class="native"/>设置自动生成表结构

       </id>

       <property name="name"type="string" column="name"length="20"/>

    </class>

   

</hibernate-mapping>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值