== Sqlsession.selectone ("test.finduserbyid", id), bad encapsulation, Because the Test.finduserbyid is variable, it is difficult to generate automatically by proxy.
1 Mapper.xml (usermapper.xml) namespace specifies the fully qualified name of the Mapper interface <mapper namespace = " cn.itcast.mybatis.mapper.usermapper " > This step purpose: through Mapper.xml and Mapper.java ( Usermapper.java) are associated. 2 , The ID of statement in Mapper.xml is Mapper.java (Usermapper.java) in the method name 3 , Mapper.xml statement in ParameterType and Mapper.java (Usermapper.java), the method input parameter type is consistent 4 , Mapper.xml statement in Resulttype and Mapper.java (Usermapper.java) have the same type of method return value.
1.1 . 1mapper.xml (usermapper.xml Map file) Mapper Map file naming recommendation: Table name Mapper.xmlnamespace specified as the fully qualified name of the Mapper interface
The Mapper (Usermapper.java) interface MyBatis presents the Mapper interface, which is equivalent to the DAO interface. Mapper the name of the interface Recommendation: Table name Mapper
1.1. 1 Loading Mapper.xml in Sqlmapconfig.xml
The Mapper interface returns a single object and a collection object regardless of whether the query records are individual or multiple, resulttype definitions are consistent in statement, and are the Pojo types of the one-record mappings. The Mapper interface method returns a value, if it is a single object returned, the return value type is the Pojo type, the generated proxy object gets the record internally through SelectOne, and if the return value type is a collection object, the generated proxy object is retrieved internally through SelectList.
Test:
//Session Factory Privatesqlsessionfactory sqlsessionfactory; //Create a factory@Before Public voidinit () throws IOException {//configuration file (sqlmapconfig.xml)String resource ="Sqlmapconfig.xml"; //loading the configuration file into the input streamInputStream InputStream =Resources.getresourceasstream (Resource); //To create a session factorySqlsessionfactory =NewSqlsessionfactorybuilder (). Build (InputStream); } @Test Public voidTestfinduserbyid () throws Exception {sqlsession sqlsession=sqlsessionfactory.opensession (); //Create a proxy objectUsermapper usermapper = Sqlsession.getmapper (usermapper.class); User User= Usermapper.finduserbyid (1); System. out. println (user); } @Test Public voidTestfinduserbyusername () throws Exception {sqlsession sqlsession=sqlsessionfactory.opensession (); //Create a proxy objectUsermapper usermapper = Sqlsession.getmapper (usermapper.class); List<User> list = Usermapper.finduserbyname ("Xiao Ming"); System. out. println (list); }
MYBATIS10 implementing class proxy object development