hibernate日常笔记

本文介绍了Spring框架下JdbcTemplate的基本配置方法,展示了如何通过Hibernate进行动态SQL查询,并提供了多种ApplicationContext的获取方式。此外,还讨论了Hibernate配置及优化技巧,包括使用公式(formula)关键词进行映射、左连接查询实现及dynamic-insert的性能优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jdbcTemplate

< bean  id =  "jdbcTemplate"  class =  "org.springframework.jdbc.core.JdbcTemplate"  >
             < property  name  = "dataSource"  ref = "nuDataSource" />
      </ bean  >

映射文件使用formula(公式)关键词

在list页面要显示分类的名称,但有没有配置对象关系,那好如果做呢?
Factory.hbm.xml 中定义虚拟列 typeName
<property name="typeName" type="string"
formula="(SELECT t.name FROM sys_code_b t WHERE t.sys_code_id=ctype and t.parent_id='0103')"
insert="false" update="false">
</property>

List.jsp
<td>${typeName}</td>

应用formula,注意其SQL的特殊写法。
返回单值
设定别名
Where字段为数据库字段,不是映射字段。大小写没关系。

ApplicationContext的三种获取方式

第一种方式:ApplicationContext context = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
第二种方式:Application context = WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext())
ProcessEngine processEngine = context.getBean("processEngine");
第三种方式: AbstractApplicationContext context =  AppContext. getInstance ().getAppContext()
context.getBean(dao)

boolean的默认在bean中设置,映射文件中不能设置默认值

private  boolean  success  =  false   //申请是否成功
其他类型的属性设置默认大概也一样,还没测试
    

测试

当对象叫spring管理后,有两种方法测试
1、专门弄一个Action,利用前台访问的方式做测试
2、 ApplicationContext context =  new  ClassPathXmlApplicationContext( "spring/applicationContext.xml"  );
           SessionFactory sessionFactory = (SessionFactory)context.getBean( "sessionFactory"  );
           Session session = sessionFactory.openSession();
           Transaction transaction = session.beginTransaction();
//         applyDao.saveResume(resume);
           Apply apply =  new  Apply();
           Resume resume =  new  Resume();
           resume.setApply( apply);
            apply.getResumes().add(resume);
           session.save( apply);
           transaction.commit();
           session.close();

返回为空处理

List list =  hibernateTemplate .find(  "from Resume where rid=?" ,rid);
             return   (list== null ||list.size()==0)? null  :(Resume)list.get(0);

hibernate.cfg.xml初始化细节
如果表已经存在,表有数据,重新初始化hibernate.cfg.xml不能够将表中的字段类型和数据清空


左连接实例1

根据关系获取权限
public  Collection<Menuitem> getAllMenuitemByEid (Serializable eid) {
       //  TODO  Auto-generated method stub
        return  this . hibernateTemplate .find( "from Menuitem m left join fetch m.users u where u.uid=?" ,eid) ;
}


dynamic-insert优化性能

如果一个表的结构很复杂,字段很多的情况下,使用dynamic-insert,dynamic-update能够性能上的少许提升。
用法:<class name="model.User" table="Users"  dynamic-insert="true" dynamic-update="true">
使用前:
================testSaveUser=================
Hibernate: insert into Users (age, firstname, lastname) values (?, ?, ?)
================testUpdateUser=================
Hibernate: insert into Users (age, firstname, lastname) values (?, ?, ?)
Hibernate: update Users set age=?, firstname=?, lastname=? where ID=?
使用后:
================testSaveUser=================
Hibernate: insert into Users (age) values (?)
================testUpdateUser=================
Hibernate: insert into Users (age) values (?)
Hibernate: update Users set firstname=? where ID=?
原理:再次运行测试类,就会发现生成的SQL中涉及的字段只包含User类中修改的属性所对应的表字段。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值