MyBatis Learning Road (ii) Mapper Agent development Method-development specification and implementation

Source: Internet
Author: User

1. Namespace equals Mapper interface address in Mapper.xml

<?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.jjt.ssm.mapper.TuserMapper" >

<select id= "Finduserbyid" parametertype= "int" resulttype= "Com.jjt.ssm.pojo.Tuser" >

SELECT * from Tuser WHERE Id=#{userid}

</select>

</mapper>

The method name in the 2.mapper.java interface is consistent with statement in Mapper.xml (Finduserbyid)

The method input parameter type in the 3.mapper.java interface is consistent with the ParameterType specified type of mapper.xml and statement.

The return type in the 4.mapper.java interface is consistent with the Resulttype specified type of mapper.xml and statement.

Import Com.jjt.ssm.pojo.Tuser;

Public interface Tusermapper {
Public Tuser Finduserbyid(int userid) throws Exception;
}

Sqlmapconfig.xml Configuration

<?xml version= "1.0" encoding= "UTF-8"?> <! DOCTYPE configuration Public "-//mybatis.org//dtd Config 3.0//en"
"Http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
<!--and spring are integrated after this environments is removed--

<environments default= "Development" >
<environment id= "Development" >
<transactionmanager type= "JDBC"/>
<!--Configuring database connection Information--
<datasource type= "Pooled" >
<!--Value property value references the values configured in the Db.properties configuration file--
<property name= "Driver" value= "Com.mysql.jdbc.Driver"/>
<property name= "url" value= "jdbc:mysql://127.0.0.1:3306/jiajitao?useunicode=true&amp;characterencoding= UTF-8 "/>
<property name= "username" value= "root"/>
<property name= "Password" value= "root"/>
</dataSource>
</environment>
</environments>

<mappers>
<!--<mapper resource= "Sqlmap/tuser.xml"/>--
<mapper resource= "Mapper/tusermapper.xml"/>
</mappers>
</configuration>

Test class

Import Java.io.InputStream;

Import org.apache.ibatis.io.Resources;
Import org.apache.ibatis.session.SqlSession;
Import Org.apache.ibatis.session.SqlSessionFactory;
Import Org.apache.ibatis.session.SqlSessionFactoryBuilder;
Import Org.junit.Before;
Import Org.junit.Test;

Import Com.jjt.ssm.pojo.Tuser;

public class Tusermappertest {
Sqlsessionfactory sqlsessionfactory;

@Before
public void SetUp () throws Exception {
String resource = "Sqlmapconfig.xml";
Configuration file Read
InputStream InputStream = resources.getresourceasstream (Resource);

Sqlsessionfactory = new Sqlsessionfactorybuilder (). Build (InputStream);
}

@Test
public void Testfinduserbyid () throws Exception {
Sqlsession sqlsession = Sqlsessionfactory.opensession ();
Tusermapper usermapper = Sqlsession.getmapper (Tusermapper.class);
Tuser user = Usermapper.finduserbyid (1);
System.out.println (User.getusername ());

}

}

MyBatis Learning Road (ii) Mapper Agent development Method-development specification and implementation

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.