Method of transmitting parameters
The insert operation implemented in the previous article is implemented by several different methods of communication.
Using @param
In the previous consolidation example, we have used this simplest way of communicating the parameters as follows:
@Insert ("INSERT into USER (NAME, age) VALUES (#{name}, #{age})") int Insert (@Param (' name ') String NAME, @Param ("age") Integer age);
This approach is well understood, and the @Param
definitions in name
SQL correspond to those #{name}
age
in SQL #{age}
.
Using map
The following code, through the Map object, serves as a container for passing parameters:
@Insert ("Insert into USER (NAME, age) VALUES (#{name,jdbctype=varchar}, #{age,jdbctype=integer})") int Insertbymap (MAP <string, object> map);
For the parameters required in the INSERT statement, we only need to fill in the map with the same name, as shown in the following code:
map<string, object> map = new hashmap<> () map.put ("name", "CCC"); Map.put ("Age", 40); Usermapper.insertbymap (map);
Working with objects
In addition to the map object, we can use the normal Java object directly as the parameter of the query, for example, we can use the user object directly:
@Insert ("Insert into USER (NAME, age) VALUES (#{name}, #{age})") int insertbyuser (user user);
In such a statement #{name}
, the #{age}
and attributes in the user object correspond to each other name
age
.
Change and delete
MyBatis provides different annotations for different database operations to configure, as demonstrated in the previous example @Insert
, following a basic set of additions and deletions for the user table as an example:
Public interface Usermapper { @Select ("SELECT * from user WHERE name = #{name}") user Findbyname (@Param ("name") St ring name); @Insert ("INSERT into user (name, age) VALUES (#{name}, #{age})") int Insert (@Param (' name ') String name, @Param ("age") Integer age); @Update ("Update user SET Age=#{age} WHERE name=#{name}") void Update (user user); @Delete ("Delete from user WHERE ID =#{id}") void Delete (Long id);}
After the completion of a set of additions and deletions, we can try the following unit tests to verify the correctness of the above operation:
@RunWith (Springjunit4classrunner.class) @SpringApplicationConfiguration (classes = application.class) @ Transactionalpublic class Applicationtests {@Autowiredprivate usermapper usermapper; @Test @rollbackpublic void Testusermapper () throws Exception {//Insert a piece of data and select out to verify Usermapper.insert ("AAA", 20); User u = usermapper.findbyname ("AAA"); Assert.assertequals (U.getage (). Intvalue ());//Update a data and select to verify U.setage (); usermapper.update (u); u = Usermapper.findbyname ("AAA"); Assert.assertequals (U.getage (). Intvalue ());//delete this data and select Verify Usermapper.delete (U.getid ()); u = Usermapper.findbyname ("AAA"); Assert.assertequals (null, u);}}
Returns the binding of the result
For the increase, delete, change the operation of the relatively small change. And for the "check" operation, we often need to do multi-table association, summary calculation, etc., then for the results of the query is often no longer a simple entity object, often need to return a different than the database entity wrapper class, then for such cases, you can use @Results
and @Result
annotations to bind, Specific as follows:
@Results ({ @Result property = ' name ', column = ' name '), @Result (property = ' age ', column = ' Age ')}) @Select (" Select name, age from User ") list<user> findAll ();
In the preceding code, the property attribute in the @Result corresponds to the member name in the user object, and column corresponds to the field name of the Select. The id attribute is intentionally not detected in this configuration, only the name and age objects in the user counterpart are mapped, so that the ID identified is null by the following unit test, and the other property is not null:
@Test @rollbackpublic void Testusermapper () throws Exception {list<user> userlist = Usermapper.findall (); for (User User:userlist) {assert.assertequals (null, User.getid ()); Assert.assertnotequals (NULL, User.getname ());}}
Source Source
Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (14) using MyBatis annotation configuration in Spring boot