This article transferred from: http://www.yihaomen.com/article/java/302.htm
Increase:
<!--executes the SQL statement that adds the operation. The ID and ParameterType are identical to the name and parameter type of the AddUser method in the Iuseroperation interface, respectively. Referring to the Name property of the student parameter as #{name}, MyBatis will use reflection to read this property of the student parameter. #{name} is case sensitive in name. References to other properties such as gender are consistent with this. Segeneratedkeys is set to "true" to indicate that MyBatis gets the primary key generated automatically by the database; keyproperty= "id" specifies the id attribute to inject the acquired primary key value into the student - <InsertID= "AddUser"ParameterType= "User"Usegeneratedkeys= "true"Keyproperty= "id">INSERT INTO User (username,userage,useraddress) VALUES (#{username},#{userage},#{useraddress})</Insert>
By deleting:
<id= "DeleteUser" parametertype= "int"> Delete from user where Id=#{id}</Delete>
Change:
<id= "UpdateUser" parametertype= "User"> update user set username=#{username},userage=#{userage},useraddress=#{useraddress} where Id=#{id} </ Update >
Check:
General Query:
<Mappernamespace= "Com.yihaomen.mybatis.models.UserMapper"> <SelectID= "Selectuserbyid"ParameterType= "int"Resulttype= "User">select * from ' user ' where id = #{id}</Select></Mapper>
List query:
Query out the list, that is, to return to list, in our example is list<user>, this way to return the data, you need to configure User.xml inside the returned type Resultmap, note is not resulttype,
And this resultmap is supposed to be our own configuration.
<!--Returnmap defined to return the list type -<Resultmaptype= "User"ID= "Resultlistuser"> <IDcolumn= "id" Property= "id" /> <resultcolumn= "UserName" Property= "UserName" /> <resultcolumn= "Userage" Property= "Userage" /> <resultcolumn= "UserAddress" Property= "UserAddress" /></Resultmap><!--return to the list's SELECT statement, note that the value of Resultmap is pointed to the previously defined -<SelectID= "Selectusers"ParameterType= "string"Resultmap= "Resultlistuser">SELECT * from user where userName like #{username}</Select>
Associated data query:
<!--User Union article configuration for one of the query methods (many-to-one approach) -<ResultmapID= "Resultuserarticlelist"type= "article"> <ID Property= "id"column= "Aid" /> <result Property= "title"column= "title" /> <result Property= "Content"column= "Content" /> <Association Property= "User"Javatype= "User"> <ID Property= "id"column= "id" /> <result Property= "UserName"column= "UserName" /> <result Property= "UserAddress"column= "UserAddress" /> </Association></Resultmap><SelectID= "Getuserarticles"ParameterType= "int"Resultmap= "Resultuserarticlelist">Select User.id,user.username,user.useraddress,article.id aid,article.title,article.content from USER,ARTICL e where User.id=article.userid and User.id=#{id}</Select>
Associated Query improvements:
<Resultmaptype= "User"ID= "Resultlistuser"> <IDcolumn= "id" Property= "id" /> <resultcolumn= "UserName" Property= "UserName" /> <resultcolumn= "Userage" Property= "Userage" /> <resultcolumn= "UserAddress" Property= "UserAddress" /></Resultmap><!--User Union article configuration of Query method two (many-to-one approach) - <ResultmapID= "ResultUserArticleList-2"type= "article"> <ID Property= "id"column= "Aid" /> <result Property= "title"column= "title" /> <result Property= "Content"column= "Content" /> <Association Property= "User"Javatype= "User"Resultmap= "Resultlistuser" /> </Resultmap><SelectID= "Getuserarticles"ParameterType= "int"Resultmap= "Resultuserarticlelist">Select User.id,user.username,user.useraddress,article.id aid,article.title,article.content from User,arti CLE where User.id=article.userid and User.id=#{id}</Select>
Paging query:
<id= "Getuserarticles" parametertype= "Map" Resultmap = "resultuserarticlelist"> Select User.id,user.username,user.useraddress, Article.id aid,article.title,article.content from User,article where User.id=article.userid and User.id=#{id} Limit #{offset},#{pagesize}</Select>
MyBatis additions and deletions to search