MyBatis (vi)------load the mapping file via the Mapper interface

Source: Internet
Author: User

The mapping file is loaded via the Mapper interface, which is important for the integration of the following SSM three frameworks. So what is loading the mapping file via the Mapper interface?

We first look at the previous practice, in the global configuration file Mybatis-configuration.xml through the <mappers> tag to load the mapping file, then if our project is large enough, there are many mapping files, do we each map file load it like this, This is certainly not possible, then we need to use the Mapper interface to load the mapping file

Previous practice:

  

Improved practice: Use the Mapper interface to load a mapping file

1. Define Usermapper Interface
Package Com.ys.mapper;import Org.apache.ibatis.annotations.delete;import Org.apache.ibatis.annotations.Insert; Import Org.apache.ibatis.annotations.select;import Org.apache.ibatis.annotations.update;import com.ys.po.User;  Public interface Usermapper {//query user table data by ID Public user Selectuserbyid (int id) throws exception;//insert a data public into the user table void Insertuser (user user) throws exception;//modify User table data by id public void Updateuserbyid (user user) throws exception;//root The user table data is deleted according to ID public void Deleteuserbyid (int id) throws Exception;}

2. Load the Usermapper interface (a single load map file) in the global configuration file Mybatis-configuration.xml file

3. Writing usermapper.xml files
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >< Mapper namespace= "Com.ys.mapper.UserMapper" > <!--Query the data ID in the user table by ID: Unique identifier, the ID value in this file cannot be repeated Resulttype: return value type , a database record also corresponds to an object of the entity class ParameterType: The parameter type, which is the type of the query condition--><select id= "Selectuserbyid" resulttype= "Com.ys.po.User "parametertype=" int "> <!--here and the normal SQL query statement is similar, the following #{id} represents a placeholder, it does not have to write the ID, write anything can, but do not empty---select * from the user wh Ere id = #{id1}</select><!--update the data for the user table based on the ID--<update id= "Updateuserbyid" parametertype= "com.ys.po.u Ser "> Update user u <!--<set> <if test=" username! = null and Username! = "" > u.username = #{userna  Me}, </if> <if test= "Sex! = null and sex! =" "> u.sex = #{sex} </if> </set>--<trim prefix= "Set" suffixoverrides= "," > <if test= "username! = null and Username! = '" "> U.username =#{username}, </if> <if test= "Sex! = null and sex! =" "> u.sex = #{sex}, </if> </trim> wh Ere Id=#{id} </update> <!--insert a data to the user table--<insert id= "Insertuser" parametertype= "Com.ys.po.User > <!--return the inserted data primary key to the user object Keyproperty: Set the query to the primary key to parametertype the property specified to the object Select LAST_INSERT_ID (): Query Last execution Inse The primary key ID value returned by the RT operation only applies to the self-increment primary key Resulttype: Specifies the result type of the Select last_insert_id () Order:after, in relation to the order of the Select LAST_INSERT_ID () Operation--&G  T <selectkey keyproperty= "id" resulttype= "int" order= "after" > select last_insert_id () </selectKey> INSERT in to User (username,sex,birthday,address) value (#{username},#{sex},#{birthday},#{address}) </insert> <!--root Data ID Delete User table--<delete id= "Deleteuserbyid" parametertype= "int" > delete from user where Id=#{id} </de Lete> </mapper>

  

4. Testing
Query user table data by id @testpublic void Testselectuserbyid () {/* This string consists of two parts of the Usermapper.xml file <mapper namespace= " Com.ys.po.userMapper "> Namespace value <select id=" Selectuserbyid "> ID value */string statement =" Com.ys.mapper.UserMapper.selectUserById "; User user = Session.selectone (statement, 1); SYSTEM.OUT.PRINTLN (user); Session.close ();}

5. Bulk Load Map File
  <mappers>         <!--bulk Load Mapper         Specifies the package name of the Mapper interface, mybatis the Mapper interface under the Auto-scan package to load-         <package Name= "Com.ys.mapper"/>  </mappers>

  

 

6. Attention

1. The Usermapper interface must have the same name as the Usermapper.xml file and under the same package, that is, the namespace in the Usermapper.xml file is the full class name of the Usermapper interface.

  

2. The method name in the Usermapper interface is consistent with the ID defined in the Usermapper.xml file

3, Usermapper interface input parameter type to be consistent with the parametertype defined in the Usermapper.xml

4. Usermapper interface returns data type consistent with Resulttype defined in Usermapper.xml

MyBatis (vi)------load the mapping file via the Mapper interface

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.