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&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