First, the realization principle
Mapper interface development method only requires programmers to write the Mapper interface (equivalent to DAO interface), the MyBatis framework creates an interface based on the interface definition of the dynamic proxy object, the proxy object's method body with the upper DAO interface implementation class method.
Mapper interface development is subject to the following specifications:
1. The namespace in the Mapper.xml file is the same as the classpath of the Mapper interface.
2. The Mapper interface method name is the same as the ID of each statement defined in the Mapper.xml
3. The input parameter type of the Mapper interface method is the same as the type of parametertype for each SQL defined in Mapper.xml
4. The output parameter type of the Mapper interface method is the same as the type of resulttype for each SQL defined in Mapper.xml
Second, mapper.xml mapping file
To define the mapper mapping file Usermapper.xml (content same as Users.xml), you need to modify the namespace value to the Usermapper interface path. Place the Usermapper.xml under the Mapper directory under Classpath.
[HTML]View PlainCopyprint?
- <? 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="Cn.itcast.mybatis.mapper.UserMapper">
- <!--get user information by ID--
- <Select id= "Finduserbyid" parametertype="int" resulttype=" Cn.itcast.mybatis.po.User ">
- SELECT * from user where id = #{id}
- </Select>
- <!--custom criteria query user list-
- <Select id= "finduserbyusername" parametertype="java.lang.String "
- resulttype="Cn.itcast.mybatis.po.User">
- SELECT * from user where username like '%${value}% '
- </Select>
- <!--adding users--
- <insert id="Insertuser" parametertype="Cn.itcast.mybatis.po.User">
- <selectkey keyproperty="id" order="after" resulttype="Java.lang.Integer" >
- Select LAST_INSERT_ID ()
- </selectkey>
- Insert into User (username,birthday,sex,address)
- VALUES (#{username},#{birthday},#{sex},#{address})
- </Insert>
- </mapper>
Third, Mapper.java (interface file)
[Java]View PlainCopyprint?
- /**
- * User Management Mapper
- */
- Public interface Usermapper {
- //query user information based on user ID
- Public User Finduserbyid (int id) throws Exception;
- //query user list
- Public list<user> Finduserbyusername (String username) throws Exception;
- //Add user information
- public void Insertuser (user user)throws Exception;
- }
The interface definition has the following characteristics:
1. The ID of the Mapper interface method name and the statement defined in the Mapper.xml are the same
2. The input parameter type of the Mapper interface method is the same as the ParameterType type of the statement defined in the Mapper.xml
3. The output parameter type of the Mapper interface method is the same as the Resulttype type of the statement defined in the Mapper.xml
Iv. Loading Usermapper.xml files
To modify the Sqlmapconfig.xml file:
[HTML]View PlainCopyprint?
- <!--load a mapping file--
- <mappers>
- <mapper resource="Mapper/usermapper.xml"/>
- </mappers>
V. Testing
[Java]View PlainCopy print?
- Public class Usermappertest extends TestCase {
- private Sqlsessionfactory sqlsessionfactory;
- protected void SetUp () throws Exception {
- //mybatis configuration file
- String resource = "Sqlmapconfig.xml";
- InputStream InputStream = resources.getresourceasstream (Resource);
- //Use Sqlsessionfactorybuilder to create Sessionfactory
- Sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);
- }
- Public void Testfinduserbyid () throws Exception {
- //Get session
- sqlsession session = Sqlsessionfactory.opensession ();
- //Get proxy object for mapper interface
- Usermapper usermapper = Session.getmapper (usermapper. Class);
- //Invoke proxy object method
- User user = Usermapper.finduserbyid (1);
- SYSTEM.OUT.PRINTLN (user);
- //Close Session
- Session.close ();
- }
- @Test
- public void Testfinduserbyusername () throws Exception {
- Sqlsession sqlsession = Sqlsessionfactory.opensession ();
- Usermapper usermapper = Sqlsession.getmapper (usermapper. Class);
- list<user> list = Usermapper.finduserbyusername ("Zhang");
- System.out.println (List.size ());
- }
- Public void Testinsertuser () throws Exception {
- //Get session
- sqlsession session = Sqlsessionfactory.opensession ();
- //Get proxy object for mapper interface
- Usermapper usermapper = Session.getmapper (usermapper. Class);
- //data to be added
- User user = new user ();
- User.setusername ("Zhang San");
- User.setbirthday (new Date ());
- User.setsex ("1");
- User.setaddress ("Beijing");
- //Add user via Mapper interface
- Usermapper.insertuser (user);
- //Submit
- Session.commit ();
- //Close Session
- Session.close ();
- }
- }
Vi. Summary
SelectOne and SelectList
The dynamic proxy object calls Sqlsession.selectone () and sqlsession.selectlist () are determined by the return value of the Mapper interface method, and the SelectList method is called if a list is returned. If a single object is returned, the SelectOne method is called.
Namespace
MyBatis official recommend the use of Mapper proxy method to develop Mapper interface, programmers do not have to write Mapper interface implementation class, when using the Mapper proxy method, input parameters can use Pojo wrapper object or Map object, to ensure the universality of DAO.
MyBatis Implement mapper Dynamic proxy mode