`
kuyuyingzi
  • 浏览: 52625 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

hibernate日常笔记

 
阅读更多

jdbcTemplate

<beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate">
<propertyname="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中设置,映射文件中不能设置默认值

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

测试

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

返回为空处理

Listlist =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

根据关系获取权限
publicCollection<Menuitem>getAllMenuitemByEid(Serializable eid) {
//TODOAuto-generated method stub
returnthis.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类中修改的属性所对应的表字段。





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics