This time, it's mainly mybatis part.
1. MyBatis basis: The difference between #{...} and ${...}
For:
MyBatis interprets #{...} as a parameter marker for the JDBC prepared statement. Instead, the ${...} is interpreted as a string substitution. It is useful to understand the difference between the two, because parameter markers (parameter markers) cannot be used in some SQL statements.
For example, we cannot use parameter markers in the location of table name.
Suppose you have the following code:
map<string , object > parms = new hashmap<string , object > (); Parms.put ( "table" , "foo" ); //table name parms.put (, 37 ); //query filter Criteria List<object > Rows = Mapper.generalselect (parms);
<select id="generalSelect" parameterType="map"> selectfromwhere#{criteria} </select>
The SQL statements generated by MyBatis (prepared statement) are as follows:
selectfromwhere col1 = ?
Important: Note that you use the $ {...} (string substitution) may have the risk of a SQL injection attack. In addition, string substitution can often occur when dealing with complex types, such as date types. Due to these factors, we recommend that you use #{...} this way whenever possible.
2, there are two ways to use like.
For:
(recommended) The first method is to add SQL wildcard characters to the Java code.
Example one:
String"%Smi%"; List<Name> names = mapper.selectLike(wildcardName);
<select id="selectLike"> selectfromwherelike#{value} </select>
The second way is to stitch the wildcard characters in the SQL statement. This approach is relatively less secure because of the potential for SQL injection attacks.
Example two:
String"Smi"; List
<select id="selectLike"> selectfromwhere‘%‘‘${value}‘‘%‘ </select>
Important: Please note the use of $ and # in two ways!
3, how to perform bulk insert?
For
First, create a simple INSERT statement:
01.id="insertName"> 02. into names (name) values (#{value}) 03.</insert>
Then execute the batch insert in the Java code as follows:
list<string> names = new arraylist<string> (); Names. Add("Fred"); Names. Add("Barney"); Names. Add("Betty"); Names. Add("Wilma"); Watch this executortype.. BATCHSqlsession sqlsession = sqlsessionfactory. Opensession(Executortype. BATCH); try {namemapper mapper = sqlsession. Getmapper(Namemapper. Class); for (String name:names) {Mapper. Insertname(name); } sqlsession. Commit(); } finally {sqlsession. Close(); }
4. How to get the auto-generated (primary) key value?
For:
The Insert method always returns an int value-this value represents the number of rows inserted. The automatically generated key values can be set to the passed-in Parameter object after the Insert method finishes executing.
Example:
<insert id = "insertname" usegeneratedkeys= keyproperty=< Span class= "hljs-string" > "id" > Insert into names (name " values (#{name}) </insert>
Name name = new Name(); name.setName("Fred"); int rows = mapper.insertName(name); // 完成后,id已经被设置到对象中 System.out.println("rows inserted = " + rows); System.out.println("generated key value = " + name.getId());
5, how to pass multiple parameters in mapper?
For:
Java's reflection mechanism does not allow the framework to get the name of the parameter (the method signature only parameter types, can be said to optimize, it can be said that the design is so, in short, the name is meaningless), so the MyBatis default name is: Param1,param2 ...
If you want to assign them a name, you can use @param annotations:
import org.apache.ibatis.annotations.Param; publicinterface UserMapper { User selectUser(@Param("username") String username, @Param("hashedPassword") String hashedPassword); }
You can then use the XML as follows (it is recommended to encapsulate a map as a single parameter to mapper):
<select id=”selectUser” resultType=”User”> select id, username, hashedPassword from some_table where#{username} and#{hashedPassword} </select>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Please give me a lot of advice ~
Five Java-related face questions per day (2)