Mybatis
The MyBatis SQL mapper framework makes it easier to use a relational database with object-oriented applications. MyBatis couples objects with stored procedures or SQL statements using a XML descriptor or annotations. Simplicity is the biggest advantage of the MyBatis data mapper over object relational mapping tools.
sql
This element can be used to define a reusable fragment of SQL code that can be included in other statements. It can be statically (during load phase) parametrized. Different property values can vary in include instances. For example:
<sql id="userColumns">${alias}.id, ${alias}.username, ${alias}.password</sql>
The SQL fragment can then be included in another statement, for example:
<select id="selectUsers" resultType="map">
select
<include refid="userColumns"><property name="alias" value="t1"></property></include>,
<include refid="userColumns"><property name="alias" value="t2"></property></include>
from some_table t1
cross join some_table t2
</select>
association
collection
#{} vs ${}
By default, using the #{} syntax will cause MyBatis to generate PreparedStatement properties and set the values safely against the PreparedStatement parameters (e.g. ?). While this is safer, faster and almost always preferred, sometimes you just want to directly inject a string unmodified into the SQL Statement. For example, for ORDER BY, you might use something like this:
ORDER BY ${columnName}
Here MyBatis won't modify or escape the string.NOTE It's not safe to accept input from user and supply it to a statement unmodified in this way. This leads to potential SQL Injection attacks and therefore you should either disallow user input in these fields, or always perform your own escape and checks.
PreparedStatement对象执行SQL命令时, 命令被数据库进行编译和解析, 然后被放到命令缓冲区. 每当执行同一个PreparedStatement对象时, 它就会被解析一次, 但不会被再次编译. 在缓冲区中可以发现预编译的命令, 并且可以重新使用. 使用PrepareStatement对象带来的编译次数减少能够提高数据库的总体性能.
问题
1. duplicate collection entities when having multiple one-to-many relations
2.
参考
- Mybatis 官网 http:\/\/www.mybatis.org\/mybatis-3\/
- Mybatis 源码 https:\/\/github.com\/mybatis\/mybatis-3