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





  • 相关阅读:
    C# 使用EWS 读取 Exchange 未读邮件并设置为已读
    .NET 操作 Exchange示例
    Elsa-Core 工作流 使用代码启动审批流或触发审批任务
    Elsa-Core-workflow 从代码启动指定工作流
    .NET CORE 开源工作流 elsa-core 基础使用示例
    文档翻译经验分享(Markdown)
    Orchard Core 开发示例教程
    VMware 与 Hyper-v 并存, 可同时运行 非启动项
    SmtpException: Failure sending mail. ---> System.InvalidOperationException: Asynchronous operations are not allowed in this context.
    Linux (Deppin ,Ubuntu )开发环境配置,VUE & dotnetcore 解决 yarn 找不到问题
  • 原文地址:https://www.cnblogs.com/kuyuyingzi/p/4266373.html
Copyright © 2020-2023  润新知