zoukankan      html  css  js  c++  java
  • hibernate日常笔记

    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类中修改的属性所对应的表字段。





  • 相关阅读:
    08 组件组合使用
    07 React 组件三大属性-----refs
    06 组件三大属性 ----- props
    05 组件三大属性----state
    04 定义组件的两种方式
    03 动态展示列表数据
    02 创建虚拟DOM的两种方式
    C++动多态和静多态
    django1.7+nginx1.4.4的static配置
    redis client API-----------python
  • 原文地址:https://www.cnblogs.com/kuyuyingzi/p/4266373.html
Copyright © 2011-2022 走看看