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

dynamic-insert,dynamic-update能够性能上的少许提升

 
阅读更多
在Hibernate的映射文件的class tag使用dynamic-insert,dynamic-update,可以优化生成的SQL语句,提高SQL执行效率,最终可以提高系统性能。
如,有一个User类。

public class User {

/** Creates a new instance of User */
public User() {
}
private long id;
private int age;

private String firstname;
private String lastname;
private Set emailAddresses;
//省略getter 和setter方法
}

Hibernate映射文件(User.hbm.xml,省略了文件头声明)定义为:
<hibernate-mapping>
<class name="model.User" table="Users" >
<id name="id" column="ID">
<generator class="native"/>
</id>
<property name="age"/>
<property name="firstname"/>
<property name="lastname"/>

<set name="emailAddresses" table="PERSON_EMAIL_ADDR">
<key column="PERSON_ID"/>
<element type="string" column="EMAIL_ADDR"/>
</set>
</class>
</hibernate-mapping>
我们写一个测试类进行测试UserTest。


public class UserTest extends TestCase {

public UserTest(String testName) {
super(testName);
}

private Session session;
private SessionFactory sessionFactory;

protected void setUp() throws Exception {
sessionFactory=HibernateUtil.getSessionFactory();
session=sessionFactory.openSession();
session.getTransaction().begin();
}

protected void tearDown() throws Exception {
session.getTransaction().commit();
session.close();

}

/**
* Test of getAge method, of class model.User.
*/
public void testSaveUser() {
System.out.println("================testSaveUser=================");

User user = new User();
user.setAge(29);
session.save(user);
assertNotNull("id is assigned !",user.getId());
}
public void testUpdateUser() {
System.out.println("================testUpdateUser=================");

User user = new User();
user.setAge(29);
session.save(user);
assertNotNull("id is assigned !",user.getId());

User _user=(User) session.get(User.class, user.getId());
_user.setFirstname("Array");
session.update(_user);

}


}
运行测试后,此时会生成完整的SQL语句(注意将hibernate属性show_sql设置成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=?

如果我们在<class ...>中加上 dynamic-insert="true" dynamic-update="true",变成如下。

<class name="model.User" table="Users" dynamic-insert="true" dynamic-update="true">

再次运行测试类,就会发现生成的SQL中涉及的字段只包含User类中修改的属性所对应的表字段。

================testSaveUser=================
Hibernate: insert into Users (age) values (?)
================testUpdateUser=================
Hibernate: insert into Users (age) values (?)
Hibernate: update Users set firstname=? where ID=?

如果一个表的结构很复杂,字段很多的情况下,使用dynamic-insert,dynamic-update能够性能上的少许提升。

分享到:
评论

相关推荐

    dynamic-insert,dynamic-update 我有话要说

    NULL 博文链接:https://dreamzhong.iteye.com/blog/1207377

    dynamic-add-date:一款基于MyBatis框架,可以对插入和更新Sql语句动态地添加日期列和对应值的插件

    Dynamic-add-date是基于Mybatis插件原理开发的可以动态在Insert和Update Sql语句中添加日期列和对应的值的插件。 可以解决MySQL 5.6.5之前的版本对自动初始化时间戳的限制: DATETIME列不支持DEFAULT CURRENT_...

    hibernate乐观锁和悲观锁学习

    &lt;class name="org.hibernate.sample.TUser" table="t_user" dynamic-update="true" dynamic-insert="true" optimistic-lock="version"&gt; …… &lt;/hibernate-mapping&gt; optimistic-lock 属性有如下可选取值: none:无...

    unigui0.83.5.820

    - UniTreeView: Node dynamic add/delete support - UniTreeView: Several Memory leak issues - UniDBGrid: DBGrid.Column.Visible bug - PageControl: Render problem in invisible tabs - TUniListBox: Items ...

    mybatis-dynamic-sql:Kotlin和JavaSQL DSL(域特定语言)。 支持MyBatis或Spring JDBC模板的呈现

    该库将生成完整的DELETE,INSERT,SELECT和UPDATE语句。 该库实现的DSL与本机SQL非常相似,但是它包含许多允许非常动态SQL语句的功能。 例如,典型的搜索可以用这样的查询进行编码(以下代码是Kotlin,但是Java代码...

    SQL语法大全

    adOpenDynamic 2 启动一个Dynamic类型的游标。 adOpenStatic 3 启动一个Static类型的游标。 ------------------------------------------------------------- 以上几个游标类型将直接影响到Recordset对象所有的属性...

    微软内部资料-SQL性能优化3

    For example, a second transaction traversing the doubly linked list mentioned above would see the list before or after the insert, but it will see only complete changes. Durability After a ...

    关于游标使用sql

    测试3条数据 ...[ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ] [ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ] [ TYPE_WARNING ] FOR select_statement [ FOR UPDATE [ OF column_name [ ,...n ] ] ]

    Delphi7.1 Update

    * This update can not be removed after it is installed.* You will need the original Delphi 7 installation CD available to install this update.* To install this update from the CD, insert the CD, and ...

    T-SQL基本原理-第三版

    Insert, update, delete, and merge data Use transactions in a concurrent environment Get started with programmable objects–from variables and batches to user-defined functions, stored procedures, ...

    ArcPy and ArcGIS - Second Edition

    Arc Py Cursors - Search, Insert, and Updateb'Chapter 3: Arc Py Cursors - Search, Insert, and Update'b'Pythonfunctions\xc3\x82\xc2\xa0\xc3\xa2\xc2\x80\xc2\x93\xc3\x82\xc2\xa0avoidrepeating code'b'The ...

    MyBatis Dynamic SQL 动态sql案例.zip

    在XML文件中,标签通常被嵌套在其他标签内,如、&lt;insert&gt;和&lt;update&gt;等标签内,用于控制生成的SQL语句的结构和内容。 标签通常包含一个test属性,该属性被用于指定条件表达式。如果表达式的结果为true,则标签内的...

    Delphi7.完美经典

    14-2-5 INSERT、UPDATE语句 14-2-6 DELETE语句 14-3 SQL指令高级使用 14-3-1 UNION运算 14-3-2 JOIN运算 14-3-3 特殊运算符 14-3-4 子查询(Sub Query) 第15章 Delphi数据库程序基础 15-1 Delphi各种数据库...

    微软内部资料-SQL性能优化5

    On a qualified select, update, or delete, the correct leaf page will be the lowest page of the tree in which one or more rows with the specified key or keys reside. A qualified operation is one that ...

    Delphi7.完美经典.part1

    14-2-3 DROP语句 14-2-4 SELECT语句 14-2-5 INSERT、UPDATE语句 14-2-6 DELETE语句 14-3 SQL指令高级使用 14-3-1 UNION运算 14-3-2 JOIN运算 14-3-3 特殊运算符 14-3-4 子查询...

    这是一篇有关 在线聊天系统 的系统报告书

    more effective handling of customer requests, and has a simpler and more easy database access methods, easy to maintain and update. view of the above program, the system uses Html, JavaScript and ...

    sqlmap (懂的入)

    | Insert_priv | enum | | Lock_tables_priv | enum | | max_connections | int | | max_questions | int | | max_updates | int | | max_user_connections | int | | Password | char | | Process_priv | ...

    sciter-sdk-4.0.3.5348

    [textarea, edit] fix of dynamic @novalue handling NOTE: sciter.dlls, sciter.exe and notes.exe Windows executables in SDK are digitally signed now. On MacOS you may need to run SDK executables directly...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    Version 1.7 ----------- - ADD: Delphi/CBuilder 10.2 Tokyo now supported. - ADD: Delphi/CBuilder 10.1 Berlin now supported. - ADD: Delphi/CBuilder 10 Seattle now supported. - ADD: Delphi/CBuilder XE8 ...

Global site tag (gtag.js) - Google Analytics